Slots in Alexa and How to Use Them

Introduction 

 
This blog is about declaring and using Alexa slots. You will see a model for the declaration of slot and lambda code to read slot values.
 

What are slots?

 
Slots are variables that are embedded to activate a skill. Amazon provides inbuilt and custom slots.
 
Slot types are divided into the below three categories:
  1. Numbers, Dates, and Times: Convert the user’s utterance into data types such as numbers and dates.
  2. Phrases: Allow for input from a user with fewer constraints on format and content.
  3. List of Items: Represents a list of items. These slot types depend on the language selected for your skill.

Code

 
I have used Visual Studio Code for development of this skill where I have setup the Alexa Skill Kit toolkit extension to make development and deployment easier.
 
Below is the model code which invokes RollNumberIntent. The name of the slot is the rollNumber of number type which is embedded in an utterance sample.
  1. {  
  2.     "name""RollNumberIntent",  
  3.     "slots": [{  
  4.         "name""rollNumber",  
  5.         "type""AMAZON.NUMBER"  
  6.     }],  
  7.     "samples": ["my roll number is {rollNumber}""roll number is {rollNumber}""roll number {rollNumber}"]  
  8. }  
Now, let’s access the value of declared slot rollNumberin lambda:
  1. const RollNumberIntentHandler = {  
  2.     canHandle(handlerInput) {  
  3.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'RollNumberIntentHandler ';  
  4.     },  
  5.     async handle(handlerInput) {  
  6.         const rollNumber = handlerInput.requestEnvelope.request.intent.slots.rollNumber.value;  
  7.         return handlerInput.responseBuilder.speak(rollNumber).getResponse();  
  8.     },  
  9. };  

Conclusion

 
Amazon provides a wide variety of slot type references in a simple and clear manner. Alexa has multilanguage support, so slot values will be based on selected locale. For example, the slot type AMAZON.FirstName will be based on the selected local.
 
References