问题
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