When building a skill for Alexa you need to be able to get input from the user. All but the most simplistic business logic is going to have variables for completing the users desire. For Alexa, these variables are called Slots. Just like a variable in a strongly typed programming language these Slots must be declares and given a type so that the user can provide the data. This post will talk about how Slots work and how to use them in your skill.
There are basically two types of slots you can use in an Alexa skill.
- Value types, like numbers, dates, times, etc.
- Lists of items (i.e. multiple choice)
There is actually a third type called Phrases but Phrases are complicated types of slots that open the door for more responsive skills, such as search queries. Keep an eye on my blog for a full writeup on Phrase slots in the near future, but we will not be getting into those today.
The available Value Types are limited to whole numbers, phone numbers, and several variations on date and time. Aside from the pick lists, these are all the options you have. Specifically, the options are
- Dates – the user can give a specific calendar date or a descriptive phrase like “this Wednesday” or “first of next month”. Your skill will receive a slot value of the specific date
- Duration – this is for a span of time, like when you set a kitchen timer with your Echo.
- Numbers – this supports positive and negative whole numbers, not decimals. There is also a special type of slot called Four Digit Numbers that is useful for years, PIN codes, and such.
- Ordinal – if the user says “second”, your skill gets a 2
- Phone Numbers – many different formats are supported including U. S. local 7 digit numbers, numbers with area code, and numbers with country code
- Time – you skill gets the time the user stated in 24-hour format where midnight is “00:00”. It also supports certain terms like “Midnight” or Noon.
When I say Alexa supports “date” you may think it’s limited to YYYY-MM-DD, but that would be a mistake. Alexa supports many different permutations of date so you must be prepared to interpret each because you cannot control what the user says. When you declare that your slot accepts a date, you can get any one of the formats described here.
If the user says a specific calendar date, the value will be returned to your skill in ISO 8601 format, which is just YYYY-MM-DD except for some special cases when using different languages and locales.
Date slots can also accept descriptive words like “tomorrow” “today”, “next Tuesday”, “Christmas”, etc. Your skill will just receive the specific date without knowing how they described that date. If they use a descriptive term for a date then it will always be interpreted as being in the future. If they say “Monday”, it’s next Monday. If today is Monday, then you get today’s date.
The user is also allowed to specify a week, such as “this week”. In that case you will get the week date format instead of calendar date. The week format is in the form YYYY-WNN where YYYY is the year and NN is the week number. For example, the first week of 2022 is 2022-W01. The descriptive terms are limited; for example “last week of the year” will get no response, nor will “the week of February 1”. However, “this week”, “last week”, and “next week” are reliable.
Weekends work the same way as weeks but you get a “-WE” tacked onto the end. The first weekend of the year would be 2022-W01-WE.
Asking for a month will return the month in the format YYYY-MM if you are using a form of English or YYYY-MM-XX when using some other languages. For example February will be either 2022-02 or 2022-02-XX.
Years work the same way as months except you will receive just the year, such as YYYY or YYYY-XX-XX, depending on your language setting.
The user can also specify a decade such as “this 90’s”, in which case you will receive the string 199X or 199X-XX-XX.
Finally the user can specify a season as Spring, Summar, Fall, or Winter. Your skill will receive the year then a season modifier, like 2022-SP, 2022-SU, 2022-FA, or 2022-WI.
Your skill can never tell what kind of date will be returned, so when you are coding for a date be prepared for any of these options.
Duration slots return a time span in a special format. An example of duration use is in setting cooking timers on your Echo.
Duration returns a value in the format “PnYnMnDTnHnMnS” where P means “duration” and then the number of years, months, days, hours, minutes and seconds. Note the “T” in the middle that marks the transition from days to time. This may seem unnecessary but it is necessary to detect the difference between Months and Minutes. If your timespan is 9 Months you will receive “P9M”, but for 9 Minutes you will receive PT9M.
In theory if the user says “two years one month eight days four hours nine minutes and three seconds” you should receive the string “P2Y1M8D4TH9M3S”. However large durations like this can be unpredictable. When I tried the utterance above I got 4 different values as follows
From the point of view of my skill, the user gave me 4 different durations for one slot.
There are two number types you can use depending on your purpose – Numbers and Four-Digit-Numbers. There is very little difference between the two except that the Four-Digit-Numbers is a bit better at recognizing PIN numbers and other types of codes. It is also better at treating the “oh” as a zero when the user says something like “six oh nine nine”.
Note that the Four-Digit-Numbers name is a misnomer because it will return any number of digits, not just 4. If you specify a number in the millions for a Four-Digit-Number slot, you will still get the whole number. Same as for a 1 or 2 digit number, you will get what the person said. In daily use, I’ve noticed very little difference between the two.
The Number slot type can handle positive and negative whole numbers, but not decimal numbers. If you try to say “seven point two” you will often get a question mark back, but sometimes just one or the other of the two numbers. If you need to get a decimal number input, you will need to work at it.
Ordinals are “first”, “third”, etc. When you specify an Ordinal slot your skill will receive the integer representing that word. There’s not much more so say about ordinals.
The phone number recognition can be powerful, recognizing everything from local numbers to overseas. However, there is no enforcement for phone number format. If someone speaks a number without enough digits, you will get that incorrect number. It’s up to you to validate the format and ask for the information again.
The user can specify the country code, area code, and local phone number, including saying “plus” before the country code if they wish (it’s not required). If they say “plus” your skill will receive the “plus”. If they do not say it, you will not get it. Other than the plus, you will not get any punctuation in the number – just the digits.
The phone number slot value seems to be more of a simple regex enforcement for the proper allowable characters of numbers and “plus”. Beyond that you are on your own to validate the incoming number.
Time values are received in 24 hour format where midnight is “00:00”. The values are always in HH:MM format for all language settings.
The user can also specify a time period like Evening (your skill will receive the string “EV”), Night (“NI”), Morning (“MO”), and Afternoon (“AF”).
Something to keep in mind is when I said “midday” with an en-US locale I was given midnight, not noon. I have not tested whether this would be recognized properly in a different locale.
All the rest of the built in slot types, as well as any custom slot types you may create, are all lists. There are many types of predefined lists, and you can see them all here. Below is a list of the 90+ items that were available when I wrote this article.