Alexa Skill Development — Part 1

Vivek Navadia
6 min readJan 4, 2019

--

Alexa Skill Development

Amazon Alexa, known simply as Alexa, is a virtual assistant developed by Amazon, first used in the Amazon Echo and the Amazon Echo Dot smart speakers. It is capable of voice interaction, music playback, making to-do lists, setting alarms, streaming podcasts, playing audiobooks, and providing weather, traffic, sports, and other real-time information, such as news. Alexa can also control several smart devices using itself as a home automation system. Users are able to extend the Alexa capabilities by installing “skills”.

Skills are nothing but additional functionality developed by the third party. in another word we can call it as apps.

In this article, we are going to create one basic skill. we will add more advanced features to skill in other stories.

Steps

Create Skill
  • Click on Create to create new skill
Add details to Skill
  • Add Skill Name and select Default language support of your skill. you can add more language support later on. Amazon provides multiple models for skill creation. You can select as per your need. We are going to use Custom.
  • Click on Create Skill.
  • Amazon provides different templates for skill creation with pre-defined intents. You can explore these templates to understand different components, but for now, we would like to create from scratch. so select Start from scratch and click on Choose
  • Once you select a Custom template, you will see below configurations on your console to build a model. Only a few of them are important for building a basic model. the rest of them are advanced, will elaborate more on them in other stories.
Custom Skill Configuration
  • Invocation — Amazon Alexa skill starts a conversation with invocation name. so for example, if invocation name is daily horoscopes then a user can start a conversation with Alexa by saying

Ask daily horoscopes for the horoscope for Gemini.

for our example, I have used skill invocation name as pregnancy assistant. so we can start a conversation with Alexa by saying

Hey Alexa, Can you open pregnancy assistant?

Invocation
  • Intents — Intents provides an option to create set utterances that will be used by Alexa to identify what question user has asked and what to respond accordingly based on endpoints configurations
  • There are few required intents available by default for fallback events. A user can create custom intent too by providing a name of intent and set utterances and click on Build Model.
Creating Custom Intent
Setting Utterances

So far we are done with setups that helps Alexa engine to understand how to respond based on user’s input. but we haven’t configured Alexa response content yet. To do that, we have to configure endpoint.

  • Endpoint — The Endpoint will receive POST requests when a user interacts with your Alexa Skill. The request body contains parameters that your service can use to perform logic and generate a JSON-formatted response. There are two options (AWS Lambda, HTTPS) for endpoint configuration. we will use AWS Lambda
  • AWS Lambda — Open AWS Console and go to Lambda service and create a new function
Create a Lambda Function
  • AWS Lambda Function Configuration — It provides an option to create a function from scratch OR select from boilerplate. We are going to create function scratch so select an appropriate option.
  • Provide function name, runtime environment (we are going to use Node.JS) and choose the role. Lambda service requires a specific role to execute the function. so create a role if you don't have an existing one and create function.
Create function from scratch
  • Once you have created a function, you will have access of editor and can write different routines to build response accordingly. Following are few mandatory code block to run this sample.
  • Following code block shows the complete code to get a response from backend based on the queries to Alexa. Alternatively, you can clone repository as well.
const Alexa = require('ask-sdk-core');const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
const speechText = 'Welcome to Pregnancy Assistant! You can ask pragnancy related queries';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Pregnancy Assistant', speechText)
})
.getResponse();
},
};
const ExerciseIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'ExerciseIntent';
},
handle(handlerInput) {
const speechText = 'Hi there! Let me tell you this, If you already exercised prior to becoming pregnant, talk with your health care team to see if you should continue or change your program. If you did not exercise prior to becoming pregnant, now is not the time to begin a vigorous routine. Discuss all exercise options with your health care team.';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Exercise - Pregnancy Assistant', speechText)
})
.getResponse();
},
};
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
const speechText = 'You can ask pragnancy related queries.';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard('Help', speechText)
})
.getResponse();
},
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
const speechText = 'Goodbye!';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard('Cancle And Stop', speechText)
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.speak('Sorry, I can\'t understand the command. Please say again.')
.reprompt('Sorry, I can\'t understand the command. Please say again.')
.getResponse();
},
};
const skillBuilder = Alexa.SkillBuilders.custom();exports.handler = skillBuilder
.addRequestHandlers(
LaunchRequestHandler,
ExerciseIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();
  • Next step is to add triggers in AWS Lambda that. To do that select AWS Skill Kit from the left panel and add to AWS Lambda designer view.
Alexa Skill Kit Trigger

Please note that Alexa Skill kit is NOT available on all region as of now.

  • Trigger added to AWS Lambda is to ensure bridge between Alexa engine and AWS Lambda function.
  • Check the top right corner of AWS console, you will find ARN as displayed below. copy it.
  • Go to Alexa developer console and open Endpoint configuration and paste copied ARN

Please note that you have to paste ARN in as per region in which AWS lambda function is created.

Endpoint Configuration
  • Now, Copy Skill ID (first line in the above image) and go to Alexa Skill Set configuration in AWS Lambda and paste it

Save configuration and we are done with our first Alexa skill. Now its time to test.

  • Go to Alexa developer console and open Test section. Start your conversation with an invocation.
Simulator to Test

Hope this step by step guide will help. Will cover more advanced sections and APL (Alexa Presentation Language) in the next part.

--

--

Vivek Navadia

Engineering Manager@digicorp, A Master of Computers and Bachelor of Physics by Education, loves cooking and photography. Gymaholic and Marathoner.