如何使用 ASK-SDK v2 中的 handlerInput 获取自定义意图槽值
How to get custom intent slot values using handlerInput in ASK-SDK v2
我正在使用 ASK-SDK v2 创建一个基本的计算器技能。我不确定如何将用户提供的插槽值放入新版本的 Lambda 代码中。我能够让它与旧版本一起使用。
对话
用户:打开计算
Alexa:你可以让我加减乘除
用户:加二加三
Alexa:2 和 3 的和是 5
下面是我的 IntentSchema
{
"interactionModel": {
"languageModel": {
"invocationName": "calculate",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AddIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"Sum of {numA} and {numB}",
"add {numA} and {numB}"
]
},
{
"name": "SubIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"difference between {numA} and {numB}",
"subtract {numA} from {numB}"
]
},
{
"name": "ProductIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"multiply {numA} and {numB}",
"product of {numA} and {numB}"
]
},
{
"name": "DivideIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"divide {numB} by {numA}",
"divide {numA} by {numB}"
]
},
{
"name": "ExponentialIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
},
{
"name": "numC",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"{numA} raised to the power of {numB} by {numC}",
"{numA} raised to the power {numB}"
]
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
}
],
"types": []
}
}
}
我要在这里添加 addintenthandler。请告诉我我用来从意图中获取槽值的方法是否正确,或者我是否应该使用 sessionattributes
const AddIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AddIntent';
},
handle(handlerInput) {
var output1 = "";
var num1 = handlerInput.resuestEnvelope.request.intent.slots.numA.value;
var num2 = handlerInput.resuestEnvelope.request.intent.slots.numB.value;
if((num1)&&(num2)){
output1 = 'The sum of ' +num1+ ' and ' +num2+ ' is ' + (num1+num2);
}
else {
output1 = 'Enter valid number';
}
const speechText = output1;
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
Alexa 回复 "Unable to process requested skill response"
欢迎任何帮助
更新:SDK 中现在有内置函数:
Alexa.getSlotValue()(returns字符串值)和getSlot()(returns槽对象)
Alexa.getSlotValue(handlerInput.requestEnvelope, "someSlotName")
旧答案:
你打错了,resuestEnvelope
应该是 requestEnvelope
。在任何情况下,我都创建了完全相同的技能,一个计算器(西班牙语,但它基本上是同一件事)并且我使用了一个名为 getSlotValues()
的辅助函数,我鼓励您重复使用它。当您必须捕获自定义插槽时(由于实体解析结构不同,处理方式不同),它也会很好用:
https://github.com/germanviscuso/skill-sample-nodejs-mycalculator
我正在使用 ASK-SDK v2 创建一个基本的计算器技能。我不确定如何将用户提供的插槽值放入新版本的 Lambda 代码中。我能够让它与旧版本一起使用。
对话 用户:打开计算 Alexa:你可以让我加减乘除 用户:加二加三 Alexa:2 和 3 的和是 5
下面是我的 IntentSchema
{
"interactionModel": {
"languageModel": {
"invocationName": "calculate",
"intents": [
{
"name": "AMAZON.CancelIntent",
"samples": []
},
{
"name": "AMAZON.HelpIntent",
"samples": []
},
{
"name": "AMAZON.StopIntent",
"samples": []
},
{
"name": "AddIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"Sum of {numA} and {numB}",
"add {numA} and {numB}"
]
},
{
"name": "SubIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"difference between {numA} and {numB}",
"subtract {numA} from {numB}"
]
},
{
"name": "ProductIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"multiply {numA} and {numB}",
"product of {numA} and {numB}"
]
},
{
"name": "DivideIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"divide {numB} by {numA}",
"divide {numA} by {numB}"
]
},
{
"name": "ExponentialIntent",
"slots": [
{
"name": "numA",
"type": "AMAZON.NUMBER"
},
{
"name": "numB",
"type": "AMAZON.NUMBER"
},
{
"name": "numC",
"type": "AMAZON.NUMBER"
}
],
"samples": [
"{numA} raised to the power of {numB} by {numC}",
"{numA} raised to the power {numB}"
]
},
{
"name": "AMAZON.NavigateHomeIntent",
"samples": []
}
],
"types": []
}
}
}
我要在这里添加 addintenthandler。请告诉我我用来从意图中获取槽值的方法是否正确,或者我是否应该使用 sessionattributes
const AddIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AddIntent';
},
handle(handlerInput) {
var output1 = "";
var num1 = handlerInput.resuestEnvelope.request.intent.slots.numA.value;
var num2 = handlerInput.resuestEnvelope.request.intent.slots.numB.value;
if((num1)&&(num2)){
output1 = 'The sum of ' +num1+ ' and ' +num2+ ' is ' + (num1+num2);
}
else {
output1 = 'Enter valid number';
}
const speechText = output1;
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.getResponse();
}
};
Alexa 回复 "Unable to process requested skill response" 欢迎任何帮助
更新:SDK 中现在有内置函数: Alexa.getSlotValue()(returns字符串值)和getSlot()(returns槽对象) Alexa.getSlotValue(handlerInput.requestEnvelope, "someSlotName")
旧答案:
你打错了,resuestEnvelope
应该是 requestEnvelope
。在任何情况下,我都创建了完全相同的技能,一个计算器(西班牙语,但它基本上是同一件事)并且我使用了一个名为 getSlotValues()
的辅助函数,我鼓励您重复使用它。当您必须捕获自定义插槽时(由于实体解析结构不同,处理方式不同),它也会很好用:
https://github.com/germanviscuso/skill-sample-nodejs-mycalculator