Creating A Custom Random Dessert 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 Indian Desserts. With the help of this skill, Users can ask Alexa to fetch an Indian dessert, and Alexa will fetch a dessert at random and give information about that particular dessert.
 
Each time, Alexa asks if the user wants to fetch anymore dessert. The users can give a response by saying yes or no accordingly. If a user responds by saying no, then Alexa will prompt a goodbye message. The backend code required for the Alexa skill to work will be stored in a lambda function inside the Amazon developer console.
 

Creating an 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 a 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 create a 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 GetDessert along with some sample utterances such as give me a dessert, please fetch me a dessert. No slots and custom slot types are defined for this Alexa skill.
 
After creating a model for a particular skill, we can save and build the model by clicking on the save model and build the model button on the top.
 
Json code for the above frontend is as follows:
  1. {  
  2.     "interactionModel": {  
  3.         "languageModel": {  
  4.             "invocationName""indian desserts",  
  5.             "modelConfiguration": {  
  6.                 "fallbackIntentSensitivity": {  
  7.                     "level""LOW"  
  8.                 }  
  9.             },  
  10.             "intents": [  
  11.                 {  
  12.                     "name""AMAZON.CancelIntent",  
  13.                     "samples": []  
  14.                 },  
  15.                 {  
  16.                     "name""AMAZON.HelpIntent",  
  17.                     "samples": [  
  18.                         "help"  
  19.                     ]  
  20.                 },  
  21.                 {  
  22.                     "name""AMAZON.StopIntent",  
  23.                     "samples": []  
  24.                 },  
  25.                 {  
  26.                     "name""AMAZON.NavigateHomeIntent",  
  27.                     "samples": []  
  28.                 },  
  29.                 {  
  30.                     "name""AMAZON.FallbackIntent",  
  31.                     "samples": []  
  32.                 },  
  33.                 {  
  34.                     "name""GetDessert",  
  35.                     "slots": [],  
  36.                     "samples": [  
  37.                         "give me a dessert",  
  38.                         "please fetch me a dessert"  
  39.                     ]  
  40.                 },  
  41.                 {  
  42.                     "name""AMAZON.NoIntent",  
  43.                     "samples": [  
  44.                         "no"  
  45.                     ]  
  46.                 },  
  47.                 {  
  48.                     "name""AMAZON.YesIntent",  
  49.                     "samples": [  
  50.                         "yes"  
  51.                     ]  
  52.                 }  
  53.             ],  
  54.             "types": []  
  55.         }  
  56.     }  
  57. }  

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. const GET_DESSERT_MESSAGE = "Here's your dessert: ";  
  4. const HELP_MESSAGE = 'You can say please fetch me a dessert, or, you can say exit... What can I help you with?';  
  5. const HELP_REPROMPT = 'What can I help you with?';  
  6. const STOP_MESSAGE = 'Enjoy the day...Goodbye!';  
  7. const MORE_MESSAGE = 'Do you want more?'  
  8.   
  9.   
  10.   
  11. const data = [  
  12.     'black forest cake  ',  
  13.     'chocolate ice cream ',  
  14.     'chocolate brownie',  
  15.     'chocolate doughnut ',  
  16.     'choco lava cake ',  
  17.     'choc chip muffins',  
  18.     'Hot Chocolate ',  
  19.     'chocolate pudding ',  
  20.     'Dalgona Coffee',  
  21. ];  
  22.   
  23. const LaunchRequestHandler = {  
  24.     canHandle(handlerInput) {  
  25.         return handlerInput.requestEnvelope.request.type === 'LaunchRequest';  
  26.     },  
  27.     handle(handlerInput) {  
  28.         const speechObj = getADessert();  
  29.         return handlerInput.responseBuilder  
  30.             .speak(speechObj.speach)  
  31.             .reprompt(speechObj.reprompt)  
  32.             .getResponse();  
  33.     }  
  34. };  
  35. const GetDessertHandler = {  
  36.     canHandle(handlerInput) {  
  37.         return handlerInput.requestEnvelope.request.type === 'IntentRequest'  
  38.             && handlerInput.requestEnvelope.request.intent.name === 'GetDessert';  
  39.     },  
  40.     handle(handlerInput) {  
  41.       const speechObj = getADessert();  
  42.       return handlerInput.responseBuilder  
  43.           .speak(speechObj.speach)  
  44.           .reprompt(speechObj.reprompt)  
  45.           .getResponse();  
  46.     }  
  47. };  
  48.   
  49. const YesIntentHandler = {  
  50.     canHandle(handlerInput) {  
  51.         return handlerInput.requestEnvelope.request.type === 'IntentRequest'  
  52.             && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.YesIntent';  
  53.     },  
  54.     handle(handlerInput) {  
  55.       const speechObj = getADessert();  
  56.       return handlerInput.responseBuilder  
  57.           .speak(speechObj.speach)  
  58.           .reprompt(speechObj.reprompt)  
  59.           .getResponse();  
  60.     }  
  61. };  
  62.   
  63. const NoIntentHandler = {  
  64.     canHandle(handlerInput) {  
  65.         return handlerInput.requestEnvelope.request.type === 'IntentRequest'  
  66.             && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.NoIntent';  
  67.     },  
  68.     handle(handlerInput) {  
  69.       const speechText = STOP_MESSAGE;  
  70.       return handlerInput.responseBuilder  
  71.           .speak(speechText)  
  72.           .getResponse();  
  73.     }  
  74. };  
  75. const HelpIntentHandler = {  
  76.     canHandle(handlerInput) {  
  77.         return handlerInput.requestEnvelope.request.type === 'IntentRequest'  
  78.             && handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';  
  79.     },  
  80.     handle(handlerInput) {  
  81.         const speechText = HELP_MESSAGE;  
  82.         const speechprompt = HELP_REPROMPT;  
  83.         return handlerInput.responseBuilder  
  84.             .speak(speechText)  
  85.             .reprompt(speechprompt)  
  86.             .getResponse();  
  87.     }  
  88. };  
  89. const CancelAndStopIntentHandler = {  
  90.     canHandle(handlerInput) {  
  91.         return handlerInput.requestEnvelope.request.type === 'IntentRequest'  
  92.             && (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'  
  93.                 || handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');  
  94.     },  
  95.     handle(handlerInput) {  
  96.         const speechText = STOP_MESSAGE;  
  97.         return handlerInput.responseBuilder  
  98.             .speak(speechText)  
  99.             .getResponse();  
  100.     }  
  101. };  
  102. const SessionEndedRequestHandler = {  
  103.     canHandle(handlerInput) {  
  104.         return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';  
  105.     },  
  106.     handle(handlerInput) {  
  107.         // Any cleanup logic goes here.  
  108.         return handlerInput.responseBuilder.getResponse();  
  109.     }  
  110. };  
  111.   
  112.   
  113. const IntentReflectorHandler = {  
  114.     canHandle(handlerInput) {  
  115.         return handlerInput.requestEnvelope.request.type === 'IntentRequest';  
  116.     },  
  117.     handle(handlerInput) {  
  118.         const intentName = handlerInput.requestEnvelope.request.intent.name;  
  119.         const speechText = `You just triggered ${intentName}`;  
  120.   
  121.         return handlerInput.responseBuilder  
  122.             .speak(speechText)  
  123.             .getResponse();  
  124.     }  
  125. };  
  126.   
  127.   
  128. const ErrorHandler = {  
  129.     canHandle() {  
  130.         return true;  
  131.     },  
  132.     handle(handlerInput, error) {  
  133.         console.log(`~~~~ Error handled: ${error.message}`);  
  134.         const speechText = `Sorry, I couldn't understand what you said. Please try again.`;  
  135.   
  136.         return handlerInput.responseBuilder  
  137.             .speak(speechText)  
  138.             .reprompt(speechText)  
  139.             .getResponse();  
  140.     }  
  141. };  
  142.   
  143. function getADessert() {  
  144.   
  145.   const dessertArr = data;  
  146.   const dessertIndex = Math.floor(Math.random() * dessertArr.length);  
  147.   const randomDessert = dessertArr[dessertIndex];  
  148.   const tempOutput = GET_DESSERT_MESSAGE + randomDessert;  
  149.   const speechOutput = tempOutput + MORE_MESSAGE  
  150.   const more = MORE_MESSAGE  
  151.   return {speach: speechOutput, reprompt:more };  
  152. }  
  153.   
  154. exports.handler = Alexa.SkillBuilders.custom()  
  155.     .addRequestHandlers(  
  156.         LaunchRequestHandler,  
  157.         GetDessertHandler,  
  158.         YesIntentHandler,  
  159.         NoIntentHandler,  
  160.         HelpIntentHandler,  
  161.         CancelAndStopIntentHandler,  
  162.         SessionEndedRequestHandler,  
  163.         IntentReflectorHandler)   
  164.     .addErrorHandlers(  
  165.         ErrorHandler)  
  166.     .lambda();  
To receive a request from the user, request handlers are created for each intent to handle. Inside each handlers, canHandle and handle functions are defined.
 
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.
 
With the help of LaunchRequestHandler, Alexa tells the user a random dessert name and also asks the user if he/she wants to know about any more desserts.
 
After that GetDessertHandler is defined to handle each and every user’s fetching dessert request. This handler will enable Alexa to tell users a random dessert name and also asks the user if he/she wants to know about any more desserts.
 
As we can see from the above code that a function getADessert() is defined. Inside the function, an array name containing the name of all the desserts is defined. With the help of random function, a dessert name is extracted from the array and then displayed to the user by Alexa.
 
Output
 
 
As we can see from the output above, to invoke a skill, the user can say Alexa followed by invocation name and sample utterance. Here Indian desserts are an invocation name to invoke this skill and get me a dessert is a sample utterance defined for the skill. Once the skill is invoked, Alexa tells the user a random dessert name and also asks the user if he/she wants to know about any more desserts.
 
If a user responds by saying yes, then Alexa will again tell the user a random dessert name and also asks the user if he/she wants to know about any more desserts.
 
 
If a user responds by saying no, then Alexa will prompt a goodbye message.
 

Summary

 
In this article, I created a custom Alexa skill. I also defined the invocation name, intents, and sample utterances. I demonstrated the method to generate a random dessert name each time a user asks for it. Proper coding snippets along with the output for the backend of skill is also provided. 


Similar Articles