[英]Wit.ai Not Parsing Data to Chat Response
I am having some problems with my modification of wit.ai's messenger.js on GitHub. 我在GitHub上修改了wit.ai的messenger.js时遇到了一些问题。 I added some API calls to get real time weather but I am having trouble getting the result into the bot's reply (context.forecast).
我添加了一些API调用以获得实时天气但我无法将结果导入机器人的回复(context.forecast)。 The context.forecast line is not able to 'see' my weather data it seems.
context.forecast行无法“看到”我的天气数据。
Most relevant line is: 最相关的是:
context.forecast = 'Todays forecast is: ' + hourlyWeather +' in ' + location;
hourlyWeather has data such as "Light rain until this evening." hourlyWeather的数据包括“直到今晚的小雨”。 but the bots reply leaves it out.
但是机器人的回复让它失败了。
Here is the relevant code, I am hoping I just have something in the wrong place/order as I am new to Node. 这是相关的代码,我希望我只是在错误的地方/顺序中有一些东西,因为我是Node的新手。 I appreciate the help.
我很感激帮助。
// Our bot actions
const actions = {
send({sessionId}, {text}, request) {
//const {context, entities} = request;
// Our bot has something to say!
// Let's retrieve the Facebook user whose session belongs to
const recipientId = sessions[sessionId].fbid;
if (recipientId) {
// Yay, we found our recipient!
// Let's forward our bot response to her.
// We return a promise to let our bot know when we're done sending
return fbMessage(recipientId, text)
.then(() => null)
.catch((err) => {
console.error(
'Oops! An error occurred while forwarding the response to',
recipientId,
':',
err.stack || err
);
});
} else {
console.error('Oops! Couldn\'t find user for session:', sessionId);
// Giving the wheel back to our bot
return Promise.resolve()
}
},
// You should implement your custom actions here
// See https://wit.ai/docs/quickstart
//Problems start here. ----------------
getForecast({context, entities}) {
return new Promise(function(resolve, reject) {
var location = firstEntityValue(entities, 'location')
if (location) {
// we should call a weather API here
//API call to convert city name to longitude latitude
const requestw = require('request'),url = 'http://nominatim.openstreetmap.org/search?q='+location+'&format=json'
request(url, (error, response, body)=> {
if (!error && response.statusCode === 200) {
const fbResponse = JSON.parse(body)
//console.log("Got a response: ", fbResponse)
//convert JSON to array
var arr = [];
for(var x in fbResponse){
arr.push(fbResponse[x]);
}
//find latitude and longitude in array and store for later weather api call
if(arr[0].hasOwnProperty('lat')){
var lat = arr[0].lat;
}
if(arr[0].hasOwnProperty('lon')){
var lon = arr[0].lon;
}
console.log(lat, lon)
//API call for weather status
forecast.get([lat, lon], function(err, weather) {
if(err) return console.dir(err);
//console.dir(weather);
//Store weekly and daily report - Cant get to show up in forecast string. --------------
console.log(weather.hourly.summary)
var hourlyWeather = weather.hourly.summary;
console.log(weather.daily.summary)
var dailyWeather = weather.daily.summary;
});
} else {
console.log("Got an error while grabbing coordinates of city: ", error, ", status code: ", response.statusCode)
}
})
//Cant get hourlyWeather or dailyWeather to show here. Location works fine. Have tried declaring the variables elsewhere. -----------
//context.forecast = hourlyWeather;
context.forecast = 'Todays forecast is: ' + hourlyWeather +' in ' + location;
delete context.missingLocation;
} else {
context.missingLocation = true;
delete context.forecast;
}
return resolve(context);
});
},
};
The bot's reply would only read "Todays forecast is: in Atlanta". 机器人的回复只会是“今天的预测是:在亚特兰大”。
You need to store the hourlyWeather against the context. 您需要将hourlyWeather存储在上下文中。
var hourlyWeather = weather.hourly.summary;
context.hw = hourlyWeather;
Then access the weather via the context. 然后通过上下文访问天气。
context.forecast = 'Todays forecast is: ' + context.hw +' in ' + location;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.