尽管CloudWatch中没有记录任何错误,Alexa Skill - 收到“响应无效”

时间:2018-02-06 22:44:07

标签: javascript node.js amazon-web-services https aws-lambda

我正在开发一种Alexa技能,可以为大学项目返回所需食物的卡路里量,为此我一直在使用Nutritionix api,它可以返回不同食物的卡路里量基于搜索词的类型。在我的代码中,我正在搜索用户通过API请求的术语,并返回返回的食物项数组中第一项的卡路里数。但是,当我在Amazon Developer控制台的测试部分中测试该功能时,我收到“响应无效”的响应。 CloudWatch中没有记录任何错误,我实际上使用了console.log来确保在我的代码中正确创建语音输出(我可以在CloudWatch中看到它)。

Cloudwatch中出现的唯一消息是“警告:未设置应用程序ID”,这应该不是问题,“American Colossal有540卡路里”。这与下图所示的请求有关。

Get Response is invalid issue as you can see

这是我的代码,出于安全考虑,我已删除了我的app-id和app-key,但两者都正确,因为https请求运行正常,如果有人能看到导致问题的原因我会很感激。据我所知,问题必须发生在“this.emit(”:tell“,speechOutput);”但我不确定为什么。感谢。

"use strict";

//Variables
var Alexa = require("alexa-sdk");
var SKILL_NAME = "Nutrition Info";
var APP_ID = "";
var https = require("https");

//Setup
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(":ask", 'Welcome to the Nutrition Information app, you can ask me how many calories are in a large variety of different foods', 'Ask me how many calories there are in different food stuffs');
    },
    'GetCaloriesIntent': function() {
        this.emit('GetCalories');
    },
    'GetCalories' : function() {
        var foodString = this.event.request.intent.slots.FoodStuff.value;
        var speechOutput = '';
        var options = {
          host: 'trackapi.nutritionix.com',
          port: 443,
          path: '/v2/search/instant?query=' + foodString,
          method: 'GET',
          headers: {
              "x-app-id": [REMOVED THIS FOR SECURITY PURPOSES],
              "x-app-key": [REMOVED THIS FOR SECURITY PURPOSES]
              }
        };


        var req = https.request(options, function(res) {

          var data = ""
          res.setEncoding('UTF-8');
          res.on('data', function (chunk) { data += chunk })
          res.on('end', function() {
            var result = JSON.parse(data);
            var speechOutput = result.branded[0].food_name + " has " + result.branded[0].nf_calories + " calories.";
            console.log(speechOutput);
            this.emit(":tell",speechOutput);
          })
        });

        req.on("error",function(err) {this.emit(":tell","Sorry, there was an issue processing your request.")});

        req.end();

    },
    'AMAZON.HelpIntent' : function() {
        var speechOutput = "You can get nutrition information by asking how many calories there are in a food stuff";
        var reprompt = "What can I help you with?";
        this.emit(":ask", speechOutput, reprompt);
    },
    'AMAZON.StopIntent' : function() {
        this.emit(":tell","Goodbye!");
    },
    'AMAZON.CancelIntent' : function() {
        this.emit(":tell","Goodbye!");
    }
}

1 个答案:

答案 0 :(得分:0)

典型的,我发布后几乎立即找到了解决方案! 对于有类似问题的人。这是因为API调用中引用的“this”不正确,这就是我解决它的方法。

var foodString = this.event.request.intent.slots.FoodStuff.value;
        var speechOutput = '';
        const self = this;
        var options = {
          host: 'trackapi.nutritionix.com',
          port: 443,
          path: '/v2/search/instant?query=' + foodString,
          method: 'GET',
          headers: {
              "x-app-id": [Removed for security purposes],
              "x-app-key": [Removed for security purposes]
              }
        };


        var req = https.request(options, function(res) {

          var data = ""
          res.setEncoding('UTF-8');
          res.on('data', function (chunk) { data += chunk })
          res.on('end', function() {
            var result = JSON.parse(data);
            var speechOutput = result.branded[0].food_name + " has " + result.branded[0].nf_calories + " calories.";
            console.log(speechOutput);
            self.emit(":tell",speechOutput);
          })
        });

        req.on("error",function(err) {self.emit(":tell","Sorry, there was an issue processing your request")});

        req.end();

在函数开头使用const作为“this”可以防止问题发生。