首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ASK-SDK v2中的handlerInput获取自定义意图槽值

如何使用ASK-SDK v2中的handlerInput获取自定义意图槽值
EN

Stack Overflow用户
提问于 2019-02-18 15:32:24
回答 1查看 1.5K关注 0票数 0

我正在使用ASK-SDK v2创建一个基本的计算器技能。我不确定如何在新版本中将用户提供的槽值放入Lambda代码中。我能够让它与旧版本一起工作。

对话用户:打开calculate Alexa:你可以要求我进行加、减、乘、除用户:将2加3 Alexa: 2加3的和是5

下面是我的IntentSchema

代码语言:javascript
复制
{
    "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。请告诉我,我从意图中获取槽值的方法是否正确,或者我是否应该使用sessionattribute

代码语言:javascript
复制
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 skill response“欢迎任何帮助

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-19 18:03:38

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

老答案:你有一个拼写错误,resuestEnvelope应该是requestEnvelope。在任何情况下,我都创建了完全相同的技能,一个计算器(在西班牙语中,但它基本上是相同的东西),并且我使用了一个称为getSlotValues()的辅助函数,我鼓励您重用它。当您必须捕获自定义插槽(处理方式不同,因为实体解析结构不同)时,它也会很好地工作:

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54742339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档