Ben wit.ai kullanmaya çalışıyorum hızlı başlangıç örneği . Örnek kodlanmış değerlerle çalışır ama üçüncü parti hava API kullanmak ve çalıştığımda çalışmıyor kullanıcıya yanıt vermek için.
kodunu Çalışma:
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;
},
};
Şimdi üçüncü şahıs hava apı'sini ve kullanıcının belirli bir yerde hava bilgi almak için fonksiyon GetWeather ({bağlam, kişiler}, konum) yazdı.
Çalışma dışı kodu:
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;
},
};
Ayrıca Biraz GetWeather () işlevi değiştirmek ve hareket halinde + konum 'güneşli' context.forecast =; context.missingLocation silme; request.get ait geri arama fuction () tekrar çalışacaktır diyoruz, ama bu noktada ben 3. parti API gelen hava bilgi yok dışarıda:
kodunu Çalışma:
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);
}
})
}
Yani nasıl context.forecast = apiRes + Yer; http aramanın callback'inde iç hat çalışır mı? Yanlış burada ne işim var?
NOT: Hata yanıtı ben wit.ai aldığım:
Hata: Bir dakika, ne yapacağımı bilmiyorum.
at F:\..\node-wit\lib\wit.js:87:15 at process._tickCallback (internal/process/next_tick.js:103:7)
Ben npm paketi kullanıyorum isteği Düğüm içindeki http aramaları yapmak için.













