Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Amazon AWS Lambda Alexa HTTP获取问题_Node.js_Amazon Web Services_Aws Lambda_Alexa_Alexa Skills Kit - Fatal编程技术网

Node.js Amazon AWS Lambda Alexa HTTP获取问题

Node.js Amazon AWS Lambda Alexa HTTP获取问题,node.js,amazon-web-services,aws-lambda,alexa,alexa-skills-kit,Node.js,Amazon Web Services,Aws Lambda,Alexa,Alexa Skills Kit,我一直对Amazon Lambda和alexa skills工具包的以下代码有疑问。我在这上面花了无数个小时,却无法让它工作。我一直收到返回的消息,不知道http get失败的原因。“请稍后再试”。它甚至没有打印控制台消息 var Alexa = require('alexa-sdk'); var http = require('http'); var APP_ID = "omitted"; var SKILL_NAME = 'omitted'; var options = {

我一直对Amazon Lambda和alexa skills工具包的以下代码有疑问。我在这上面花了无数个小时,却无法让它工作。我一直收到返回的消息,不知道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脚本是异步的,因此以下代码:

if(text == ''){
speechOutput = "Please try again later";
}
else{speechOutput = text;}
this.emit(':tellWithCard', speechOutput, SKILL_NAME, text);
在对API的调用获得响应之前正在运行

不幸的是,您不能仅仅将上述代码移动到http.get块中,因为this.emit中的“this”将不再起作用,您将得到一个未定义的响应,该响应将被发送回Alexa Skill

我认为最简单的解决方案是将http调用拉到一个单独的函数中。调用该函数时,您必须使用回调来避免lambda在移动到下一代码行之前不等待http调用的响应这一问题。因此,通过函数调用传递一个函数,并从那里发送响应NB-要使其起作用,必须在函数调用外部为“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);
});
}

谢谢成功了