alexa-app
alexa-app copied to clipboard
Asynchronous Handlers Example, not able to call HTTP request
I am trying to call http.get call inside intent but every time, I get below response:
{
"version": "1.0",
"response": {
"directives": [],
"shouldEndSession": true
},
"sessionAttributes": {}
}
Do you have full example code or a failing test case you can share?
You may want to confirm that your intent handler returns the promise that is doing the asynchronous work. If you’re using a callback-based library, you’ll need to wrap it in a promise.
var getProducts=function(){ return new Promise(function(resolve,reject){ var options = { url: ', method: 'GET', }; request(options, function (error, response, body) { if (error){ reject(error); } if (!error && response.statusCode == 200) { console.log(body); resolve(JSON.parse(body)); } }); }); }; I am using above logic, please correct if I'm doing wrong
That function looks reasonable, but that doesn't actually show how it's being used in context of this library. Happy to take a brief look over some code, but I can only really be helpful if you can provide an actual runnable project or an executable failing test.
Me too facing the same issue. While I am trying to get response from other API, it is giving me
{ "version": "1.0", "response": { "directives": [], "shouldEndSession": true }, "sessionAttributes": {} }
response, I think it is a bug. for more details please see the sample below,
app.intent("CoinIntent", { "slots": { "origin": "ORIGIN" }, "utterances": [ "get the value of {origin}" ] }, function (request, response) { var slot = request.slots['origin']; var origin = slot.value; requestObj('https://httpbin.org/get', function (error, responseObj, body) { console.log('after success :: '); response.say(responseObj.origin); }); });
@raviteja2503 try
return requestObj(...)
assuming requestObj() actually returns a promise. Though, since it takes a callback, it might not. In which case you need to write it in a promise.