Jeg prøver å bruke wit.ai Quickstart eksempel . Eksempelet fungerer med hardkodede verdier, men når jeg bruker tredjeparts været API og prøve å gi svar til brukeren det fungerer ikke.
Arbeide kode:
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;
},
};
Nå skrev jeg funksjon getWeather ({kontekst, enheter}, plassering) å kalle været API tredjepart og få vær info for brukerens gitt sted.
Nonworking kode:
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;
},
};
Også hvis jeg endrer funksjon getWeather () lett og bevege context.forecast = 'sol' + plassering; slette context.missingLocation; utenfor tilbakeringing fuction av request.get () kaller det vil igjen fungere, men på dette punktet jeg ikke har vær info fra tredje part api:
Arbeide kode:
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);
}
})
}
Så hvordan å lage context.forecast = apiRes + plassering; linjen arbeid inne tilbakeringing http samtale? Hva jeg gjør galt her?
MERK: Feil responsen jeg får fra wit.ai:
Feil: Oops, jeg vet ikke hva jeg skal gjøre.
at F:\..\node-wit\lib\wit.js:87:15 at process._tickCallback (internal/process/next_tick.js:103:7)
Jeg bruker NPM pakken forespørsel for å gjøre http samtaler inne Node.













