首页 > 解决方案 > 如何使用 ASK-SDK v2 中的 handlerInput 获取自定义意图槽值

问题描述

我正在使用 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 回复“无法处理请求的技能响应”欢迎任何帮助

标签: node.jsaws-lambdaalexa-skills-kitalexa-slot

解决方案


更新:SDK 中现在有内置函数:Alexa.getSlotValue()(返回字符串值)和 getSlot()(返回 Slot 对象) Alexa.getSlotValue(handlerInput.requestEnvelope, "someSlotName")

旧答案:您有错字,resuestEnvelope应该是requestEnvelope. 无论如何,我已经创建了完全相同的技能,一个计算器(在西班牙语中,但它基本上是相同的东西),我使用了一个名为的辅助函数getSlotValues(),我鼓励你重用它。当您必须捕获自定义插槽(由于实体解析结构不同而处理方式不同)时,它也会很好地工作:

https://github.com/germanviscuso/skill-sample-nodejs-mycalculator


推荐阅读