Alexa技能在developer.amazon上工作但不在设备上?

时间:2017-03-28 20:58:46

标签: javascript aws-lambda alexa alexa-skills-kit alexa-skill

好的,这需要详细说明:     首先,当我在developer.amazon.com上询问意图“DeparmentLocation”时,它运行正常。当我使用真实设备时,它会调用'unhandledIntent'。     其次,在重新提示时工作,如下所示:     “嗨Alexa,问调用名称。”并且在回复未处理的消息之前不要说什么。然后我可以正常说出话语,它会回应。     第三个也是最后一个,使用“facilityLocation”意图。我不确定发生了什么或者有什么不同。     这是代码。     谢谢!

var doc = require('dynamodb-doc');
var db = new doc.DynamoDB();
var Alexa = require('alexa-sdk');

exports.handler = function(event, context) {
    var alexa = Alexa.handler(event, context);
var handlers = {
        'sessionStartedRequest' : function(){
            this.emit(':talk', "welcome.")
        },
        'AMAZON.StopIntent': function() {
            this.emit(':tell', "Goodbye!");
        },
        'AMAZON.CancelIntent': function() {
            this.emit(':tell', "Goodbye!");
        },
        'Unhandled': function() {
            this.emit(':ask', "I'm sorry. I didn't get that. Could you repeat it?", "Ask me a question.");
        },
        'SessionEndedRequest': function() {
            this.emit(":tell", "Goodbye!");
        },
'DepartmentLocationIntent': function () {
            var name = "";

            if (this.event.request.intent.slots.department.value) {
                name = this.event.request.intent.slots.department.value.toLowerCase().trim();
            }

            var key = {
                'name': name
            };
            var tableName = "Department";
            var params = {
                TableName: tableName,
                Key: key,
                ProjectionExpression: 'loc'
            };

            db.getItem(params, function (err, data) {
                //alexa.emit(":tell", name);
                if (err) {
                    console.log(err);
                    //alexa.emit(':tell', "Sorry! I did not catch that!");
                    alexa.emit(':tell', err);
                } else {
                    var loc = JSON.stringify(data.Item.loc);
                    alexa.emit(':tell', "The " + name + " department is in " + loc);
                }
            }, context.done);
        },

        'FacilityLocationIntent': function() {
            var name = "";
            if (this.event.request.intent.slots.facility.value) {
                name = this.event.request.intent.slots.facility.value.toLowerCase().trim();
            }
            var key = {
                'name': name
            };
            var tableName = "Facility";
            var params = {
                TableName: tableName,
                Key: key,
                ProjectionExpression: 'loc'
            };
            db.getItem(params, function(err, data) {
                if (err) {
                    console.log(err);
                    alexa.emit(':tell', "Sorry! This facility does not exist!");
                } else {
                    var response = JSON.stringify(data.Item.loc);
                    alexa.emit(':tell', name + " is located in " + response);
                }
            }, context.done);
}
};

    alexa.registerHandlers(handlers);
    alexa.execute();
};

`

1 个答案:

答案 0 :(得分:1)

bdeir

我不知道您是否仍在寻求解决方案,但我认为您的问题是代码的嵌套。 尝试将东西从'exports.handler'中分离出来,因为它更清晰,更容易使用,如AWS lambda指南所述。 所以试试这个:

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

然后在不同的处理程序下面

var handlers = {
        'sessionStartedRequest' : function(){
            this.emit(':talk', "welcome.")
        },
        'AMAZON.StopIntent': function() {
            this.emit(':tell', "Goodbye!");
        },
        'AMAZON.CancelIntent': function() {
            this.emit(':tell', "Goodbye!");
        },
        'Unhandled': function() {
            this.emit(':ask', "I'm sorry. I didn't get that. Could you repeat it?", "Ask me a question.");
        },
        'SessionEndedRequest': function() {
            this.emit(":tell", "Goodbye!");
        },
'DepartmentLocationIntent': function () {
            var name = "";

            if (this.event.request.intent.slots.department.value) {
                name = this.event.request.intent.slots.department.value.toLowerCase().trim();
            }

            var key = {
                'name': name
            };
            var tableName = "Department";
            var params = {
                TableName: tableName,
                Key: key,
                ProjectionExpression: 'loc'
            };

            db.getItem(params, function (err, data) {
                //alexa.emit(":tell", name);
                if (err) {
                    console.log(err);
                    //alexa.emit(':tell', "Sorry! I did not catch that!");
                    alexa.emit(':tell', err);
                } else {
                    var loc = JSON.stringify(data.Item.loc);
                    alexa.emit(':tell', "The " + name + " department is in " + loc);
                }
            }, context.done);
        },

        'FacilityLocationIntent': function() {
            var name = "";
            if (this.event.request.intent.slots.facility.value) {
                name = this.event.request.intent.slots.facility.value.toLowerCase().trim();
            }
            var key = {
                'name': name
            };
            var tableName = "Facility";
            var params = {
                TableName: tableName,
                Key: key,
                ProjectionExpression: 'loc'
            };
            db.getItem(params, function(err, data) {
                if (err) {
                    console.log(err);
                    alexa.emit(':tell', "Sorry! This facility does not exist!");
                } else {
                    var response = JSON.stringify(data.Item.loc);
                    alexa.emit(':tell', name + " is located in " + response);
                }
            }, context.done);
        }
    };

希望这能解决你的问题。