top

Develop Alexa skills with Alexa SDK using Node.js

OK, so can you first answer these questions, simple yes/no kind.Do you want to build voice-based applications?Do you see all these Alexa Skill and gets amazed and surprised at the same time, that how these apps can be developed?Do you know a little bit of JavaScript?Do you have 5 minutes?Well, if your answer to the above questions is a “Yes”, today’s your lucky day. Stick to the end, and by the end, you will know how to develop Alexa Skills using Node.js from scratch. In this post, I’m covering Alexa SDK, if you want me to discuss intent schema of Alexa Skills, do let me know in the comment. You can find the complete project on this gist.Let’s start buildingTo create Alexa skill with NodeJS you need two things installed,NodeNPMThis link will get your job done if above are not installed.Once you’ve installed those, choose a directory in your computer, in my case, it’s “my-first-skill”.Once you’re in it via shell, run the following set of commands:npm install --save alexa-sdk npm install -g node-lambda node-lambda setup So, what happened above? We installed two packages from Node package manager. Alexa SDK is to handle intents and node-lambda is for testing and deploying your lambda function.As you can see, setup command have created some files for you. Now, you’ve to go in your .env file and paste the below file and replace the values inside curly brackets with your AWS credentials.AWS_ENVIRONMENT=development AWS_ACCESS_KEY_ID={your-token} AWS_SECRET_ACCESS_KEY={your-secret-token} AWS_ROLE_ARN=AWS_REGION={your-aws-region} AWS_FUNCTION_NAME={your-function-name} AWS_HANDLER=index.handler AWS_MEMORY_SIZE=128 AWS_TIMEOUT=10 AWS_DESCRIPTION= AWS_RUNTIME=nodejs6.10 PACKAGE_DIRECTORY=build CONFIG_FILE=deploy.env DEPLOY_TIMEOUT=3000000 Done? You’re almost getting there!!!Now create an index.js filetouch index.js Go into the index file and paste this content.const Alexa = require('alexa-sdk'); const handlers = {   'LaunchRequest': function () {      this.emit('HelloWorldIntent');   },   'HelloWorldIntent': function () {      this.emit(':tell', 'Hello World!');   } }; exports.handler = function(event, context, callback) {     const alexa = Alexa.handler(event, context, callback);     alexa.registerHandlers(handlers);     alexa.execute(); }; You just created an Alexa Skill, which will say “Hello World!” on getting invoked.Wanna test? Let’s deploy it first.Go to your shell, and deploy your lambda function with the following command: node-lambda deploy If you’ve set your .env file correctly, your lambda console will see a new lambda function. Now, it’s time to go to your developer console.Create a new skill, pass basic information like name and description of skill.You will see in your console that your intent schema has already 4 default skills:FallbackIntentStopIntentCancelIntentHelpIntentCreate a new intent, let’s say “HelloIntent” and save the skill. Now, in lambda endpoints section, copy-paste your Lambda ARN id here. If build passes, go to your test console and invoke your skill by sayingopen <your skill name> Your Skill will respond with “Hello World!”BANG!!! You just created your first skill. Let’s add some more basic intents'AMAZON.StopIntent': function () {     const card = 'Goodbye'     const content = 'Thanks for using the skill'     this.emit(':tellWithCard', content, card, content, null) }, 'AMAZON.CancelIntent': function () {     this.emit('StopIntent') }, 'AMAZON.HelpIntent': function () {     const card = 'Help'     const content = 'Help will always be given at Hogwarts to those who ask for it'     this.emit(':tellWithCard', content, card, content, null) }, 'HelloIntent': function () {     const card = 'Hello!!!'     const content = 'Woah! You created your first custom intent'     this.emit(':tellWithCard', content, card, content, null) },Let’s deploy your lambda function again, and now test your skill with these phrases: Help Stop close cancel hello You will notice, the echo will say the phrases you’ve programmed in your lambda handler. Why not, modify those and play with it a bit :)The more utterances you will have, the better your skill will become. So, try to add more ways that will possibly invoke your skill and improve your skills Voice Experience.Alexa supports SDK for multiple languages: Java, Nodejs, Python, which makes easier for developers to develop skills.There are many resources you can find on Alexa Skills, you can start with the documentation and blogs:Alexa Built in SlotsAlexa Built-in IntentsAn Alexa Based Project for multiple domains (VR, Recommendation system)This was a basic tutorial to learn Alexa Skills using Node JS. If you want to build a really cool Alexa skill with Node JS, you’ve to go in depth and try to play with different options from Alexa console. If you want to contribute to some open source Alexa Skill project, you can start with one of my project: https://github.com/PaytmBuildForIndia/book-engineProject Github Link: https://gist.github.com/myke11j/53c21154b8bdb686a4a8d0bbc235a152Do let me know your feedback, or if you have any queries!!!
Rated 4.5/5 based on 11 customer reviews
Normal Mode Dark Mode

Develop Alexa skills with Alexa SDK using Node.js

Mukul Jain
Blog
05th Sep, 2018
Develop Alexa skills with Alexa SDK using Node.js

OK, so can you first answer these questions, simple yes/no kind.

Do you want to build voice-based applications?

Do you see all these Alexa Skill and gets amazed and surprised at the same time, that how these apps can be developed?

Do you know a little bit of JavaScript?

Do you have 5 minutes?

