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.
An alexa skill is like a simple project that can be made on Amazon's developer console. I first came to know about this process in a workshop and in the matter of only 10-12 minutes, I was able to create my first skill - It is called UCL Result, alexa speaks out the UCL Final Result in this like 'Liverpool won by 2 goals' or 'Tottenham lost by 2 goals'.
So lets get started on how to build a Simple Alexa Skill:
Step 1: Register yourself on Amazon developers website or Log onto the console if you already have an account. This is how the console login page looks:
Step 2: Navigate to Alexa Skill Section and this is what you will see
Step 3: Click on Create Skill. In 'choose a model' select custom and in 'method to host' select Alexa-Hosted. Then click on Create Skill.
Step 4: When you are navigated to the Alexa developer console next, as you can see in the screenshot I have created an Intent called result. So click on ADD(+) sign and add an Intent.
Step 5: Now you will see a column to enter Utterances. Here add those sentences which will target a response from Alexa. Then click on Save Model.
Step 6: Now you need to write some code in JavaScript. Go to the Code option in the menu bar above and click on it. Select index.js file. The functions you see are the 'intent handlers' and they are triggered when you request a skill from Alexa. So we need to create our own Intent Handler then, code is given below, you can compare it to other intent handlers to see what all things we have changed.
const resultHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'result';
},
handle(handlerInput) {
const speechText = response[Math.floor(Math.random()*4)];
return handlerInput.responseBuilder
.speak(speechText)
//.reprompt('add a reprompt if you want to keep the session open for the user to respond')
.getResponse();
}
};
The variable 'response' is an array describes as:
const response = ['liverpool won', 'tottenham lost', 'liverpool won by 2 goals', 'tottenham lost by 2 goals'];
response is declared above in the file index.html just below Alexa's declaration().
const Alexa = require('ask-sdk-core');
const response = ['liverpool won', 'tottenham lost', 'liverpool won by 2 goals', 'tottenham lost by 2 goals'];
The last thing to do in the code is to make the entry of newly created Intent Handler below in 'exports.handler' like this
Step 8: Click on Save. Go to Build section and click on 'Build Model'. Then on Code section click on 'Deploy'. Thats it! You are ready to test your skill now. Make sure you Build your Model and Deploy the code everytime you make any changes.
Now click on Test. On the left panel say 'Alexa, open *intent name*'
Now ask any utterance you entered previously
Thats all! You have learned a new skill.
Noice