问题描述
我有一个包含请求调用的模块,但它似乎没有被执行.
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);
}
}
我从未在 Lambda 控制台或其他任何地方的 console.log 中看到 google 正文出现.我已经尝试了其他调用(如 API 发布到我的应用程序服务器 API),但也没有看到该服务器上出现该调用.
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.
在 Amazon Lambda测试器"中,我得到了有效的响应.在 Alexa测试器"中,我得到了Recorded Test"的响应.在 Echo(通过 Alexa)上,我从设备收到Recorded Test"响应.所以这项技能似乎很有效.只是请求"操作(在本例中,只是拉取 google.com)失败了.
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.
谢谢!!
更新:我至少能够完成调用,但可能不是最干净的方法.
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;
}
);
}
}
推荐答案
您的(原始)代码不起作用,因为您正在调用this.emit(':tell', SpeechOutput);
Your (original) code is not working because you are callingthis.emit(':tell', speechOutput);
就在之后request("http://www.google.com",
:tell
函数将调用 lambda 回调并终止 lambda 函数的执行.
The :tell
function will call the lambda callback and terminate the execution of the lambda function.
您自己找到了解决方案:等待 request
回调被执行并在那时发出 :tell
事件.
You found the solution yourself : wait for the request
callback to be executed and issue the :tell
event at that time.
在
查看alexa-skills-kit-sdk-for-nodejs代码https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/blob/master/lib/response.js#L6
和
https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/blob/master/lib/response.js#L101
您可以在 http://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html
这篇关于使用 Alexa 技能的 Node JS 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!