Create A Custom Skill To Extract Values From A Slot In Alexa

Introduction

 
In this article, I am going to demonstrate how to create a custom Alexa skill on Alexa Hosted node.js server to extract slot values for different slots defined in an interaction model of an Alexa skill. For that, I will be creating a custom Alexa skill named getting input. With the help of this skill, Users can tell Alexa about themselves. The backend code required for the Alexa skill to work will be stored in a lambda function inside the Amazon developer console.
 

Creating Custom Alexa Skill

 
To create a new skill, first we need to login into the Alexa developer console, we need to mention the unique skill name and select the default language according to our location.
 
 
 
After that, we can choose a model to add to our skill. To create a custom skill, we can select custom model.
 
 
 
We can also choose a method or a template to host the skill’s backend code inside a lambda function.
 
 
 
We can choose Alexa hosted node.js or python template. We can also mention our own endpoint or server to store backend resources for the required Alexa skills.
 
The next step is to choose a template to add to our skill, which we customize later according to our need and click on the create skill button. Now as a skill has been created, we need to make adjustments to the skill’s frontend. Now I will be creating intents, slots, and custom slot types to create skill’s frontend.
 
First, we need to mention the invocation name. Users say a skill's invocation name to begin an interaction with a particular custom skill.
 
 
 
Now we have to create intents:
 
 
 
Here, I have added a new intent named MyNameIsIntent along with a sample utterance. Inside the utterance “{name}” and “{name} is my name”, name is a slot defined for this skill. Slots are defined within curly brackets.
 
Slots and slot type of each slot is defined as follows:
 
 
 
Here Amazon.FirstName is a pre-defined slot created for the above-mentioned intent.
 
After creating a model for a particular skill, we can save and build the model by clicking on save model and build model button on the top.
 
Json code for the interaction model is as follows,
  1. {  
  2.     "interactionModel": {  
  3.         "languageModel": {  
  4.             "invocationName""getting input",  
  5.             "modelConfiguration": {  
  6.                 "fallbackIntentSensitivity": {  
  7.                     "level""LOW"  
  8.                 }  
  9.             },  
  10.             "intents": [  
  11.                 {  
  12.                     "name""AMAZON.NavigateHomeIntent",  
  13.                     "samples": []  
  14.                 },  
  15.                 {  
  16.                     "name""AMAZON.CancelIntent",  
  17.                     "samples": []  
  18.                 },  
  19.                 {  
  20.                     "name""AMAZON.HelpIntent",  
  21.                     "samples": []  
  22.                 },  
  23.                 {  
  24.                     "name""AMAZON.StopIntent",  
  25.                     "samples": []  
  26.                 },  
  27.                 {  
  28.                     "name""AMAZON.FallbackIntent",  
  29.                     "samples": []  
  30.                 },  
  31.                 {  
  32.                     "name""MyNameIsIntent",  
  33.                     "slots": [  
  34.                         {  
  35.                             "name""name",  
  36.                             "type""AMAZON.FirstName"  
  37.                         }  
  38.                     ],  
  39.                     "samples": [  
  40.                         "{name}",  
  41.                         "{name} is my name",  
  42.                         "my name is {name}",  
  43.                         "i am {name}",  
  44.                         "you can call me {name}"  
  45.                     ]  
  46.                 }  
  47.             ],  
  48.             "types": []  
  49.         }  
  50.     }  
  51. }  

