Amazon AWS Lambda Alexa HTTP获取问题

我一直在使用Amazon Lambda和alexa技能工具包获取以下代码。 我已经花了无数个小时,并且无法工作。 我不断得到这个消息返回,并不能找出为什么http get失败。 “请稍后再试”。 它甚至不打印控制台消息。

var Alexa = require('alexa-sdk'); var http = require('http'); var APP_ID = "omitted"; var SKILL_NAME = 'omitted'; var options = { host: 'api.forismatic.com', path: '/api/1.0/?method=getQuote&lang=en&format=text', method: 'GET' }; exports.handler = function(event, context, callback) { var alexa = Alexa.handler(event, context); alexa.APP_ID = APP_ID; alexa.registerHandlers(handlers); alexa.execute(); }; var handlers = { 'LaunchRequest': function () { this.emit('Inspiration'); }, 'IntentRequest': function() { this.emit('Inspiration'); }, 'InspirationIntent': function () { this.emit('Inspiration'); }, 'Inspiration': function () { var speechOutput = ''; var text = ''; http.get(options, function(res) { console.error("Got response: " + res.statusCode); res.on("data", function(chunk) { console.error("BODY: " + chunk); text = chunk; }); }).on('error', function(e) { text = 'error' + e.message; console.error("Got error: " + e.message); }); if(text == ''){ speechOutput = "Please try again later"; } else{speechOutput = text;} this.emit(':tellWithCard', speechOutput, SKILL_NAME, text); }, 'AMAZON.HelpIntent': function () { var speechOutput = "You can ask Inspirational Quote for some advice."; var reprompt = "What would you like me to do?"; this.emit(':ask', speechOutput, reprompt); }, 'AMAZON.CancelIntent': function () { this.emit(':tell', 'Goodbye!'); }, 'AMAZON.StopIntent': function () { this.emit(':tell', 'Goodbye!'); }, 'Unhandled': function() { this.emit('AMAZON.HelpIntent'); } }; 

因为Java脚本是asynchronous的,所以这个代码:

 if(text == ''){ speechOutput = "Please try again later"; } else{speechOutput = text;} this.emit(':tellWithCard', speechOutput, SKILL_NAME, text); 

在调用API之前运行得到响应。

不幸的是,你不能将上面的代码移动到http.get块内,因为this.emit中的'this'将不再起作用,你会得到一个未定义的响应被发送回Alexa技巧。

我认为最简单的解决办法就是把http调用放到一个单独的函数中。 当调用该函数时,必须使用callback来避免相同的问题,在转移到下一个代码行之前,不等待来自http调用的响应。 所以传递一个函数的函数调用,并从那里发送你的回应。 NB – 为了这个工作,你必须在函数调用之外为这个“this”的值赋一个variables,并在函数调用中使用该variables而不是this。

示例如下:

 var Alexa = require('alexa-sdk'); var http = require('http'); var APP_ID = "omitted"; var SKILL_NAME = 'omitted'; var options = { host: 'api.forismatic.com', path: '/api/1.0/?method=getQuote&lang=en&format=text', method: 'GET' }; exports.handler = function(event, context, callback) { var alexa = Alexa.handler(event, context); alexa.APP_ID = APP_ID; alexa.registerHandlers(handlers); alexa.execute(); }; var handlers = { 'LaunchRequest': function () { this.emit('Inspiration'); }, 'IntentRequest': function() { this.emit('Inspiration'); }, 'InspirationIntent': function () { this.emit('Inspiration'); }, 'Inspiration': function () { var speechOutput = ''; var text = ''; var self = this; getQuote(options, function (quote){ if(quote == ''){ speechOutput = "Please try again later"; } else{speechOutput = quote;} self.emit(':tellWithCard', speechOutput, SKILL_NAME, text); } )}, 'AMAZON.HelpIntent': function () { var speechOutput = "You can ask Inspirational Quote for some advice."; var reprompt = "What would you like me to do?"; res(this.emit(':ask', speechOutput, reprompt)); }, 'AMAZON.CancelIntent': function () { this.emit(':tell', 'Goodbye!'); }, 'AMAZON.StopIntent': function () { this.emit(':tell', 'Goodbye!'); }, 'Unhandled': function() { this.emit('AMAZON.HelpIntent'); } }; function getQuote(options, callback){ http.get(options, function(res) { console.error("Got response: " + res.statusCode); res.on("data", function(chunk) { console.error("BODY: " + chunk); text = '' + chunk; return callback(text); }); }).on('error', function(e) { text = 'error' + e.message; console.error("Got error: " + e.message); }); }