Skip to content

Instantly share code, notes, and snippets.

@harshmandalgi
Last active September 6, 2021 18:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harshmandalgi/7ab9c346a0ad54a6b7f41791aeded1d4 to your computer and use it in GitHub Desktop.
Save harshmandalgi/7ab9c346a0ad54a6b7f41791aeded1d4 to your computer and use it in GitHub Desktop.
Simple Alexa Skill building

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:

Login Page

Step 2: Navigate to Alexa Skill Section and this is what you will see

Dashboard

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.

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.

Add 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.

Add Utterance

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'];

Intent handler code

The last thing to do in the code is to make the entry of newly created Intent Handler below in 'exports.handler' like this

entry of handler

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*'

test1

Now ask any utterance you entered previously

test2

Thats all! You have learned a new skill.

congrats

@harshitlikhar
Copy link

Noice

@harshmandalgi
Copy link
Author

harshmandalgi commented Jun 4, 2019

Noice

;D
arigato gozai mashita senpai des

@harshitlikhar
Copy link

harshitlikhar commented Jun 4, 2019 via email

@IshanPradhan
Copy link

Nice!

@iharshit009
Copy link

have you got the Tshirt

@harshmandalgi
Copy link
Author

have you got the Tshirt

nope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment