, когда я пытаюсь callSendAPI
в waether.js
, это показывает, что ошибка не callSendAPI
не является функцией
weather.js
const request = require('request');
const config = require('./config');
const messages = require('./messages');
weatherAsQuickReply = (messageData) => {
let replies = [{
"content_type": "location"
}];
let sendQuickReply = {
recipient: {
id: messageData.recipient.id
},
message: {
text: messageData.message.text,
quick_replies: replies
}
};
messages.callSendAPI(sendQuickReply);
}
getWeather = (result, messageData) => {
let city = result.parameters["geo-city"];
if (city != '') {
request({
url: 'https://api.apixu.com/v1/current.json',
qs: {
q: city,
key: config.WEATHER_APP_ID,
},
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
let weather = JSON.parse(body);
if (weather.hasOwnProperty("current")) {
let reply = `Location: ${weather.location.region}
Temperature: ${weather.current.temp_c},
${weather.current.condition.text}`;
messageData.message.text = reply;
messages.callSendAPI(messageData);
};
} else {
return res.status(400).json({
status: {
code: 400,
errorType: 'I failed to look up the city name.'
}
});
}
});
} else {
weatherAsQuickReply(messageData);
}
}
module.exports = {
weatherAsQuickReply: weatherAsQuickReply,
getWeather: getWeather
}
message.js
const request = require('request');
const config = require('./config');
const weather = require('./weather');
const apiaiApp = require('apiai')(config.API_AI_CLIENT_ACCESS_TOKEN);
callSendAPI = (messageData) => {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: {
access_token: config.FB_PAGE_TOKEN
},
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
if (messageId) {
console.log("Successfully sent message with id %s to recipient %s",
messageId, recipientId);
} else {
console.log("Successfully called Send API for recipient %s",
recipientId);
}
} else {
console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
}
});
}
sendMessage = (event) => {
let sender = event.sender.id;
let text = event.message.text;
let apiai = apiaiApp.textRequest(text, {
sessionId: 'minaaaaa213'
});
apiai.on('response', (response) => {
console.log(response);
let aiText = response.result.fulfillment.speech;
let result = response.result;
let messageData = {
recipient: {id: sender},
message: {text: aiText}
};
if (response.result.action === 'weather') {
console.log('weather');
weather.getWeather(result, messageData);
} else {
callSendAPI(messageData);
}
});
apiai.on('error', (error) => {
console.log('err: ' + error);
});
apiai.end();
}
module.exports = {
callSendAPI: callSendAPI,
sendMessage: sendMessage
}