我正在关注Wit AI教程,我遇到了困难的地步。 我正在尝试扩展快速启动天气教程,以调用实际的天气API,我没有运气。
这是我修改过的getForecast方法。原文可以在这里找到:https://wit.ai/docs/quickstart
var weather = require('weather-js');
const actions = {
send(request, response) {
const {sessionId, context, entities} = request;
const {text, quickreplies} = response;
console.log('sending...', JSON.stringify(response));
},
getForecast({context, entities}) {
var location = firstEntityValue(entities, 'location');
weather.find({search: location, degreeType: 'F'}, function(err, result) {
if(err){
context.missingLocation = true;
delete context.forecast;
}else{
var temperature = result[0].current.temperature;
context.forecast = temperature+ ' degrees in '+location; // we should call a weather API here
delete context.missingLocation;
fs.writeFile("weather.json",JSON.stringify(result));
}
return context;
});
},
};
答案 0 :(得分:0)
所以你想对天气api进行同步调用,但节点并不鼓励它。获得同步调用的方法是使用Promises。
参考 - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
以下是知识自己实施天气机器人的链接,你可以看到如何使用承诺
https://github.com/wit-ai/witty-weather/blob/master/src/createEngine.js