简体   繁体   English

Amazon AWS Lambda Alexa HTTP获取问题

[英]Amazon AWS Lambda Alexa HTTP Get issue

I keep getting an issue with the following code with Amazon Lambda and the alexa skills kit. 我继续使用Amazon Lambda和alexa技能工具包获得以下代码的问题。 I have spent countless hours on this and cannot get it to work. 我花了无数个小时在这上面,无法让它发挥作用。 I keep getting this message returned and can't figure out why the http get is failing. 我一直收到这条消息,无法弄清楚为什么http get失败了。 "Please try again later". “请稍后再试”。 It isn't even printing the console messages. 它甚至不打印控制台消息。

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');
}
};

Because java script is asynchronous, this code : 因为java脚本是异步的,所以这段代码:

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

is running before the call out to the API gets a response. 在调用API获得响应之前运行。

Unfortunately, you can't just move the above code to inside the http.get block, as the 'this' in 'this.emit' will no longer work and you'll get an undefined response being sent back to the Alexa Skill. 不幸的是,你不能只将上面的代码移到http.get块中,因为'this.emit'中的'this'将不再起作用,你将得到一个未定义的响应被发送回Alexa Skill。

I think the neatest solution would be to pull the http call out into a separate function. 我认为最好的解决方案是将http调用拉出到一个单独的函数中。 When calling that function, you will have to use a callback to avoid the same issue of the lambda not waiting for a response from the http call before moving to the next code line. 调用该函数时,您必须使用回调来避免同一问题的lambda在转移到下一个代码行之前没有等待来自http调用的响应。 So pass a function with the function call and send your response from there. 因此,通过函数调用传递函数并从那里发送您的响应。 NB - for this to work you have to assign a variable to the value of 'this' outside the function call and use that variable instead of 'this', inside the function call. 注意 - 为此,你必须在函数调用之外为变量指定一个变量,并在函数调用中使用该变量而不是'this'。

Example below: 示例如下:

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);
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM