如何从 Alexa 请求中获取规范槽值 [英] How do I get the canonical slot value out of an Alexa request

查看:27
本文介绍了如何从 Alexa 请求中获取规范槽值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义插槽编写 Alexa 技能,但 Alexa 忽略了我的同义词.显然 Alexa.getSlotValue(requestEnvelope, 'intentSlotName'); 将检索实际说出的单词,而不是规范值.我正在与规范值进行比较以确定程序行为,因此我真的希望我的插槽在遇到同义词时返回规范值,而不是同义词本身.

I am attempting to write an Alexa skill with custom slots, but Alexa ignores my synonyms. Apparently Alexa.getSlotValue(requestEnvelope, 'intentSlotName'); will retrieve the actual spoken words, as opposed to the canonical value. I am comparing against the canonical values to determine program behavior, so I would really prefer my slots to return the canonical value when I hit a synonym, rather than the synonym itself.

我该怎么做?我在 Alexa 文档中找到答案时遇到了一些麻烦,而我看到的答案对于行为来说似乎非常复杂,似乎它实际上应该是默认的(并且当我尝试它们时它们不起作用)

How do I do this? I have been having some trouble finding the answer in the Alexa documentation, and the answers I do see seem really complicated for behavior that seems like it should practically be default (and they didn't work when I tried them)

有没有类似的东西

Alexa.getCanonicalSlotValue(requestEnvelope, 'intentSlotName');

推荐答案

ASK SDK 目前没有帮助程序来访问规范值,但您可以创建一个简单的函数来获取它.您需要从使用 getSlot 助手开始,它返回一个 Slot 对象,其中包含定义的解析实体 这里.

There are currently no helpers in the ASK SDK to access canonical value, but you can create a simple function to fetch it. You'll want to start with using the getSlot helper, which returns a Slot object with resolved entities as defined here.

const getCanonicalSlot = (slot) => {
    if (slot.resolutions && slot.resolutions.resolutionsPerAuthority.length) {
        for (let resolution of slot.resolutions.resolutionsPerAuthority) {
            if (resolution.status && resolution.status.code === 'ER_SUCCESS_MATCH') {
                return resolution.values[0].value.name;
            }
        }
    }
}

然后在您的处理程序中调用它:

Then call this in your handler:

let mySlot = Alexa.getSlot(requestEnvelope, 'mySlot');
let mySlotCanonical = getCanonicalSlot(mySlot);

我建议在 Alexa 开发人员控制台的测试"选项卡中进行试验(或简单地完整记录请求),以更好地理解上述代码为何有效.例如,基本插槽实现的 JSON 将按如下方式返回:

I recommend experimenting in the Test tab on the Alexa Developer Console (or simply logging requests in full) to better understand why the code above works. For example, the JSON for a basic slot implementation will be returned as such:

"slots": {
    "mySlot": {
        "name": "mySlot",
        "value": "bar",
        "resolutions": {
            "resolutionsPerAuthority": [{
                "authority": "amzn1.er-authority.echo-sdk.amzn1.ask.skill.****.mySlotType",
                "status": {
                    "code": "ER_SUCCESS_MATCH"
                },
                "values": [{
                    "value": {
                        "name": "foo",
                        "id": "acbd18db4cc2f85cedef654fccc4a4d8"
                    }
                }]
            }]
        },
        "confirmationStatus": "NONE",
        "source": "USER"
    }
}

这篇关于如何从 Alexa 请求中获取规范槽值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