Node.js Lambda 函数返回“响应无效";从 REST 调用返回 Alexa Service Simulator [英] Node.js Lambda function returns "The response is invalid" back to Alexa Service Simulator from REST call

查看:18
本文介绍了Node.js Lambda 函数返回“响应无效";从 REST 调用返回 Alexa Service Simulator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 node.js Lambda 函数和 Alexa 之间对 API 进行 REST 调用时遇到问题.我正在使用 request 库通过帐户关联技能进行调用.我只为意图设置了一个示例话语,模拟器看到了这一点.

Having issues making a REST call to an API between a node.js Lambda function and Alexa. I'm using the request library to make the calls with an account linked skill. I've only set one sample utterance for the intent, and the simulator see this fine.

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

Also, the cloudwatch logs show a 200 response code from the api endpoint and any of the returned data from the API from console.logs to 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.

    }
};

我猜这与我要求 Alexa发出/告诉"的语音输出格式有关?

I'm guessing its something to do with the format of speechOutput that I'm asking Alexa to "emit/tell"?

推荐答案

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

No, it has nothing to do with the format of speechOutput. The issue is that when the callback of the request method is executed, the reference to this is lost. To solve that, keep a reference to this before you call request (e.g. assign this to a variable called 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.

    }
};

这篇关于Node.js Lambda 函数返回“响应无效";从 REST 调用返回 Alexa Service Simulator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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