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:
- Numbers, Dates, and Times: Convert the user’s utterance into data types such as numbers and dates.
- Phrases: Allow for input from a user with fewer constraints on format and content.
- 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.
- {
- "name": "RollNumberIntent",
- "slots": [{
- "name": "rollNumber",
- "type": "AMAZON.NUMBER"
- }],
- "samples": ["my roll number is {rollNumber}", "roll number is {rollNumber}", "roll number {rollNumber}"]
- }
Now, let’s access the value of declared slot rollNumberin lambda:
- const RollNumberIntentHandler = {
- canHandle(handlerInput) {
- return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'RollNumberIntentHandler ';
- },
- async handle(handlerInput) {
- const rollNumber = handlerInput.requestEnvelope.request.intent.slots.rollNumber.value;
- return handlerInput.responseBuilder.speak(rollNumber).getResponse();
- },
- };
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