目前正在尝试编写我的第一个Alexa技能,一个简单的计算器。我试图通过加入一个额外的意图来启动它。我已经收到了一连串的错误,并且没有找到关于这个问题的任何记录。
这是相关的node.js代码:
var https = require('https');
var Alexa = require('alexa-sdk');
exports.handler = (event, context) => {
try {
if (event.session.new) {
// New Session
console.log("NEW SESSION");
}
switch (event.request.type) {
case "LaunchRequest":
// Launch Request
console.log(`LAUNCH REQUEST`);
context.succeed(
generateResponse(
buildSpeechletResponse("Launch Request", "Welcome to Pro Calculator", "", true),
{}
)
);
break;
case "IntentRequest":
// Intent Request
console.log(`INTENT REQUEST`);
onIntent(event.request,
event.session,
function callback(sessionAttributes, speechletResponse){
context.succeed(generateResponse(sessionAttributes, speechletResponse));
});
break;
case "SessionEndedRequest":
// Session Ended Request
console.log(`SESSION ENDED REQUEST`);
break;
default:
context.fail(`INVALID REQUEST TYPE: ${event.request.type}`);
}
} catch(error) { context.fail(`Exception: ${error}`) }
};
// Called when the user specifies an intent for this skill.
function onIntent(intentRequest, session, callback) {
console.log("onIntent requestId=" + intentRequest.requestId
+ ", sessionId=" + session.sessionId);
var cardTitle = "Addition";
var intent = intentRequest.intent,
intentName = intentRequest.intent.name;
// dispatch custom intents to handlers here
switch(intentName){
//addition
case "addIntent":
var valA = this.event.request.intent.slots.valA;
var valB = this.event.request.intent.slots.valB;
var ans = valA + valB;
callback(session.attributes, buildSpeechletResponse(cardTitle, `The answer is ${ans}`, "", "true"));
break;
default:
throw "Invalid intent";
}
}
这是相关的json代码:
{
"intent": "addIntent"
},
{
"slots": [
{
"name": "valA",
"type": "AMAZON.NUMBER"
},
{
"name": "valB",
"type": "AMAZON.NUMBER"
}
],
}
最后,生成了错误:
{
"errorMessage": "Exception: TypeError: Cannot read property 'request' of undefined"
}
非常感谢任何帮助
答案 0 :(得分:0)
交互模型中的意图模式看起来不正确。请尝试使用以下意图架构,
{
"intents": [
{
"slots": [
{
"name": "valA",
"type": "AMAZON.NUMBER"
},
{
"name": "valB",
"type": "AMAZON.NUMBER"
}
],
"intent": "addIntent"
}
]
}

答案 1 :(得分:0)
首先,你的意图架构不正确,如@Vijayanath Viswanathan所说,通过以下架构更改你的意图架构:
{
"intents": [
{
"intent": "addIntent",
"slots": [
{
"name": "valA",
"type": "AMAZON.NUMBER"
},
{
"name": "valB",
"type": "AMAZON.NUMBER"
}
]
}
]
}
然后你必须改变代码以获得插槽值。
改为此,
var valA = this.event.request.intent.slots.valA.value;
var valB = this.event.request.intent.slots.valB.value;
而不是这个,
var valA = this.event.request.intent.slots.valA;
var valB = this.event.request.intent.slots.valB;
通过此更改,您将获得插槽的值。