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 skill’s front end. 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.
Here I have added a new intent named ShowBirthdayIntent along with a sample utterance. Inside the utterance “my birthday is on {month} {day} {year}”, month, day and year are slots defined for the skill. Slots are defined within curly brackets.
The slot type of each slot is defined as follows,
Day and year have been assigned pre-defined slot types whereas month is assigned a custom slot type.
Custom slot type can 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 validation rules for each and every slots being created. To create a validation rule we can choose a validator such as Accept only slot type’s values and synonyms. We can also specify what alexa will say to ask the user for an acceptable value.
The json code for the above frontend is as follows,
- {
- "interactionModel": {
- "languageModel": {
- "invocationName": "birthday night",
- "intents": [
- {
- "name": "AMAZON.CancelIntent",
- "samples": []
- },
- {
- "name": "AMAZON.HelpIntent",
- "samples": []
- },
- {
- "name": "AMAZON.StopIntent",
- "samples": []
- },
- {
- "name": "AMAZON.NavigateHomeIntent",
- "samples": []
- },
- {
- "name": "ShowBirthdayIntent",
- "slots": [
- {
- "name": "month",
- "type": "MonthType"
- },
- {
- "name": "day",
- "type": "AMAZON.Ordinal"
- },
- {
- "name": "year",
- "type": "AMAZON.FOUR_DIGIT_NUMBER"
- }
- ],
- "samples": [
- "my birthday is on {month} {day} {year}"
- ]
- }
- ],
- "types": [
- {
- "name": "MonthType",
- "values": [
- {
- "name": {
- "value": "december"
- }
- },
- {
- "name": {
- "value": "november"
- }
- },
- {
- "name": {
- "value": "october"
- }
- },
- {
- "name": {
- "value": "september"
- }
- },
- {
- "name": {
- "value": "august"
- }
- },
- {
- "name": {
- "value": "july"
- }
- },
- {
- "name": {
- "value": "june"
- }
- },
- {
- "name": {
- "value": "may"
- }
- },
- {
- "name": {
- "value": "april"
- }
- },
- {
- "name": {
- "value": "march"
- }
- },
- {
- "name": {
- "value": "february"
- }
- },
- {
- "name": {
- "value": "january"
- }
- }
- ]
- }
- ]
- },
- "dialog": {
- "intents": [
- {
- "name": "ShowBirthdayIntent",
- "confirmationRequired": false,
- "prompts": {},
- "slots": [
- {
- "name": "month",
- "type": "MonthType",
- "confirmationRequired": false,
- "elicitationRequired": false,
- "prompts": {},
- "validations": [
- {
- "type": "hasEntityResolutionMatch",
- "prompt": "Slot.Validation.477931298699.1338914932885.605682662604"
- }
- ]
- },
- {
- "name": "day",
- "type": "AMAZON.Ordinal",
- "confirmationRequired": false,
- "elicitationRequired": false,
- "prompts": {}
- },
- {
- "name": "year",
- "type": "AMAZON.FOUR_DIGIT_NUMBER",
- "confirmationRequired": false,
- "elicitationRequired": false,
- "prompts": {}
- }
- ]
- }
- ],
- "delegationStrategy": "ALWAYS"
- },
- "prompts": [
- {
- "id": "Slot.Validation.477931298699.1338914932885.605682662604",
- "variations": [
- {
- "type": "PlainText",
- "value": "please mention correct month name"
- }
- ]
- }
- ]
- }
- }
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 functionality, we can write source code inside index.js node.js file under lambda. The code for the custom Alexa skill is as follows,
-
-
-
- const Alexa = require('ask-sdk-core');
-
- const LaunchRequestHandler = {
- canHandle(handlerInput) {
- return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
- },
- handle(handlerInput) {
- const speakOutput = 'Hello! Welcome to birthday night. When is your birthday?';
- const repromptText = 'I was born Nov. 6th, 2014. When were you born?';
- return handlerInput.responseBuilder
- .speak(speakOutput)
- .reprompt(repromptText)
- .getResponse();
- }
- };
- const ShowBirthdayIntentHandler = {
- canHandle(handlerInput) {
- return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
- && Alexa.getIntentName(handlerInput.requestEnvelope) === 'ShowBirthdayIntent';
- },
- handle(handlerInput) {
-
- const year = handlerInput.requestEnvelope.request.intent.slots.year.value;
-
- const month = handlerInput.requestEnvelope.request.intent.slots.month.value;
-
- const day = handlerInput.requestEnvelope.request.intent.slots.day.value;
-
- const speakOutput = `Thanks, I'll remember that you were born ${month} ${day} ${year}.`;
- return handlerInput.responseBuilder
- .speak(speakOutput)
-
- .getResponse();
- }
- };
- const HelpIntentHandler = {
- canHandle(handlerInput) {
- return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
- && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
- },
- handle(handlerInput) {
- const speakOutput = 'You can say hello to me! How can I help?';
-
- return handlerInput.responseBuilder
- .speak(speakOutput)
- .reprompt(speakOutput)
- .getResponse();
- }
- };
- const CancelAndStopIntentHandler = {
- canHandle(handlerInput) {
- return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
- && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
- || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
- },
- handle(handlerInput) {
- const speakOutput = 'Goodbye!';
- return handlerInput.responseBuilder
- .speak(speakOutput)
- .getResponse();
- }
- };
- const SessionEndedRequestHandler = {
- canHandle(handlerInput) {
- return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
- },
- handle(handlerInput) {
-
- return handlerInput.responseBuilder.getResponse();
- }
- };
-
-
-
-
-
- const IntentReflectorHandler = {
- canHandle(handlerInput) {
- return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
- },
- handle(handlerInput) {
- const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
- const speakOutput = `You just triggered ${intentName}`;
-
- return handlerInput.responseBuilder
- .speak(speakOutput)
-
- .getResponse();
- }
- };
-
-
-
-
- const ErrorHandler = {
- canHandle() {
- return true;
- },
- handle(handlerInput, error) {
- console.log(`~~~~ Error handled: ${error.stack}`);
- const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;
-
- return handlerInput.responseBuilder
- .speak(speakOutput)
- .reprompt(speakOutput)
- .getResponse();
- }
- };
-
-
-
-
- exports.handler = Alexa.SkillBuilders.custom()
- .addRequestHandlers(
- LaunchRequestHandler,
- ShowBirthdayIntentHandler,
- HelpIntentHandler,
- CancelAndStopIntentHandler,
- SessionEndedRequestHandler,
- IntentReflectorHandler,
- )
- .addErrorHandlers(
- ErrorHandler,
- )
- .lambda();
To receive requests 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.
After that ShowBirthdayIntentHandler is defined to handle each and every user’s birthday request.
This handler will receive day, month and year of user’s birthday as request parameter and give response accordingly.
Output
If user fails to mention valid month name while making a request to Alexa to store a user’s birthdate then Alexa will throw an error message and ask the user to provide a valid month name.
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.