Creating the backend resource for the Alexa skill

 
To create backend code inside the lambda function, we can write code inside the index.js node.js file. The code for the custom Alexa skill is as follows:
  1. const Alexa = require('ask-sdk-core');  
  2.   
  3.     
  4. const LaunchRequestHandler = {  
  5.   canHandle(handlerInput) {  
  6.     return handlerInput.requestEnvelope.request.type === 'LaunchRequest';  
  7.   },  
  8.   handle(handlerInput) {  
  9.    const speechText = 'Hi, What is your name?';  
  10.    const repromptText = 'can you tell me your name?';  
  11.   
  12.       
  13.     return handlerInput.responseBuilder  
  14.       .speak(speechText)  
  15.       .reprompt(repromptText)  
  16.       .getResponse();  
  17.   },  
  18. };  
  19.   
  20. const MyNameIsIntentHandler = {  
  21.   canHandle(handlerInput) {  
  22.     return handlerInput.requestEnvelope.request.type === 'IntentRequest'  
  23.       && handlerInput.requestEnvelope.request.intent.name === 'MyNameIsIntent';  
  24.   },  
  25.   handle(handlerInput) {  
  26.       
  27.   
  28.     const userName = handlerInput.requestEnvelope.request.intent.slots.name.value;  
  29.     const speechText = `Hello ${userName}. It is nice to meet you.`;  
  30.        
  31.   
  32.     return handlerInput.responseBuilder  
  33.       .speak(speechText)  
  34.       .getResponse();  
  35.   },  
  36. };  
  37.   
  38.   
  39.   
  40.   
  41. const HelpIntentHandler = {  
  42.   canHandle(handlerInput) {  
  43.     return handlerInput.requestEnvelope.request.type === 'IntentRequest'  
  44.       && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';  
  45.   },  
  46.   handle(handlerInput) {  
  47.       
  48.   
  49.     const speakOutput ='You can introduce yourself by telling me your name.';  
  50.       
  51.   
  52.     return handlerInput.responseBuilder  
  53.       .speak(speakOutput)  
  54.       .reprompt(speakOutput)  
  55.       .getResponse();  
  56.   },  
  57. };  
  58.   
  59. const CancelAndStopIntentHandler = {  
  60.   canHandle(handlerInput) {  
  61.     return handlerInput.requestEnvelope.request.type === 'IntentRequest'  
  62.       && (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'  
  63.         || handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');  
  64.   },  
  65.   handle(handlerInput) {  
  66.       
  67.   
  68.     const speakOutput = 'Goodbye!';  
  69.   
  70.     return handlerInput.responseBuilder  
  71.       .speak(speakOutput)  
  72.       .getResponse();  
  73.   },  
  74. };  
  75.   
  76. const SessionEndedRequestHandler = {  
  77.   canHandle(handlerInput) {  
  78.     return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';  
  79.   },  
  80.   handle(handlerInput) {  
  81.     console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);  
  82.   
  83.     return handlerInput.responseBuilder.getResponse();  
  84.   },  
  85. };  
  86.   
  87.   
  88. const ErrorHandler = {  
  89.   canHandle() {  
  90.     return true;  
  91.   },  
  92.   handle(handlerInput, error) {  
  93.     console.log(`Error handled: ${error.message}`);  
  94.       
  95.   
  96.     return handlerInput.responseBuilder  
  97.       .speak('Sorry I can\'t understand the command. please say again.')  
  98.       .reprompt('Sorry I can\'t understand the command. please say again.')  
  99.       .getResponse();  
  100.   },  
  101. };  
  102.   
  103.   
  104.   
  105.   
  106. const skillBuilder = Alexa.SkillBuilders.custom();  
  107.   
  108. exports.handler = skillBuilder  
  109.   .addRequestHandlers(  
  110.       
  111.     LaunchRequestHandler,  
  112.     MyNameIsIntentHandler,  
  113.       
  114.     HelpIntentHandler,  
  115.     CancelAndStopIntentHandler,  
  116.     SessionEndedRequestHandler,  
  117.       
  118.   )  
  119.     
  120.   .addErrorHandlers(ErrorHandler)  
  121.     
  122.   .lambda();  
To receive request from the user, request handlers are created for each intent to handle. Inside each handlers, canHandle and handle functions are written.
 
The canHandle() function is where you define what requests the handler responds to. The handle() function returns a response to the user. If your skill receives a request, the canHandle() function within each handler determines whether or not that handler can service the request.
 
In this case, the user wants to launch the skill, which is a LaunchRequest. Therefore, the canHandle() function within the LaunchRequestHandler will let the SDK know it can fulfill the request. In computer terms, the canHandle returns true to confirm it can do the work.
 
After that, MyNameIsIntentHandler is defined to handle each and every request of user to tell Alexa about themselves. This handler will receive Name as requested slot parameter and extract the slot value mention by the user and prompts a dialog mentioning user name accordingly.
 
Output
 
 
 
As we can see from the above output, a user can invoke the skill by saying just the invocation name which is getting input. Then Alexa greets user asks for his/her name. As soon as the user mentions his/her name, Alexa extracts the slot value from the slot and prompts a message saying “Hello Abhishek. It is nice to meet you”.
 

Summary

 
In this article, I created a custom Alexa skill. I also defined intents, slots and custom slot types for each slot. I demonstrated how to extract slot values for different slots defined in an interaction model of an Alexa skill. Proper coding snippets along with the output for the backend of the skill are also provided. 


Similar Articles