Amazon AWS Lambda Alexa HTTP获取问题 [英] Amazon AWS Lambda Alexa HTTP Get issue

查看:113
本文介绍了Amazon AWS Lambda Alexa HTTP获取问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Amazon Lambda和alexa技能套件的以下代码始终遇到问题。我为此花费了无数的时间,无法使它正常工作。我一直收到此消息,但无法弄清楚为什么http获取失败。 请稍后再试。甚至没有打印控制台消息。

I keep getting an issue with the following code with Amazon Lambda and the alexa skills kit. 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. "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');
}
};


推荐答案

由于Java脚本是异步的,因此此代码:

Because java script is asynchronous, this code :

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

在对API的调用获得响应之前正在运行。

is running before the call out to the API gets a response.

不幸的是,您不能只将上面的代码移到http.get块中,因为 this.emit中的 this将不再起作用,并且您将得到未定义的响应发送回Alexa技能。

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调用拉出一个单独的函数。调用该函数时,您将必须使用回调来避免相同的问题,即lambda在移至下一行代码之前不等待http调用的响应。因此,通过函数调用传递一个函数,然后从那里发送您的响应。 NB -为此,您必须在函数调用之外将变量分配给'this'的值,并在函数调用内部使用该变量而不是'this'。

I think the neatest solution would be to pull the http call out into a separate function. 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. 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.

下面的示例:

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

这篇关于Amazon AWS Lambda Alexa HTTP获取问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