Node JS callbacks with Alexa skill

夙愿已清 提交于 2019-12-07 15:27:48

问题


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:


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



来源:https://stackoverflow.com/questions/40098494/node-js-callbacks-with-alexa-skill

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!