3

In the following example: If the user says toffee, shouldn't that be translated to candy? I ask because the value handed to my intent is 'toffee'. So not sure what I have incorrect.

types":[  
   {  
      "name":"SHOPPING_LIST",
      "values":[  
         {  
            "id":null,
            "name":{  
               "value":"candy",
               "synonyms":[  
                  "toffee"
               ]
            }
         },
         {  
            "name":"GetPrice",
            "samples":[  
               "Get me the price of {item}",
               "tell me the price of {item}",

            ],
            "slots":[  
               {  
                  "name":"item",
                  "type":"SHOPPING_LIST"
               }
            ]
         }
Viktorov
  • 136
  • 2
  • 8
thedreamsaver
  • 1,294
  • 4
  • 16
  • 27

2 Answers2

5

We need to handle the entity resolution in your backend code. More can be referred here: https://developer.amazon.com/blogs/alexa/post/5de2b24d-d932-4c6f-950d-d09d8ffdf4d4/entity-resolution-and-slot-validation

In your code you can add,

this.attributes.item = slotValue(this.event.request.intent.slots.item);

Also, add this outside your handler function,

function slotValue(slot, useId){
    let value = slot.value;
    let resolution = (slot.resolutions && slot.resolutions.resolutionsPerAuthority && slot.resolutions.resolutionsPerAuthority.length > 0) ? slot.resolutions.resolutionsPerAuthority[0] : null;
    if(resolution && resolution.status.code == 'ER_SUCCESS_MATCH'){
        let resolutionValue = resolution.values[0].value;
        value = resolutionValue.id && useId ? resolutionValue.id : resolutionValue.name;
    }
    return value;
}

Now when your user enters toffee, it will be translated to candy.

thedreamsaver
  • 1,294
  • 4
  • 16
  • 27
  • Thank you for the excellent function but I find it staggering that this isn't built in for us in some way. – Caltor Oct 11 '18 at 22:00
  • True that. I had messed up my head a lot trying to find out if there was anything built-in. Unfortunately, there isn't any yet. – thedreamsaver Oct 20 '18 at 17:47
  • @Caltor actually the response would be a JSON so for extracting you just need to follow the tree, see my below answer for the direct extraction without passing anything to any function – Daniel Selvan Mar 20 '19 at 12:10
3

The general response JSON structure would be as follows, so to extract it in a line use

source = intent.slots.source.resolutions.resolutionsPerAuthority[0].values[0].value.name;

"request": {
    "type": "IntentRequest",
    "requestId": "amzn1.ech****************************************************************************************",
    "timestamp": "2019-03-13T08:34:46Z",
    "locale": "en-US",
    "intent": {
        "name": "STMDStreamStartIntent",
        "confirmationStatus": "NONE",
        "slots": {
            "source": {
                "name": "source",
                "value": "source 1",
                "resolutions": {
                    "resolutionsPerAuthority": [
                        {
                            "authority": "amzn1.er-authority.e************************************************",
                            "status": {
                                "code": "ER_SUCCESS_MATCH"
                            },
                            "values": [
                                {
                                    "value": {
                                        "name": "source1",
                                        "id": "SOURCE_ONE"
                                    }
                                }
                            ]
                        }
                    ]
                },
                "confirmationStatus": "NONE",
                "source": "USER"
            }
        }
    }
}

Hope this would be simple and helpful.

Daniel Selvan
  • 959
  • 10
  • 23
  • 2
    If it doesn't exist this will throw an error. That's why @thedreamsaver uses an if statement to see if it exists. Also his is a function so it is reusable throughout the skill. – dillon.harless May 17 '19 at 11:44