Είμαι προσπαθεί να χρησιμοποιήσει wit.ai παράδειγμα βήματα . Το παράδειγμα συνεργάζεται με τις hardcoded τιμές, αλλά όταν χρησιμοποιώ το τρίτο καιρό κόμμα API και να προσπαθήσει να δώσει την απάντηση στο χρήστη που δεν λειτουργεί.
Κωδικός εργασίας:
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;
},
};
Τώρα που έγραψα getWeather λειτουργία ({πλαίσιο, φορείς}, θέση) για να καλέσετε τον τρίτο καιρό API και να πάρει τις πληροφορίες καιρού για δεδομένη τοποθεσία του χρήστη.
Μη εργάσιμος Κωδικός:
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;
},
};
Επίσης, αν μπορώ να αλλάξω getWeather λειτουργία () λίγο και να προχωρήσουμε context.forecast = «ηλιοφάνεια» + θέση? διαγραφή context.missingLocation? έξω από το επανάκλησης fuction της request.get () κλήση θα λειτουργήσει και πάλι, αλλά αυτή τη στιγμή δεν έχω πληροφορίες καιρού από το 3ο μέρος api:
Κωδικός εργασίας:
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);
}
})
}
Έτσι, πώς να κάνει context.forecast = apiRes + θέση? γραμμή εργασιών στο εσωτερικό του επανάκλησης του κλήση http; Τι κάνω λάθος εδώ;
ΣΗΜΕΙΩΣΗ: Απόκριση σφάλματος παίρνω από wit.ai:
Σφάλμα: Ωχ, δεν ξέρω τι να κάνω.
at F:\..\node-wit\lib\wit.js:87:15 at process._tickCallback (internal/process/next_tick.js:103:7)
Είμαι χρησιμοποιώντας το πακέτο NPM αίτημα για την πραγματοποίηση κλήσεων http μέσα Κόμβος.













