Ik ben op zoek naar wit.ai gebruiken quickstart voorbeeld . Het voorbeeld werkt met de hardcoded waarden, maar toen ik de derde partij weer API gebruiken en proberen om de reactie op de gebruiker het niet werkt geven.
Werkende 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;
},
};
Nu heb ik de functie getWeather ({context, entiteiten}, locatie) aan de derde partij weer API bellen en krijgen informatie over het weer voor gezien de locatie van de gebruiker.
Niet-werkende 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;
},
};
Ook als I getWeather functie () verandert lichtjes en beweeg context.forecast = 'zonnig in' + locatie; verwijderen context.missingLocation; buiten de callback fuction van request.get () noemen het zal weer aan het werk, maar op dit punt heb ik niet weer info van 3rd party api:
Werkende 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);
}
})
}
Dus hoe je context.forecast = apiRes + locatie; lijnwerk in de callback van http call? Wat ik doe hier verkeerd?
OPMERKING: Foutreactie ik krijg van wit.ai:
Error: Oeps, ik weet niet wat te doen.
at F:\..\node-wit\lib\wit.js:87:15 at process._tickCallback (internal/process/next_tick.js:103:7)
Ik ben met behulp van NPM pakket verzoek om http oproepen binnen Node te maken.













