Próbálom használni wit.ai gyorstalpaló például . A példa működik a kódolt értékek, de ha használom a harmadik fél időjárási API és próbálja meg, hogy a válasz, hogy a felhasználó nem működik.
Munka kód:
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;
},
};
Most írtam funkció getWeather ({összefüggésben szervezetek}, hely), hogy hívja a harmadik felet időjárási API és kap az időjárás info a felhasználó megadott helyen.
Nonworking kód:
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;
},
};
Akkor is, ha azt változtatni getWeather () függvény enyhén és mozgassa context.forecast = „napsütéses” + helyen; törölni context.missingLocation; kívül a visszahívás fuction a request.get () hívás akkor ismét működik, de ezen a ponton nincs időjárás info a 3rd party api:
Munka kód:
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);
}
})
}
Tehát hogyan lehet context.forecast = apiRes + helyét; line munkát belül a visszahívás a http hívást? Mit csinálok rosszul van?
Megjegyzés: Hiba választ kapok wit.ai:
Hiba: Hoppá, nem tudom, mit kell tenni.
at F:\..\node-wit\lib\wit.js:87:15 at process._tickCallback (internal/process/next_tick.js:103:7)
Én a NPM csomag kérés , hogy http hívások belül csomópont.













