Node.js   发布时间:2022-04-24  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了node.js – 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',reprompt);
},'AMAZON.CancelIntent': function () {
    this.emit(':tell','Goodbye!');
},'AMAZON.StopIntent': function () {
    this.emit(':tell','Unhandled': function() {
    this.emit('AMAZON.HelpIntent');
}
};

解决方法

为Java脚本是异步的,所以这段代码

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

调用API获得响应之前运行.

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

我认为最好的解决方案是将http调用拉出到一个单独的函数中.调用函数时,您必须使用回调来避免同一问题的lambda在转移到下一个代码行之前没有等待来自http调用的响应.因此,通过函数调用传递函数并从那里发送您的响应.注意 – 为此,你必须在函数调用之外为变量指定一个变量,并在函数调用中使用该变量而不是’this’.

示例如下:

var Alexa = require('alexa-sdk');
var http = require('http');
var APP_ID = "omitted";     
var SKILL_NAME = 'omitted';

var options = {
    host: 'api.forismatic.com',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',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',reprompt));
    },'AMAZON.CancelIntent': function () {
        this.emit(':tell','Goodbye!');
    },'AMAZON.StopIntent': function () {
        this.emit(':tell','Unhandled': function() {
        this.emit('AMAZON.HelpIntent');
    }
};

function getQuote(options,callBACk){
    http.get(options,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);
});
}

大佬总结

以上是大佬教程为你收集整理的node.js – Amazon AWS Lambda Alexa HTTP获取问题全部内容,希望文章能够帮你解决node.js – Amazon AWS Lambda Alexa HTTP获取问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。