Estoy tratando de utilizar wit.ai ejemplo de inicio rápido . El ejemplo funciona con los valores codificados pero cuando se utiliza la tercera API de tiempo partido y tratar de dar la respuesta al usuario que no funciona.
el código de trabajo:
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;
},
};
Ahora escribí función GetWeather ({} contexto, entidades, ubicación) para llamar a la API de tiempo de terceros y obtener la información del tiempo en lugar determinado del usuario.
código no laborable:
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;
},
};
También si cambio de función GetWeather () ligeramente y se mueven context.forecast = 'sol en' + ubicación; eliminar context.missingLocation; fuera del fuction de devolución de llamada de request.GET () llaman a esto volverá a trabajar, pero en este momento no tengo información de tiempo desde el 3 api partido:
el código de trabajo:
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);
}
})
}
Entonces, ¿cómo hacer context.forecast = apiRes + ubicación; línea de trabajo dentro de la devolución de llamada de la llamada http? Lo que estoy haciendo mal aquí?
NOTA: Reacción de error que recibo de wit.ai:
Error: Vaya, no sé qué hacer.
at F:\..\node-wit\lib\wit.js:87:15 at process._tickCallback (internal/process/next_tick.js:103:7)
Estoy utilizando el paquete NPM solicitud para hacer llamadas HTTP dentro del nodo.