Well, if your answer to the above questions is a “Yes”, today’s your lucky day. Stick to the end, and by the end, you will know how to develop Alexa Skills using Node.js from scratch. In this post, I’m covering Alexa SDK, if you want me to discuss intent schema of Alexa Skills, do let me know in the comment. 

You can find the complete project on this gist.


Let’s start building

To create Alexa skill with NodeJS you need two things installed,

  1. Node
  2. NPM

This link will get your job done if above are not installed.

Once you’ve installed those, choose a directory in your computer, in my case, it’s “my-first-skill”.

Once you’re in it via shell, run the following set of commands:

npm install --save alexa-sdk
npm install -g node-lambda
node-lambda setup

So, what happened above? We installed two packages from Node package manager. Alexa SDK is to handle intents and node-lambda is for testing and deploying your lambda function.

As you can see, setup command have created some files for you. Now, you’ve to go in your .env file and paste the below file and replace the values inside curly brackets with your AWS credentials.

AWS_ENVIRONMENT=development
AWS_ACCESS_KEY_ID={your-token}
AWS_SECRET_ACCESS_KEY={your-secret-token}
AWS_ROLE_ARN=AWS_REGION={your-aws-region}
AWS_FUNCTION_NAME={your-function-name}
AWS_HANDLER=index.handler
AWS_MEMORY_SIZE=128
AWS_TIMEOUT=10
AWS_DESCRIPTION=
AWS_RUNTIME=nodejs6.10
PACKAGE_DIRECTORY=build
CONFIG_FILE=deploy.env
DEPLOY_TIMEOUT=3000000

Done? You’re almost getting there!!!

Now create an index.js file

touch index.js

Go into the index file and paste this content.

const Alexa = require('alexa-sdk');
const handlers = {
  'LaunchRequest': function () {
     this.emit('HelloWorldIntent');
  },
  'HelloWorldIntent': function () {
     this.emit(':tell', 'Hello World!');
  }
};

exports.handler = function(event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.registerHandlers(handlers);
    alexa.execute();
};

You just created an Alexa Skill, which will say “Hello World!” on getting invoked.

Wanna test? Let’s deploy it first.

Go to your shell, and deploy your lambda function with the following command:

 

node-lambda deploy

If you’ve set your .env file correctly, your lambda console will see a new lambda function. 

To create Alexa skill with NodeJS

Now, it’s time to go to your developer console.

Create a new skill, pass basic information like name and description of skill.

You will see in your console that your intent schema has already 4 default skills:

  1. FallbackIntent
  2. StopIntent
  3. CancelIntent
  4. HelpIntent

Create a new intent, let’s say “HelloIntent” and save the skill.

 

Develop Alexa skills with Alexa SDK using Node.js


Now, in lambda endpoints section, copy-paste your Lambda ARN id here.

 

AWS lambda ARN

If build passes, go to your test console and invoke your skill by saying

open <your skill name>

Your Skill will respond with “Hello World!”

BANG!!! You just created your first skill.

 

Let’s add some more basic intents

'AMAZON.StopIntent': function () {
    const card = 'Goodbye'
    const content = 'Thanks for using the skill'
    this.emit(':tellWithCard', content, card, content, null)
},
'AMAZON.CancelIntent': function () {
    this.emit('StopIntent')
},
'AMAZON.HelpIntent': function () {
    const card = 'Help'
    const content = 'Help will always be given at Hogwarts to those who ask for it'
    this.emit(':tellWithCard', content, card, content, null)
},
'HelloIntent': function () {
    const card = 'Hello!!!'
    const content = 'Woah! You created your first custom intent'
    this.emit(':tellWithCard', content, card, content, null)
},


Let’s deploy your lambda function again, and now test your skill with these phrases:

Help
Stop
close
cancel
hello

You will notice, the echo will say the phrases you’ve programmed in your lambda handler. Why not, modify those and play with it a bit :)

The more utterances you will have, the better your skill will become. So, try to add more ways that will possibly invoke your skill and improve your skills Voice Experience.

Alexa supports SDK for multiple languages: Java, Nodejs, Python, which makes easier for developers to develop skills.

There are many resources you can find on Alexa Skills, you can start with the documentation and blogs:

Alexa Built in Slots

Alexa Built-in Intents

An Alexa Based Project for multiple domains (VR, Recommendation system)

This was a basic tutorial to learn Alexa Skills using Node JS. If you want to build a really cool Alexa skill with Node JS, you’ve to go in depth and try to play with different options from Alexa console. If you want to contribute to some open source Alexa Skill project, you can start with one of my project: 
https://github.com/PaytmBuildForIndia/book-engine

Project Github Link: https://gist.github.com/myke11j/53c21154b8bdb686a4a8d0bbc235a152

Do let me know your feedback, or if you have any queries!!!

Mukul

Mukul Jain

Blog Author
Software Engineer at Edfora. Passionate about nodejs, serverless, AI & building futuristic stuff. Also a spontaneous blogger & witty tech speaker.

Leave a Reply

Your email address will not be published. Required fields are marked *

Top comments

Michael Smith

20 November 2018 at 4:34pm
Your article had helped me a lot in learning indepth concepts of nodejs,keep up the good work.

SUBSCRIBE OUR BLOG

Follow Us On

Share on

other Blogs

20% Discount