Sto cercando di utilizzare wit.ai esempio QuickStart . L'esempio funziona con i valori hardcoded, ma quando uso il terzo tempo API e cercare di dare la risposta per l'utente che non funziona.
Codice di lavoro:
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;
},
};
Ora ho scritto la funzione GetWeather ({contesto}, enti, posizione) per chiamare il tempo API di terze parti e ottenere le informazioni meteo per data la posizione dell'utente.
Codice non lavorativo:
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;
},
};
Anche se cambio la funzione GetWeather () leggermente e spostare context.forecast = 'di sole a' + posizione; eliminare context.missingLocation; al di fuori del fuction callback di request.get () chiamano tornerà a lavorare, ma a questo punto non ho informazioni meteo dal 3 api parti:
Codice di lavoro:
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);
}
})
}
Quindi, come fare context.forecast = apiRes + posizione; linea di lavoro all'interno della callback di chiamata http? Quello che sto facendo male qui?
NOTA: la risposta di errore ottengo da wit.ai:
Errore: Oops, non so cosa fare.
at F:\..\node-wit\lib\wit.js:87:15 at process._tickCallback (internal/process/next_tick.js:103:7)
Sto usando NPM pacchetto di richiesta per effettuare chiamate HTTP all'interno del nodo.













