3
votes

I have a module that includes a request call, which doesn't appear to be getting executed.

var request = require('request');
var Alexa = require('alexa-sdk');
var APP_ID = <my alexa app ID>;

var self = module.exports = {
   handler : function (event, context, callback) {
            var alexa = Alexa.handler(event, context);
            alexa.appId = APP_ID;
            alexa.registerHandlers(self);
            alexa.execute();
    },
    "TestIntent": function () {
        var speechOutput = "Recorded Test";
        request("http://www.google.com", 
            function(error, response,body)   
             { 
                 return console.log(body);
             }
        );
        this.emit(':tell', speechOutput);
    }
}

I never see the google body show up in my console.log on Lambda console or anywhere else. I've tried other calls (like API posts to my application server API) and do not see that show up on that server either.

Seems like the process is closing before the request callback completes.

In the Amazon Lambda "tester" I get a valid response. In the Alexa "tester" I get back the response of "Recorded Test". And on the Echo (via Alexa), I get back "Recorded Test" response from the device. So the skill seems to be working great. It's just the "request" action (in this case, just pulling google.com) that is failing.

Thanks!!

UPDATE: I was at least able to get the call to complete, but probably not the cleanest way.

var request = require('request');
var Alexa = require('alexa-sdk');
var APP_ID = <my alexa app ID>;

var self = module.exports = {
   handler : function (event, context, callback) {
            var alexa = Alexa.handler(event, context);
            alexa.appId = APP_ID;
            alexa.registerHandlers(self);
            alexa.execute();
    },
    "TestIntent": function () {
        var that = this; 
        var speechOutput = "Recorded Test";
        request("http://www.google.com", 
            function(error, response,body)   
             { 
                 console.log(body);
                 that.emit(':tell', speechOutput);
                 return;
             }
        );
    }
}
1
Are you using the current version of lambda (4.3)? I wonder whether the old version might kill the process when you invoke the callback. I know that the test harness I use (node-lamdba) does this and I think its because the behavior was designed for the old lambda and hasn't been updated.Tom
Also, maybe the call fails and body is empty. I would stick an explicit log message in there just to be sure it really isn't getting logged. And, unrelated comment, but you aren't supposed to set alexa.appId to your APP_ID, you are supposed to verify that they match.Tom
Thanks @Tom, I can get an explicit "I got here" message inside the request function to show up if I make it through a bug before executing the "this.emit". So if the TestIntent function doesn't complete, the callback has time to run and report. So I need to somehow get the this.emit to wait for the callback to finishprobbins222
I don't agree with your last sentence: you should do emit as early as possible since that is when the response goes to user.Tom
Fair, I need the close of the service to wait for this function to finish, was thinking putting the emit in app the function could do that, but I guess there could be other ways?probbins222

1 Answers

3
votes

Your (original) code is not working because you are calling this.emit(':tell', speechOutput);

just right after request("http://www.google.com",

The :tell function will call the lambda callback and terminate the execution of the lambda function.

You found the solution yourself : wait for the request callback to be executed and issue the :tell event at that time.

See alexa-skills-kit-sdk-for-nodejs code at
https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/blob/master/lib/response.js#L6
and
https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/blob/master/lib/response.js#L101

You can learn more about Lambda programming model at http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html