2017-07-19 47 views
0

我正在尝试使Alexa技能在我向苹果设备发送正确提示时向我的苹果设备发出“查找我的iPhone”警报。我在开发alexa技能集和编码方面很新颖(特别是在node.js中)。这里是我的话:Alexa技能定制意图中的未定义对象的问题

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); 
}; 

这里是拉姆达错误:

{ 
    "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技能测试区域中打开。我知道预感是如何通过的,但我无法找到关于此问题的材料。任何帮助将是太棒了!

回答

0

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

一个槽对象有一个名称和一个值 - 如果您在测试您的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" 
} 

注意我有一个名为在这种情况下,“人”的插槽,但在插槽中得到的价值,你需要访问“值”属性。在你的榜样,你会改变determineDevice功能的第一行:

var deviceSlot = intent.slots.Device.value; 

顺便说一句,我已经找到了Alexa-cookbook Github repository的重要资源,学习如何与Alexa的SDK工作,也有例子那里的大多数场景。

相关问题