Creating Custom Restaurant Reservation Alexa Skill Using Alexa Hosted NodeJS

Introduction

 
Here is another article on custom Alexa skills. In this article I am going to demonstrate how to create a custom Alexa skill on Alexa Hosted node.js server. For that I will be creating a custom Alexa skill named Eating House. With the help of this skill, users can ask Alexa to book restaurants in advance.
 
The users can tell the timing, day, number of people to book for reservations and the name of the person under whom the booking has to be done. The backend code required for the Alexa skill to work will be stored in a lambda function inside the Amazon developer console. To know the basics on what components are needed to create an Alexa skill you can visit my previous article.
 

Creating Alexa Skill

 
To create a new skill, first we need to log in 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 lamda 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 create skill button.
 
Now as the 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 the 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 CreateReservation Intent along with a sample utterance. Inside the utterance “I want to make {ReservationDate} at {ReservationTime} for {NumberOfPeople}”, ReservationDate, ReservationTime and NumberOfPeople are slots defined for the skill. Slots are defined within curly brackets.
 
Slots and slot type of each slot is defined as follows:
 
 
 
Here NumberOfPeople, ReservationDate, ReservationTime and NumberOfPeople are slots created for CreateReservation intent.
Custom slot type can also be created by clicking on Add Slot Type button under slot types menu. To create a custom slot type, type the name of the custom slot also fill in the required slot values along with Id and synonyms.
 
We can also create dialogs such as Alexa speech prompts and user utterances for each and every slot being created. When the slot required to fulfill the intent is not mentioned in the sample utterance of a user then dialogs are created to ask users to mention the slot values to fulfill the intent.
 
Here Alexa speech prompt is whatever Alexa will say to prompt the user to fill a particular slot and user utterances are whatever user might say in response to Alexa prompts.
 
To create a dialog under each slot
  • Enable option “is this slot required to fulfil the intent”.
  • Type Alexa speech prompts and user utterances accordingly.
 
 
 
The above dialogs were created for NumberOfPeople slot.
 
