gpt4 book ai didi

javascript - Wit.ai 未将数据解析为聊天响应

转载 作者:行者123 更新时间:2023-11-28 05:35:12 27 4
gpt4 key购买 nike

我在 GitHub 上修改 wit.ai 的 messenger.js 时遇到一些问题。我添加了一些 API 调用来获取实时天气,但我无法将结果输入机器人的回复(context.forecast)。 context.forecast 行似乎无法“查看”我的天气数据。

最相关的行是:

context.forecast = 'Todays forecast is: ' + hourlyWeather +' in ' + location;

hourlyWeather 具有诸如“直到今晚为止的小雨”之类的数据。但机器人的回复却忽略了它。

这是相关的代码,我希望我只是在错误的位置/顺序中有一些东西,因为我是 Node 的新手。我很感谢您的帮助。

// 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);
});
},
};

机器人的回复只会显示“今天的天气预报是:在亚特兰大”。

最佳答案

您需要根据上下文存储 hourlyWeather。

var hourlyWeather = weather.hourly.summary;
context.hw = hourlyWeather;

然后通过上下文访问天气。

context.forecast = 'Todays forecast is: ' + context.hw +' in ' + location;

关于javascript - Wit.ai 未将数据解析为聊天响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39417130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com