Alexa — What are Slots and how to read Slot-Values?

This post is about how to declare and work with slots. It will also show you the struggle which I had while working with them. Enjoy!

Introduction to Slots

Slots are basically variables in utterances. These can have predefined values but are by default empty. To define a slot you first need to create a custom Intent in your skill because you can’t use them in the built-in Intents. After that, when you need to create your sample utterances for the Intent, simply define a slot by writing it in curly brackets.

In this post we want to create a skill that returns opening hours for an office on a variable day in an also variable city. So we create an utterance that looks like this:

Sample utterance with slots

The Skill Builder will detect {City} and {Day} automatically as slots and highlights them. In our intents list we will see these two slots.

the slots in the intent list

If we click on them, we can give them a specific type. For the city slot we choose ‘Amazon.DE_City’ which is a predefined slot-type from Amazon that can handle about 5000 city names in Germany. The day slot goes with ‘Amazon.Date’. As next we want to choose if the slot is required to fulfill the Intent. In our case booth slots are required to fulfill the intent.
They’re needed because we can’t give out specific opening hours without knowing the day or the city of the given office.

We also want to take a look at the Alexa speech prompts and the user utterances. To explain them we must take a look at the Alexa feature ‘Dialog.Delegate’, this feature can delegate the dialog to Alexa if the user didn’t provide all the needed values for the slots. If a user did not provide a value for a slot from our skill, we can define some speech prompts in the slot menu. Alexa will then ask the user to provide a value for the specific slot. We will ask the user in our speech prompt for the day slot: ‘You still must tell me for which day you want to know the opening hours’ and as user utterances we define ‘on {Day}’.

To further explain ‘Dialog.Delegate’ we need another example. Let’s say the user only provides our skill with the location but not with the day. This means not all the slots that are needed to fulfill the Intent are filled. That’s the point where we hand out the dialog with the user to Alexa. It then will use our predefined utterance to ask the user for the missing value of the slot. Now the user will answer for example with ‘on Friday’. Alexa will now update the slot value, and we have all values we need to work with.

By the way, a pretty nice thing is that the user can “over answer” the question and also fill another needed slot. For example ‘on Friday in Berlin’ will set the day slot value to ‘Friday’ and the city slot to ‘Berlin’.

This sounds easy where is the struggle?

There it is…

Struggle & Solution

I ran into serval problems while working with slots, here are my ‘favorites’:

How to get these slot values?
So the user provides us with some values, but oh no, how do we access them? No problem, just take a look at the documentation.. Nope that’s a dead end. The documentation will only provide you with some utterance examples and some extra (useful) information for the different slot types. So there I was, searching on Stackoverflow and other various websites how to access a slot value with no solution, which also shows me another problem and cost really too much time. Finally I took a look at the ‘this.event.request.intent.slots-object’. One line of code. So if you want to access the city slot value simply put this line of code in your lambda function. Thank you, Peter for the pointer!

var cityName = this.event.request.intent.slots.City.value;

Why is Sunday not a valid input for the day slot?

To explain this I will show you some line of code from my skill.

const daysArr = [ “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”];
let d = new Date(this.event.request.intent.slots.Tag.value);
dayName = daysArr[d.getDay()];

This code transforms the provided date from ‘Amazon.Date’ into a speakable day. But every time the user say ‘Sunday’ the skill returns ‘dayName’ with an incorrect output. That really drived me crazy for serval minutes until I realized that the first day of the week isn’t Monday in the states, is it Sunday and I need to rearrange my array to this:

[ “Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”];

So always remember, even the smallest problems can break your skill!

Conclusion

Time to finish this post. In conclusion Alexa has some pretty nice built-in features like ‘Dialog.Delegate’, but without better explanations and code snippets these didn’t arrive in many skills which could use them to save much code and easily upgrade the user experience. Also, Alexa has multi language support, so maybe the documentation should hint to some local differences, like ‘Sunday is the first day of the week’ - thing even if it’s JavaScript specific. So please Amazon give us a better documentation and also create some mid range tutorials so the community can grow from fact-skill clones to a living and creative developer community!

Further Information