The json code for the frontend part is as follows,
  1. {  
  2.     "interactionModel": {  
  3.         "languageModel": {  
  4.             "invocationName""eating house",  
  5.             "intents": [  
  6.                 {  
  7.                     "name""AMAZON.CancelIntent",  
  8.                     "samples": []  
  9.                 },  
  10.                 {  
  11.                     "name""AMAZON.HelpIntent",  
  12.                     "samples": []  
  13.                 },  
  14.                 {  
  15.                     "name""AMAZON.StopIntent",  
  16.                     "samples": []  
  17.                 },  
  18.                 {  
  19.                     "name""AMAZON.NavigateHomeIntent",  
  20.                     "samples": []  
  21.                 },  
  22.                 {  
  23.                     "name""CreateReservation",  
  24.                     "slots": [  
  25.                         {  
  26.                             "name""NumberOfPeople",  
  27.                             "type""AMAZON.NUMBER",  
  28.                             "samples": [  
  29.                                 "there will be {NumberOfPeople} people"  
  30.                             ]  
  31.                         },  
  32.                         {  
  33.                             "name""ReservationDate",  
  34.                             "type""AMAZON.DATE",  
  35.                             "samples": [  
  36.                                 "make reservation on {ReservationDate}"  
  37.                             ]  
  38.                         },  
  39.                         {  
  40.                             "name""ReservationTime",  
  41.                             "type""AMAZON.TIME",  
  42.                             "samples": [  
  43.                                 "i want to book at {ReservationTime}"  
  44.                             ]  
  45.                         },  
  46.                         {  
  47.                             "name""PersonName",  
  48.                             "type""AMAZON.FirstName",  
  49.                             "samples": [  
  50.                                 "{PersonName}"  
  51.                             ]  
  52.                         }  
  53.                     ],  
  54.                     "samples": [  
  55.                         "please make reservation for {NumberOfPeople} people  on {ReservationDate} at {ReservationTime} for name {PersonName}",  
  56.                         "i want to make reservation on {ReservationDate} at {ReservationTime} for {NumberOfPeople} people ",  
  57.                         "i want to make a reservation for {NumberOfPeople} people",  
  58.                         "i want to make a reservation"  
  59.                     ]  
  60.                 }  
  61.             ],  
  62.             "types": []  
  63.         },  
  64.         "dialog": {  
  65.             "intents": [  
  66.                 {  
  67.                     "name""CreateReservation",  
  68.                     "confirmationRequired"false,  
  69.                     "prompts": {},  
  70.                     "slots": [  
  71.                         {  
  72.                             "name""NumberOfPeople",  
  73.                             "type""AMAZON.NUMBER",  
  74.                             "confirmationRequired"false,  
  75.                             "elicitationRequired"true,  
  76.                             "prompts": {  
  77.                                 "elicitation""Elicit.Slot.735905142355.536259982955"  
  78.                             }  
  79.                         },  
  80.                         {  
  81.                             "name""ReservationDate",  
  82.                             "type""AMAZON.DATE",  
  83.                             "confirmationRequired"false,  
  84.                             "elicitationRequired"true,  
  85.                             "prompts": {  
  86.                                 "elicitation""Elicit.Slot.735905142355.431728293953"  
  87.                             }  
  88.                         },  
  89.                         {  
  90.                             "name""ReservationTime",  
  91.                             "type""AMAZON.TIME",  
  92.                             "confirmationRequired"false,  
  93.                             "elicitationRequired"true,  
  94.                             "prompts": {  
  95.                                 "elicitation""Elicit.Slot.735905142355.1527575461050"  
  96.                             }  
  97.                         },  
  98.                         {  
  99.                             "name""PersonName",  
  100.                             "type""AMAZON.FirstName",  
  101.                             "confirmationRequired"false,  
  102.                             "elicitationRequired"true,  
  103.                             "prompts": {  
  104.                                 "elicitation""Elicit.Slot.735905142355.1189785990153"  
  105.                             }  
  106.                         }  
  107.                     ]  
  108.                 }  
  109.             ],  
  110.             "delegationStrategy""ALWAYS"  
  111.         },  
  112.         "prompts": [  
  113.             {  
  114.                 "id""Elicit.Slot.735905142355.536259982955",  
  115.                 "variations": [  
  116.                     {  
  117.                         "type""PlainText",  
  118.                         "value""How many people will be there?"  
  119.                     }  
  120.                 ]  
  121.             },  
  122.             {  
  123.                 "id""Elicit.Slot.735905142355.431728293953",  
  124.                 "variations": [  
  125.                     {  
  126.                         "type""PlainText",  
  127.                         "value""what is the reservation date?"  
  128.                     }  
  129.                 ]  
  130.             },  
  131.             {  
  132.                 "id""Elicit.Slot.735905142355.1527575461050",  
  133.                 "variations": [  
  134.                     {  
  135.                         "type""PlainText",  
  136.                         "value""at what time?"  
  137.                     }  
  138.                 ]  
  139.             },  
  140.             {  
  141.                 "id""Elicit.Slot.735905142355.1189785990153",  
  142.                 "variations": [  
  143.                     {  
  144.                         "type""PlainText",  
  145.                         "value""under whose name do you want to make the reservation?"  
  146.                     }  
  147.                 ]  
  148.             }  
  149.         ]  
  150.     }  
  151. }  
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.

 
Creating the backend resource for the Alexa skill

 
To create backend code inside lambda function, we can write code inside index.js node.js file. The code for the custom Alexa skill is as follows
  1. // This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).  
  2. // Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,  
  3. // session persistence, api calls, and more.  
  4. const Alexa = require('ask-sdk-core');  
  5.   
  6. const LaunchRequestHandler = {  
  7.     canHandle(handlerInput) {  
  8.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';  
  9.     },  
  10.     handle(handlerInput) {  
  11.         const speakOutput = 'Hello! Welcome to Create Reservation. How can I help you today?';  
  12.         return handlerInput.responseBuilder  
  13.             .speak(speakOutput)  
  14.             .reprompt(speakOutput)  
  15.             .getResponse();  
  16.     }  
  17. };  
  18. const CreateReservationHandler = {  
  19.     canHandle(handlerInput) {  
  20.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'  
  21.             && Alexa.getIntentName(handlerInput.requestEnvelope) === 'CreateReservation';  
  22.     },  
  23.     handle(handlerInput) {  
  24.           
  25.         const NumberOfPeople = handlerInput.requestEnvelope.request.intent.slots.NumberOfPeople.value;  
  26.   
  27.         const ReservationDate = handlerInput.requestEnvelope.request.intent.slots.ReservationDate.value;  
  28.   
  29.         const ReservationTime = handlerInput.requestEnvelope.request.intent.slots.ReservationTime.value;  
  30.           
  31.         const PersonName = handlerInput.requestEnvelope.request.intent.slots.PersonName.value;  
  32.           
  33.         const speakOutput = `Thanks, the reservation has been done under ${PersonName} for ${NumberOfPeople} people at ${ReservationTime} on ${ReservationDate}.`;  
  34.         return handlerInput.responseBuilder  
  35.             .speak(speakOutput)  
  36.             //.reprompt('add a reprompt if you want to keep the session open for the user to respond')  
  37.             .getResponse();  
  38.     }  
  39. };  
  40. const HelpIntentHandler = {  
  41.     canHandle(handlerInput) {  
  42.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'  
  43.             && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';  
  44.     },  
  45.     handle(handlerInput) {  
  46.         const speakOutput = 'You can say hello to me! How can I help?';  
  47.   
  48.         return handlerInput.responseBuilder  
  49.             .speak(speakOutput)  
  50.             .reprompt(speakOutput)  
  51.             .getResponse();  
  52.     }  
  53. };  
  54. const CancelAndStopIntentHandler = {  
  55.     canHandle(handlerInput) {  
  56.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'  
  57.             && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'  
  58.                 || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');  
  59.     },  
  60.     handle(handlerInput) {  
  61.         const speakOutput = 'Goodbye!';  
  62.         return handlerInput.responseBuilder  
  63.             .speak(speakOutput)  
  64.             .getResponse();  
  65.     }  
  66. };  
  67. const SessionEndedRequestHandler = {  
  68.     canHandle(handlerInput) {  
  69.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';  
  70.     },  
  71.     handle(handlerInput) {  
  72.         // Any cleanup logic goes here.  
  73.         return handlerInput.responseBuilder.getResponse();  
  74.     }  
  75. };  
  76.   
  77. // The intent reflector is used for interaction model testing and debugging.  
  78. // It will simply repeat the intent the user said. You can create custom handlers  
  79. // for your intents by defining them above, then also adding them to the request  
  80. // handler chain below.  
  81. const IntentReflectorHandler = {  
  82.     canHandle(handlerInput) {  
  83.         return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';  
  84.     },  
  85.     handle(handlerInput) {  
  86.         const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);  
  87.         const speakOutput = `You just triggered ${intentName}`;  
  88.   
  89.         return handlerInput.responseBuilder  
  90.             .speak(speakOutput)  
  91.             //.reprompt('add a reprompt if you want to keep the session open for the user to respond')  
  92.             .getResponse();  
  93.     }  
  94. };  
  95.   
  96. // Generic error handling to capture any syntax or routing errors. If you receive an error  
  97. // stating the request handler chain is not found, you have not implemented a handler for  
  98. // the intent being invoked or included it in the skill builder below.  
  99. const ErrorHandler = {  
  100.     canHandle() {  
  101.         return true;  
  102.     },  
  103.     handle(handlerInput, error) {  
  104.         console.log(`~~~~ Error handled: ${error.stack}`);  
  105.         const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;  
  106.   
  107.         return handlerInput.responseBuilder  
  108.             .speak(speakOutput)  
  109.             .reprompt(speakOutput)  
  110.             .getResponse();  
  111.     }  
  112. };  
  113.   
  114. // The SkillBuilder acts as the entry point for your skill, routing all request and response  
  115. // payloads to the handlers above. Make sure any new handlers or interceptors you've  
  116. // defined are included below. The order matters - they're processed top to bottom.  
  117. exports.handler = Alexa.SkillBuilders.custom()  
  118.     .addRequestHandlers(  
  119.         LaunchRequestHandler,  
  120.         CreateReservationHandler,  
  121.         HelpIntentHandler,  
  122.         CancelAndStopIntentHandler,  
  123.         SessionEndedRequestHandler,  
  124.         IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers  
  125.     )  
  126.     .addErrorHandlers(  
  127.         ErrorHandler,  
  128.     )  
  129.     .lambda();  
To receive requests from the user, request handlers are created for each intent to handle.
 
Inside each handler, 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 CreateReservationHandler is defined to handle each and every user’s reservation request. This handler will receive NumberOfPeople, ReservationDate, ReservationTime and PersonName as requested slot parameters and give response accordingly.
 
Output
 
 
If a user fails to mention any of the slot values while making a request to Alexa to reservation then Alexa will prompt messages and asks the user to provide specific slot values as well.
 

Summary

 
In this article, I created a custom Alexa skill. I also defined intents, slots and custom slot types for each slot. I demonstrated the method to create custom slot types and slot type values. Proper coding snippets along with output for backend of skill is also provided.