Node.js Lambda函数从REST调用返回Alexa Service Simulator的“响应无效”

时间:2017-03-09 19:45:38

标签: node.js aws-lambda alexa alexa-skills-kit alexa-skill

在node.js Lambda函数和Alexa之间对API进行REST调用时遇到问题。我正在使用request库通过帐户关联技能拨打电话。我只为意图设置了一个样本话语,模拟器看得很清楚。

此外,cloudwatch日志显示来自api端点的200响应代码以及API中从console.logs到CW的任何返回数据。

'use strict';
var http = require('http');
var request = require('request');
var Alexa = require('alexa-sdk');
var APP_ID = "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.appId = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

var handlers = {
   'LaunchRequest': function () {
        this.emit(':tell', 'Hi!');
   },

   'ApiWelcomeIntent': function () {
        request('https://some.web/api', function (error, response, body) {
            if (!error && response.statusCode == 200) {
            // from within the callback, write data to response, essentially returning it.
                var speechOutput = JSON.stringify(body);
                console.log(body + " :Raw output?");
                console.log(speechOutput + ' :JSON stringified');
                console.log(response.statusCode);
                this.emit(':tell', speechOutput);
            } else {
                console.log(error + ' : ' + response.statusCode);
                this.emit(':tell', 'There was an error');
            }
        });
    },

    'AMAZON.HelpIntent': function () {} //.........And other built in intents.

    }
};

我猜它与speechOutput的格式有关,我要求Alexa“发出/告诉”?

2 个答案:

答案 0 :(得分:4)

不,它与speechOutput的格式无关。问题是,当执行request方法的回调时,对this的引用将丢失。要解决此问题,请在致电this之前保留对request的引用(例如,将this分配给名为self的变量):

'use strict';
var http = require('http');
var request = require('request');
var Alexa = require('alexa-sdk');
var APP_ID = "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.appId = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

var handlers = {
   'LaunchRequest': function () {
        this.emit(':tell', 'Hi!');
   },

   'ApiWelcomeIntent': function () {
        self = this

        request('https://some.web/api', function (error, response, body) {
            if (!error && response.statusCode == 200) {
            // from within the callback, write data to response, essentially returning it.
                var speechOutput = JSON.stringify(body);
                console.log(body + " :Raw output?");
                console.log(speechOutput + ' :JSON stringified');
                console.log(response.statusCode);
                self.emit(':tell', speechOutput); // USE SELF HERE
            } else {
                console.log(error + ' : ' + response.statusCode);
                self.emit(':tell', 'There was an error'); // AND HERE AS WELL
            }
        });
    },

    'AMAZON.HelpIntent': function () {} //.........And other built in intents.

    }
};

答案 1 :(得分:0)

您面临的问题仅是因为NodeJS的异步调用行为,而与请求调用无关。您可以使用Promise或使用method的回调来解决此问题。以下是摘要。

'use strict';
var http = require('http');
var request = require('request');
var Alexa = require('alexa-sdk');
var APP_ID = "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX";

exports.handler = function(event, context, callback) {
    var alexa = Alexa.handler(event, context);
    alexa.appId = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

var handlers = {
   'LaunchRequest': function () {
        this.emit(':tell', 'Hi!');
   },

   'ApiWelcomeIntent': function () {
        requestAPI( (message) => {
            this.emit(':tell', message);
        });
    },
    'AMAZON.HelpIntent': function () {} //.........And other built in intents.

    }
};

function requestAPI(callback) {
    request('https://some.web/api', function (error, response, body) {
        if (!error && response.statusCode == 200) {
        // from within the callback, write data to response, essentially returning it.
            var speechOutput = JSON.stringify(body);
            console.log(body + " :Raw output?");
            console.log(speechOutput + ' :JSON stringified');
            console.log(response.statusCode);
            callback(speechOutput);
        } else {
            console.log(error + ' : ' + response.statusCode);
            callback('There was an error');
        }
    });
}