alexa-app icon indicating copy to clipboard operation
alexa-app copied to clipboard

Asynchronous Handlers Example, not able to call HTTP request

Open dk67604 opened this issue 8 years ago • 5 comments

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": {}
}

dk67604 avatar Nov 06 '17 02:11 dk67604

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.

lazerwalker avatar Nov 06 '17 04:11 lazerwalker

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

dk67604 avatar Nov 06 '17 05:11 dk67604

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.

lazerwalker avatar Nov 06 '17 23:11 lazerwalker

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 avatar Mar 11 '18 19:03 raviteja2503

@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.

matt-kruse avatar Mar 11 '18 20:03 matt-kruse