How do I get the canonical slot value out of an Alexa request

半腔热情 提交于 2021-01-29 08:30:37

问题


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.

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)

Is there anything like

Alexa.getCanonicalSlotValue(requestEnvelope, 'intentSlotName');

回答1:


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);

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"
    }
}


来源:https://stackoverflow.com/questions/59569514/how-do-i-get-the-canonical-slot-value-out-of-an-alexa-request

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!