wit.ai quickstart exampleを使用しようとしています。この例ではハードコードされた値を使用していますが、サードパーティの天気APIを使用してユーザーに返答しようとすると、動作しません。コードの作業wit.aiとNode.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');
if (location) {
context.forecast = 'sunny in ' + location; // we should call a weather API here
delete context.missingLocation;
} else {
context.missingLocation = true;
delete context.forecast;
}
return context;
},
};
は、今私は、サードパーティの天気APIを呼び出して、ユーザーの指定した場所の天気情報を取得する機能getWeather({コンテキスト、エンティティ}、場所)を書きました。
非稼働コード:また
var getWeather = function ({ context, entities }, location) {
console.log('Entities: ',entities)
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=myAppID';
request.get(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(typeof body)
var weatherObj = JSON.parse(body);
console.log('weather api res: ', weatherObj.weather[0].description);
context.forecast = weatherObj.weather[0].description + ' ' + location; // we should call a weather API here
delete context.missingLocation;
}
})
}
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');
if (location) {
//Call a function which calls the third party weather API and then handles the response message.
getWeather({ context, entities }, location);
} else {
context.missingLocation = true;
delete context.forecast;
}
return context;
},
};
IはgetWeather()関数をわずかに変更して= +位置 'に晴れ' context.forecastを移動する場合。 context.missingLocationを削除します。 request.getのコールバックのfuction(外)それが再び動作しますが、この時点で私は、サードパーティのAPIから気象情報を持っていない呼び出し:
の作業コード:
var getWeather = function ({ context, entities }, location) {
//Keeping context.forecast outside the request.get() callback function is useless as we yet to get the weather info from the API
context.forecast = 'sunny in ' + location;
delete context.missingLocation;
console.log('Entities: ',entities)
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=myAppID';
request.get(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(typeof body)
var weatherObj = JSON.parse(body);
console.log('weather api res: ', weatherObj.weather[0].description);
}
})
}
それでは、どのようにcontext.forecast = apiRes + location; httpコールのコールバック内で回線が動作していますか?私はここで間違っていますか?
注:私はwit.aiから取得 エラー応答:
Error: Oops, I don't know what to do.
at F:\..\node-wit\lib\wit.js:87:15 at process._tickCallback (internal/process/next_tick.js:103:7)
私はノード内のHTTP呼び出しを行うために、NPMパッケージrequestを使用しています。