Get Slot Value Alexa

In the previous articles we have built a Hello World Alexa skill.As any other hello world programs, the skill does only one job.We can only ask a single question and receive the single answer back.

The usability of the skill directly depends on how well the sample utterances and custom slot values represent real-world language use. As Alexa best practices say: 'Building a representative set. I have an intent and a custom slot value for this intent, the slot value is called 'event' which is a value from my 'LISTOFEVENTS' list. In this list, I have many options for slots, but more importantly, I have many synonyms for each slot value. To treat them all the same, I would like to use the ID I have assigned to each slot. Alexa: Thank you for playing Memory Challenge. Your final score is 2 out of 5. Your final score is 2 out of 5. Before we begin writing code for our skill, it’s a good idea to think through the conversation, and determine the three main acts that our skill will be handling.

Slot value: Slots are input values provided in a user’s spoken request. These values help Alexa figure out the user’s intent. These values help Alexa figure out the user’s intent. Sample utterances: The sample utterances specify the words and phrases users can say to invoke your intents.

Although you can build skill using that kind of interaction with a user, sooner or later you will feel the need of getting some custom data from a user.For example, you may want to tell a number or a name of a city and behave differently based on the answer.

Let’s see how we can do that.

Slots

To capture a user’s input we need to use so-called slots in intent utterances.To achieve that, we can use curly braces ({}) to provide a slot name.

For example:We would like to ask a user to provide any number.So we define an “AnswerIntent” with the following utterances:

  • Number {numberAnswer}
  • The number is {numberAnswer}
  • {numberAnswer}

Now, if the user says “The number is five hundred” we would like to get the number and use it somehow in our code.

At that moment we have defined the slot. But we need also to choose a “Slot Type” for that slot.The slot type will help Alexa to understand that is user saying.

That slot type can be either custom or built-in.

Built-in slot types

In our example, we need a number, so there is an “AMAZON.NUMBER” slot type.

Alexa Skill Get Slot Value

That is how the “AnswerIntent” looks in the skill builder.

Alexa Get Slot Value Id

Besides that type, the skill builder provides a vast amount of other types.The list of types is growing.

For example, there are slot types to work with dates and times.As well as some other interesting ones. There are slot types with a list of airports or animals.

We can use a built-in type and extend it with additional values.

Here is just a small portion of them.

Custom slot types

Let’s say you want to get a more specific response from your users, but there is no built-in slot type for these needs.Then you can build your own custom slot type and provide a list of possible values.

Get Slot Value Alexa

Go ahead and in the skill builder click to “Add” slot type link and choose a name for your custom slot.

Then you can specify all the possible values for that type. Even add synonyms if you need.

Get

After that, you are free to use the slot type in your slots. Create a new slot in utterance samples and attach the custom type.

Get Slot Value Alexa Echo Dot

Getting values from the slots

We have figured out how to define slots in the skill builder. The half of the job is done.

Now it’s time to access the response from our codebase.

We can achieve that in two steps.

Licht

At first, we need to tell Alexa to start listening to a user’s input.To do that we need some intent handler with reprompt call in it.

At this moment Alexa waits for the user’s input.If the user tells us something satisfying the sample utterances from “AnswerIntent” example above,Alexa would trigger “AnswerIntentHandler” where we can fetch a slot value.

The slots a live deep inside handlerInput.requestEnvelope.request.intent.slots object.We can fetch the slot by the name slots['<name-of-the-slot>'] and then access the value slots['<name-of-the-slot>'].value.

Let’s see it in the example:

Here we fetch the slots object to a slots constant.Then we’ve got the value of numberAnswer slot.

That’s it. We’ve got it.

Now to demonstrate that Alexa understood a user correctly, we tell her to say the number back to the user.

That is how the dialog looks like.

Wrapping up

We have a new tool in the toolbelt. Now when we need to get some input from the user we can use slots for that.We have learned there are two kinds of slot types: custom and built-in.We can use both in order to build better Alexa skills.

Alexa Skill Get Slot Value Java

You can find a complete example on GitHub.