Introduction to Amazon Alexa and Alexa skill
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 developed by Amazon Lab126. 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” (additional functionality developed by third-party vendors, in other settings more commonly called apps such as weather programs and audio features).
Prerequisites for developing Alexa skill
It is obvious that you should have an amazon developers account. And you must have AWS Lamda for the easiest backend support.
Steps to follow for your Alexa skill
Give the skill a unique name with a default language. You can choose the model to add to your skill and multiple methods to host your skill’s backend resources but I would prefer you to go with the custom that will be mentioned as custom and provision your own respectively.
Choose a template
Go for the Start from scratch
The screen will look like this once click on start from scratch and the browse completes.
Lambda Functions
Now go to the aws.amazon and create an AWS Lambda function.
Here in its index.js file, we have an Intent named GetNewFactIntent that will contain our data on what Alexa will respond to our queries. And in the const data array, we can add the facts.
/* eslint-disable func-names */ | |
/* eslint-disable no-console */ | |
const Alexa = require('ask-sdk'); | |
const GetNewFactHandler = { | |
canHandle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
return request.type === 'LaunchRequest' | |
|| (request.type === 'IntentRequest' | |
&& request.intent.name === 'GetNewFactIntent'); | |
}, | |
handle(handlerInput) { | |
const factArr = data; | |
const factIndex = Math.floor(Math.random() * factArr.length); | |
const randomFact = factArr[factIndex]; | |
const speechOutput = GET_FACT_MESSAGE + randomFact; | |
return handlerInput.responseBuilder | |
.speak(speechOutput) | |
.withSimpleCard(SKILL_NAME, randomFact) | |
.getResponse(); | |
}, | |
}; | |
const HelpHandler = { | |
canHandle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
return request.type === 'IntentRequest' | |
&& request.intent.name === 'AMAZON.HelpIntent'; | |
}, | |
handle(handlerInput) { | |
return handlerInput.responseBuilder | |
.speak(HELP_MESSAGE) | |
.reprompt(HELP_REPROMPT) | |
.getResponse(); | |
}, | |
}; | |
const ExitHandler = { | |
canHandle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
return request.type === 'IntentRequest' | |
&& (request.intent.name === 'AMAZON.CancelIntent' | |
|| request.intent.name === 'AMAZON.StopIntent'); | |
}, | |
handle(handlerInput) { | |
return handlerInput.responseBuilder | |
.speak(STOP_MESSAGE) | |
.getResponse(); | |
}, | |
}; | |
const SessionEndedRequestHandler = { | |
canHandle(handlerInput) { | |
const request = handlerInput.requestEnvelope.request; | |
return 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, an error occurred.') | |
.reprompt('Sorry, an error occurred.') | |
.getResponse(); | |
}, | |
}; | |
const SKILL_NAME = 'snake facts'; | |
const GET_FACT_MESSAGE = 'Here\'s your fact: '; | |
const HELP_MESSAGE = 'You can say tell me a snake facts, or, you can say exit... What can I help you with?'; | |
const HELP_REPROMPT = 'What can I help you with?'; | |
const STOP_MESSAGE = 'Goodbye!'; | |
const data = [ | |
'Snakes are carnivores (meat eaters).', | |
'Snakes don’t have eyelids.', | |
'Snakes can’t bite food so have to swallow it whole.', | |
'Snakes have flexible jaws which allow them to eat prey bigger than their head!', | |
'Snakes are found on every continent of the world except Antarctica.', | |
'Snakes have internal ears but not external ones.', | |
'Snakes used in snake charming performances respond to movement, not sound.', | |
'There are around 3000 different species of snake.', | |
'Snakes have a unique anatomy which allows them to swallow and digest large prey.', | |
'Snakes are covered in scales.', | |
'Snake skin is smooth and dry', | |
'Snakes shed their skin a number of times a year in a process that usually lasts a few days.', | |
'Snakes smell with their tongue.', | |
]; | |
const skillBuilder = Alexa.SkillBuilders.standard(); | |
exports.handler = skillBuilder | |
.addRequestHandlers( | |
GetNewFactHandler, | |
HelpHandler, | |
ExitHandler, | |
SessionEndedRequestHandler | |
) | |
.addErrorHandlers(ErrorHandler) | |
.lambda(); |
Copy ARN number
Copy the ARN number in the top right corner and paste it to the endpoint in the Alexa skill console. Now go to Intent and add an Intent with the same name GetNewFactIntent to sync the array facts and give some sample utterances related to your skill. I developed the skill Interesting facts of Bihar that is why is used this array of words repeatedly in my sample utterances.
Sample utterances
after filling up for the required utterances just save and build your model and then you to the Test section.
Test your skill
Test your skill for multiple times to get rest assured for the quality of your Amazon skill. After that go to distribution, all the other requirements are basic and I am sure that will do it if still, you find any issue then you can mail me or ask me in the response section below.
Here is the link to my Amazon Alexa skill https://amzn.to/2pS9J2e
Top comments (0)