Node.js Lambda 函数从 REST 调用将“响应无效”返回给 Alexa 服务模拟器
Posted
技术标签:
【中文标题】Node.js Lambda 函数从 REST 调用将“响应无效”返回给 Alexa 服务模拟器【英文标题】:Node.js Lambda function returns "The response is invalid" back to Alexa Service Simulator from REST call 【发布时间】:2017-07-30 22:57:15 【问题描述】:在 node.js Lambda 函数和 Alexa 之间对 API 进行 REST 调用时遇到问题。我正在使用request 库通过帐户链接技能进行呼叫。我只为意图设置了一个示例话语,模拟器可以看到这一点。
此外,cloudwatch 日志显示来自 api 端点的 200 响应代码以及从 console.logs 到 CW 的 API 返回的任何数据。
'use strict';
var http = require('http');
var request = require('request');
var Alexa = require('alexa-sdk');
var APP_ID = "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";
exports.handler = function(event, context, callback)
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
;
var handlers =
'LaunchRequest': function ()
this.emit(':tell', 'Hi!');
,
'ApiWelcomeIntent': function ()
request('https://some.web/api', function (error, response, body)
if (!error && response.statusCode == 200)
// from within the callback, write data to response, essentially returning it.
var speechOutput = JSON.stringify(body);
console.log(body + " :Raw output?");
console.log(speechOutput + ' :JSON stringified');
console.log(response.statusCode);
this.emit(':tell', speechOutput);
else
console.log(error + ' : ' + response.statusCode);
this.emit(':tell', 'There was an error');
);
,
'AMAZON.HelpIntent': function () //.........And other built in intents.
;
我猜它与我要求 Alexa“发出/告诉”的 SpeechOutput 格式有关?
【问题讨论】:
【参考方案1】:不,它与speechOutput的格式无关。问题是在执行request
方法的回调时,对this
的引用丢失了。要解决这个问题,请在调用 request
之前保留对 this
的引用(例如,将 this
分配给名为 self
的变量):
'use strict';
var http = require('http');
var request = require('request');
var Alexa = require('alexa-sdk');
var APP_ID = "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";
exports.handler = function(event, context, callback)
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
;
var handlers =
'LaunchRequest': function ()
this.emit(':tell', 'Hi!');
,
'ApiWelcomeIntent': function ()
self = this
request('https://some.web/api', function (error, response, body)
if (!error && response.statusCode == 200)
// from within the callback, write data to response, essentially returning it.
var speechOutput = JSON.stringify(body);
console.log(body + " :Raw output?");
console.log(speechOutput + ' :JSON stringified');
console.log(response.statusCode);
self.emit(':tell', speechOutput); // USE SELF HERE
else
console.log(error + ' : ' + response.statusCode);
self.emit(':tell', 'There was an error'); // AND HERE AS WELL
);
,
'AMAZON.HelpIntent': function () //.........And other built in intents.
;
【讨论】:
嘿@renansdias 你的回答听起来是正确的,但我在使用自我后仍然面临这个问题。你能在***.com/questions/45613412/… 上查看我的代码吗 @devutkarsh 你仍然面临这个问题吗? 对不起@devutkarsh 完全错过了这个。是的,您是否仍然面临这个问题? @renansdias - 现在已解决。谢谢!我也接受了你的回答:)【参考方案2】:您仅因为 NodeJS 的异步调用行为而面临问题,而与请求调用无关。您可以通过使用 Promise 或使用方法回调来解决此问题。下面是sn-p。
'use strict';
var http = require('http');
var request = require('request');
var Alexa = require('alexa-sdk');
var APP_ID = "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";
exports.handler = function(event, context, callback)
var alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
;
var handlers =
'LaunchRequest': function ()
this.emit(':tell', 'Hi!');
,
'ApiWelcomeIntent': function ()
requestAPI( (message) =>
this.emit(':tell', message);
);
,
'AMAZON.HelpIntent': function () //.........And other built in intents.
;
function requestAPI(callback)
request('https://some.web/api', function (error, response, body)
if (!error && response.statusCode == 200)
// from within the callback, write data to response, essentially returning it.
var speechOutput = JSON.stringify(body);
console.log(body + " :Raw output?");
console.log(speechOutput + ' :JSON stringified');
console.log(response.statusCode);
callback(speechOutput);
else
console.log(error + ' : ' + response.statusCode);
callback('There was an error');
);
【讨论】:
以上是关于Node.js Lambda 函数从 REST 调用将“响应无效”返回给 Alexa 服务模拟器的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法从 node.js 同步调用 AWS Lambda?
无法在 Lambda 函数(Node JS)中下载 S3 视频
使用带有 Node.js 的 AWS Lambda 函数从 S3 存储桶中提取 zip 文件并上传到另一个存储桶
Lambda@Edge 函数的 Node.Js 301 url-rewrite 问题