मैं wit.ai उपयोग करने के लिए कोशिश कर रहा हूँ त्वरित प्रारंभ उदाहरण । उदाहरण हार्डकोडेड मूल्यों के साथ काम करता है लेकिन जब मैं तीसरे पक्ष के मौसम एपीआई का उपयोग और कोशिश उपयोगकर्ता यह काम नहीं करता के जवाब देने के लिए।
कार्य कोड:
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 ({संदर्भ, संस्थाओं}, स्थान) ने लिखा है तीसरे पक्ष के मौसम एपीआई फोन और उपयोगकर्ता के दिए गए स्थान के लिए मौसम की जानकारी प्राप्त करने के लिए।
Nonworking कोड:
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 हटाना; request.get की कॉलबैक fuction () इसे फिर से काम करेंगे कहते हैं, लेकिन इस बिंदु पर मैं 3 पार्टी एपीआई से मौसम की जानकारी नहीं है बाहर:
कार्य कोड:
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 कॉल करने के लिए।













