[英]Getting started with wit.ai and Node.js
I am trying to use wit.ai quickstart example . 我正在尝试使用wit.ai 快速入门示例 。 The example works with the hardcoded values but when I use the third party weather API and try to give the response to the user it doesn't work. 该示例适用于硬编码的值,但是当我使用第三方天气API并尝试将响应提供给用户时,它将无法正常工作。
Working code: 工作代码:
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;
},
};
Now I wrote function getWeather({ context, entities }, location) to call the third party weather API and get the weather info for the user's given location. 现在,我编写了函数getWeather({context,Entitys},location)来调用第三方天气API,并获取用户给定位置的天气信息。
Nonworking code: 非工作代码:
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;
},
};
Also if I change getWeather() function slightly and move context.forecast = 'sunny in ' + location; 另外,如果我稍微改变getWeather()函数并移动context.forecast ='sunny in'+ location; delete context.missingLocation; 删除context.missingLocation; outside the callback fuction of request.get() call it will again work, but at this point I don't have weather info from 3rd party api: 在request.get()的回调函数之外进行调用将再次起作用,但是目前我没有来自第三方api的天气信息:
Working code: 工作代码:
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);
}
})
}
So how to make context.forecast = apiRes + location; 那么如何使context.forecast = apiRes + location; line work inside the callback of http call? 线工作里面的http调用回调? What I am doing wrong here? 我在这里做错了什么?
NOTE: Error response I get from wit.ai: 注意:我从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)
I am using npm package request to make http calls inside Node. 我正在使用npm包请求在Node内进行http调用。
Solved the issue by properly resolving the promise. 通过正确解决诺言解决了这一问题。 Here is the complete code: 这是完整的代码:
'use strict';
let Wit = null;
let interactive = null;
var getWeather = function ( location) {
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=myAppID';
return fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
}
})
.then(rsp => {
var res = rsp.json();
return res;
})
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
}
try {
// if running from repo
Wit = require('../').Wit;
interactive = require('../').interactive;
} catch (e) {
Wit = require('node-wit').Wit;
interactive = require('node-wit').interactive;
}
const accessToken = (() => {
if (process.argv.length !== 3) {
console.log('usage: node examples/quickstart.js <wit-access-token>');
process.exit(1);
}
return process.argv[2];
})();
// Quickstart example
// See https://wit.ai/ar7hur/quickstart
const firstEntityValue = (entities, entity) => {
const val = entities && entities[entity] &&
Array.isArray(entities[entity]) &&
entities[entity].length > 0 &&
entities[entity][0].value;
if (!val) {
return null;
}
return typeof val === 'object' ? val.value : val;
};
const actions = {
send(request, response) {
const {sessionId, context, entities} = request;
const {text, quickreplies} = response;
return new Promise(function (resolve, reject) {
console.log('sending...', JSON.stringify(response));
return resolve();
});
},
getForecast({context, entities}) {
var location = firstEntityValue(entities, 'location');
if (location) {
return new Promise(function (resolve, reject) {
return getWeather(location).then(weatherJson => {
context.forecast = weatherJson.weather[0].description + ' in the ' + location;
delete context.missingLocation;
return resolve(context);
})
});
} else {
context.missingLocation = true;
delete context.forecast;
return Promise.reject(context);
}
return context;
},
};
const client = new Wit({ accessToken, actions });
interactive(client);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.