繁体   English   中英

Alexa Skills自定义意图中的未定义对象出现问题

[英]Trouble with an undefined object in Alexa Skills Custom Intents

我当前正在尝试使用Alexa技能,当我给Alexa正确的提示时,它将向我的苹果设备发出“查找我的iPhone”警报。 对于开发alexa技能集并进行相应的编码(尤其是在node.js中),我是一个新手。 这是我的报价:

var phoneId = "I have my values here";
var ipadId = "I have my values here";
var macId = "I have my values here";
var deviceId = "";


var APP_ID = ''; //replace with "amzn1.echo-sdk-ams.app.[your-unique-value-here]";

var AlexaSkill = require('./AlexaSkill');
var alexaResponse;

//Import Apple.js
var Apple = require('./Apple');
var apple = new Apple();

var alertSuccess = "Alert sent to Kenny's phone";
var alertFailed = "Alert couldn't be sent to Kenny's phone. Good luck finding it.";

var FindDevice = function () {
    AlexaSkill.call(this, APP_ID);
};

// Extend AlexaSkill
FindDevice.prototype = Object.create(AlexaSkill.prototype);
FindDevice.prototype.constructor = FindDevice;

FindDevice.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
    console.log("Quote onSessionStarted requestId: " + sessionStartedRequest.requestId
        + ", sessionId: " + session.sessionId);
    // any initialization logic goes here
};

FindDevice.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
    console.log("Quote onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
    getWelcomeResponse(response);
};

FindDevice.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
    console.log("Quote onSessionEnded requestId: " + sessionEndedRequest.requestId
        + ", sessionId: " + session.sessionId);
    // any cleanup logic goes here
};

FindDevice.prototype.intentHandlers = {
    // register custom intent handlers
    "FindDeviceIntent": function (intent, session, response) {
        determineDevice(intent, session, response);
    }
};

/**
 * Returns the welcome response for when a user invokes this skill.
 */
function getWelcomeResponse(response) {
    // If we wanted to initialize the session to have some attributes we could add those here.
    var speechText = "Welcome to the Lost Device. Which device shall I find?";
    var repromptText = "<speak>Please choose a category by saying, " +
        "iPhone <break time=\"0.2s\" /> " +
        "Mac <break time=\"0.2s\" /> " +
        "iPad <break time=\"0.2s\" /></speak>";

    var speechOutput = {
        speech: speechText,
        type: AlexaSkill.speechOutputType.PLAIN_TEXT
    };
    var repromptOutput = {
        speech: repromptText,
        type: AlexaSkill.speechOutputType.SSML
    };
    response.ask(speechOutput, repromptOutput);
}

  function determineDevice(intent, session, response) {
    var deviceSlot = intent.slots.Device;

    if (deviceSlot == "iPhone") {
      deviceId = phoneId;
      pingDevice(deviceId);
    } else if (deviceSlot == "iPad") {
      deviceId = ipadId;
      pingDevice(deviceId);
    } else if (deviceSlot == "Mac") {
      deviceId = macId;
      pingDevice(deviceId);
    } else {
      var speechText = "None of those are valid devices. Please try again.";
      speechOutput = {
          speech: speechText,
          type: AlexaSkill.speechOutputType.PLAIN_TEXT
        };
      response.tell(speechOutput);
    }
}

  function pingDevice(deviceId) {
    apple.sendAlert(deviceId, 'Glad you found your phone.', function(success, result){
        if(success){
            console.log("Alert Sent Successfully");
            var speechOutput = alertSuccess;
            response.tell(speechOutput);
        } else {
            console.log("Alert Unsuccessful");
            console.log(result);
            var speechOutput = alertFailed;
            response.tell(speechOutput);
        }
    });
  }

// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
    // Create an instance of the FindDevice skill.
    var findDevice = new FindDevice();
    findDevice.execute(event, context);
};

这是来自lambda的错误:

{
  "errorMessage": "Cannot read property 'PLAIN_TEXT' of undefined",
  "errorType": "TypeError",
  "stackTrace": [
    "getWelcomeResponse (/var/task/index.js:87:42)",
    "FindDevice.eventHandlers.onLaunch (/var/task/index.js:58:5)",
    "FindDevice.LaunchRequest (/var/task/AlexaSkill.js:10:37)",
    "FindDevice.AlexaSkill.execute (/var/task/AlexaSkill.js:91:24)",
    "exports.handler (/var/task/index.js:137:16)"
  ]
}

我知道这里有一个未定义的对象,但是对于我一生来说,我无法弄清楚代码哪里出了问题。 我试图从意图上获取插槽,然后根据所使用的插槽词将设备更改为ping。 另外,因为我对此还很陌生,所以很多编码只是通过将所有事物修补在一起来完成的。 我确实发现,当我一起删除.PLAIN_TEXT行时,代码在lambda中运行,但随后在alexa技能测试区域中中断了。 我直觉不明白意图传递的槽孔是如何传递的,但是我很难找到可以理解的材料。 任何帮助都太棒了!

在确定设备函数中,您将直接访问“设备”插槽对象,而不是直接传入的实际值,因此它永远不会与您预定义的设备名称集匹配。

插槽对象具有名称和值-如果在测试Alexa技能时查看开发人员控制台中服务模拟器中的服务请求JSON,则会看到类似以下内容的内容:

{
"session": {
    "sessionId": "SessionId.dd05eb31-ae83-4058-b6d5-df55fbe51040",
    "application": {
    "applicationId": "amzn1.ask.skill.61dc6132-1727-4e56-b194-5996b626cb5a"
    },
    "attributes": {
    },
    "user": {
    "userId": "amzn1.ask.account.XXXXXXXXXXXX"
    },
    "new": false
},
"request": {
    "type": "IntentRequest",
    "requestId": "EdwRequestId.6f083909-a831-495f-9b55-75be9f37a9d7",
    "locale": "en-GB",
    "timestamp": "2017-07-23T22:14:45Z",
    "intent": {
    "name": "AnswerIntent",
    "slots": {
        "person": {
        "name": "person",
        "value": "Fred"
        }
    }
    }
},
"version": "1.0"
}

注意,在这种情况下,我有一个名为“ person”的插槽,但是要获取插槽中的值,您需要访问“ value”属性。 在您的示例中,您可以将defineDevice函数的第一行更改为:

var deviceSlot = intent.slots.Device.value;

顺便说一句 ,我发现Alexa-cookbook Github存储库是学习如何使用Alexa SDK的必不可少的资源,在大多数情况下都有示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM