【问题标题】:Amazon AWS Lambda Alexa HTTP Get issue亚马逊 AWS Lambda Alexa HTTP 获取问题
【发布时间】:2017-07-10 07:59:52
【问题描述】:

我一直在使用 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');
}
};

【问题讨论】:

    标签: node.js amazon-web-services aws-lambda alexa alexa-skills-kit


    【解决方案1】:

    因为java脚本是异步的,所以这段代码:

    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 调用拉到一个单独的函数中。调用该函数时,您必须使用回调来避免 lambda 在移动到下一个代码行之前不等待来自 http 调用的响应的相同问题。因此,通过函数调用传递一个函数并从那里发送您的响应。 注意 - 要使其工作,您必须在函数调用外部为“this”的值分配一个变量,并在函数调用内部使用该变量而不是“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);
    });
    }
    

    【讨论】:

    • 谢谢。做到了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多