Ingrid
Ingrid

Reputation: 161

Alexa Skill - How to Retrieve Slot Value in Lambda Function

I am developing an Alexa Skill with one Intent that includes a Slot with several possible values.

My slot is defined with "name" : "Channel", "type" : "LIST_OF_CHANNELS", and values

  1. iqram
  2. ingrid
  3. phil
  4. clyde

How do I retrieve the uttered slot value to use in my Lambda function? It's the "retrieve value of slot from utterance part" I'm looking to have answered. Thanks so much.

 // retrieve value of slot from utterance     
 var c = intent.slots.Channel.value; 

 // append value to end of URL that references API
 fetchEnseParse("/channel/" + c, function(body) {

 // continuation of this function is below 

Upvotes: 8

Views: 11357

Answers (6)

user10112683
user10112683

Reputation: 1

It is not safe to simply look at this.event.request.intent.slots.<yourslot>.value

This value has only the 'utterance' and it may not be what you are looking for

I had to deeper into the object and look at the resolutions object and check the authority object which has a status of code=="ER_SUCCESS_MATCH"

Here's my java code which worked - you can pretty much do the same in Nodejs

private String getSlotValue(JSONObject s)
{

    String utterance="";
    String choice ="NA";

  try {

        if(s.has("option"))
        {

        JSONObject optionObject =s.getJSONObject("option");

        utterance =optionObject.getString("value");

        JSONObject resolutionsObject =optionObject.getJSONObject("resolutions");



        JSONArray resolutionsPerAuthority =resolutionsObject.getJSONArray("resolutionsPerAuthority");

        for (int i = 0; i < resolutionsPerAuthority.length(); ++i) {
            JSONObject auth = resolutionsPerAuthority.getJSONObject(i);

            if (auth.getJSONObject("status").getString("code").equals("ER_SUCCESS_MATCH") )

                {
                if(auth.has("values"))
                    {
                    JSONArray  values = auth.getJSONArray("values");

                    choice=values.getJSONObject(0).getJSONObject("value").getString("id");
                    }
                }           

        }

        }

    return choice;

    }
  catch (Exception e) {
   choice="Error "+e.getMessage();
   return choice;
   }
}

Upvotes: 0

chainstair
chainstair

Reputation: 827

Lets say u said to alexa, the word "coin" and you programmed this as a synonym of bitcoin (the main objectname).

Now u can get 2 results.

First: You get the original value "bitcoin", which is the original value/matched value

Or second: You get the spoken value "coin"

First (bitcoin):

this.event.request.intent.slots.<yourslotname>.resolutions.resolutionsPerAuthority[0].values[0].value.name

Second (coin):

this.event.request.intent.slots.<yourslotname>.value

Careful: yourslotname is not the Slottype! It's the name

Upvotes: 0

Bharat Anand
Bharat Anand

Reputation: 484

For me the below worked:

event.currentIntent.slotDetails.<slot-name>.originalValue

where slot-name is Channel.

Upvotes: 0

Caleb Gates
Caleb Gates

Reputation: 1024

var c = this.event.request.intent.slots.Channel.value;

Upvotes: 0

Pseuplex
Pseuplex

Reputation: 425

var c = this.event.request.intent.slots.slotname.value

This should give you what you're looking for.

Upvotes: 13

Tom
Tom

Reputation: 17892

In the event that your lambda receives you can find it here...

{
  "request": {
    "type": "IntentRequest",
    "intent": {
      "name": "YourIntentName",
      "slots": {
        "slotname": {
          "name": "slotname",
          "value": "HERE!"
        }
      }
    },
    "locale": "en-US"
  },
} 

The event is passed to your lambda handler.

Upvotes: 5

Related Questions