Alexa Slots, Part 1

Adding Existing Slot Types to Your Amazon Alexa Skill

May 09, 2020

To teach you how to add predefined slot types to your Alexa skill, I will be using the 'Super Awesome Facts' skill I set up in my Building an Alexa Skill post a couple of weeks ago.

Start by heading to the Alexa developer console in the skill of your choice (follow link to above post if you haven't yet created one) and click on '+ Add' in the left-hand panel where it says 'Slot Types(0)'.

Alexa slot types in left panel.

You'll now see the following where you have the option to create a custom slot type or use an existing slot type. In today's post, we'll be focusing on existing slot types.

Existing Slot Types

Adding slot types.

Before we press on, I should mention that my main goal or looking at the existing slot types in this section of the Alexa dev console is to simply give you the opportunity to browse what slot types already exist so you don't go time building custom slot types when you don't need to. You can add slots to your skill here but I am going to show you a different way shortly.

When you click on the checkbox for existing types you can either search for a type or look through the list types and number, date, and time types to see if there is something that would be applicable to your skill. Amazon has 52 built-in slot types which makes adding slots to Alexa skills incredibly easy.

Now, of course, you don't want to just add any slot type to your skill. You should think about intent handlers that may add value to your users and what parts of a users speech you want to store from their response. In the case of my 'Super Awesome Facts' skill, one thing I could see being a useful feature for my users would be having the ability to specify how many facts they would like to hear. Alexa would ask something like, "How many facts would you like me to share with you?" and the user would reply with a number.

Doing a search for 'number' gives me a shortlist of 3 options. The second of which, AMAZON.NUMBER, is the most applicable to the goal I am trying to achieve. As mentioned a moment ago, I could add this here but we're going to do it slightly differently.

Using an existing slot type.

To allow my users to specify the number of facts they would like to hear there are 2 things we need to do:
  1. Create an intent handler that will respond once a user has given a number. This is where we will add our existing number slot type.
  2. Update our built-in launch intent handler to prompt the user for the desired number of facts.

Creating a New Intent Handler

First we start by creating the new intent. I'm going to call mine `NumberOfFactsIntent`.

Adding an intent.

Next, on the add utterances section, I'm going to scroll down to where it says 'Intent Slots(0)' and in the input field where it says 'Create a new slot' I'm going to add my slot 'numberOfFacts', click the + button, and then set it to 'AMAZON.NUMBER' in the dropdown.

Intent slots.

From here, I'm going to scroll back up and add a few utterances. The more variations you can add for each intent, the better, but for the purposes of this post I'll just add a handful.

Sample utterances.

You can see in the screenshot above that I have added 5 utterances and all include the slot type I added. If I were to publish this skill and a user said 'Tell me three facts' the value of '3' would be saved to session attributes so we could then use that value like you would use state in any other application.

Updating Our Launch Intent Handler

Over in my text editor, I've navigated to the built-in launch intent handler and updated the welcome message so that Alexa will prompt the user to give a number.

Updating the Alexa response.

When building an Alexa skill you need to think about your application like you would think about a conversation with a friend. If you wanted them to give you information, you would need to ask them a specific question. You'd probably find yourself in a pretty unfortunate situation if you just sat around hoping your friend would somehow read your mind and respond with whatever information you were hoping to hear. Alexa skills works the same way. If Alexa doesn't provide a prompt, a user may never give a number.

Save and build your model.

Model saving.

Then update your code and deploy your lambda function. If you aren't sure how to do this, please refer to my post Work Locally on Your Alexa Skill. It will outline how to get your code set up on your machine, how to easily generate the initial code for your lambda function, and how to deploy your function. The tool listed in the linked post that allows you to generate code will give you a number of handy functions, including one called getSlotValues which makes it easy for you to access the slot types you've added to your skill. Here's how I'm accessing the numberOfFacts slot value in my code and what I've currently set the response to:
let slotValues = getSlotValues(request.intent.slots);
let say = `You would like to hear ${slotValues.numberOfFacts.heardAs} facts... We will work on following up with a new intent and custom slot in the next post!`;

Now it's time to test out your changes. Under 'Test' in the Alexa dev console (or using your Alexa device if you have one), launch your skill.

Testing your skill.

We can see my updated welcome message with the prompt asking the user for the number of facts they would like to hear.

Updated welcome message.

Perfect! We can see in the above screenshot, after Alexa prompted me by asking how many facts I would like to hear, I responded with four, and that kicked off my new NumberOfFacts intent handler!

Stay tuned for my next post where we'll cover adding custom slot types to your skill.

Share this post: