Simple Alexa skill in Node.js using Heroku as hosting server

The think which we gonna need is;
1. Heroku login
2. Git respository
3. Amazon developer account

We will use command promt for developing node js code and deploying it to heroku.

Here are the links for download CLI for heroku and gitHub/
GitHub: https://git-scm.com/download/
Heroku cli: https://devcenter.heroku.com/articles/heroku-cli

First of all create a new directory in local machine name it whatever you like, i have given name sample. Similar create a new repository in your git account.you will below screen:




Go through the given step for creating a new respository on the command line

for using alexa we will use alexa-app-server library.

for using node js you will need node js to be install so first install npm to your local machine.
https://nodejs.org/en/download/

For installing alexa-app-server type:

npm install alexa-app-server

after finishing installing alexa-app-server lib, Create a file server.js and copy paste below code in it.

'use strict';

var AlexaServer = require( 'alexa-app-server' );

var server = new AlexaServer( {
 httpsEnabled: false,
 port: process.env.PORT || 80
} );

server.start();

Let me explain what does code means.

Here we have set port to be used by server. if code is running from heroku than it will use  port given by heroku by (process.env.PORT) other wise port 80 will be use.

Now we will create Procfile. this will tell heroku what to do when process will run.file name should be without extension. just use Procfile.

web: node server.js

Now will create Package.json file by tying npm init
this will ask some question and after completing that json file will get created.

After that push your file to git by performing below command.

git add .
git commit -m "added procfile and server.js"
git push origin master

Now, create new folder in main folder named apps and in that create sub folder skills  where add file for our skills.create new Github repository for skills name it whatever you like i use sampleskills.
perform similar step of initiaze repo and do the initial commit/push.Now we will indicate github that apps/skills is submodule. To do this go to main folder and type

git submodule add https://github.com/rsanjuseason/sampleskills.git apps/skills

Replace https://github.com/rsanjuseason/sampleskills.git with your skill git link.

Now we will generate package.json for skills folder.so that npm can know that it submodule.
To do this run below command:

npm init

Now we will install alexa-app package.

npm install alexa-app

Now again regenerate package.json for install dependency.

Now it's time to create index.js file for performing something by skill, something like say hello world.

module.change_code = 1;
'use strict';

var alexa = require( 'alexa-app' );
var app = new alexa.app( 'myskill' );


app.launch( function( request, response ) {
        response.say( 'Welcome to Alexa World' ).reprompt( 'Ask Something.' ).shouldEndSession( false );
} );


app.error = function( exception, request, response ) {
 response.say( 'Sorry some error occured ' + error.message);
};

app.intent('sayHello',
  {
 "utterances":[ 
  "say Hello",
  "Hello alexa",
  "What's up",
  "Hey alexa"]
  },
  function(request,response) {
    response.say("Hello, Welcome to alexa world.");
  }
);

module.exports = app;


To test our code run below command in cmd from main folder:

node server.js

Then, open browser and open below link:

localhost/alexa/myskill



Hope this works, then we will deploy it heroku so, that it can invoke from any place.

So, now we will commit our work to git.
first commit/push skill files to git.move to skill directory and type:

git add .
git commit -m "skills file index.js"
git push origin master


then we will push out main folder files to git by below commands after moving to main directory:

git submodule forech git pull origin master
git add .
git commit -m "update submodule"
git push origin master

Now create New app in heroku to deploy our files.Mine is helloworldskill

after creating app type below command

heroku login

After this you have enter your login details then,

heroku git:remote -a helloworldskill

Replace helloworldskill  with your app name.then push your file.

git push heroku master


Now its time create skill on amazon.go to your developer amazon and create a new skill



Add intent schema and utterences.


In configuration tab select Https and below url 

https://helloworldskill.herokuapp.com/alexa/myskill

Replace https://helloworldskill.herokuapp.com with your heroku app url.



In SSL Certificate select

My development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority

Now its time test our skill, enter utterance it should work fine.



You can test it in simulator or echo dot.
Here is link for simulator

https://echosim.io

Just Say

"alexa, Ask helloworld to say hello"

it will repeat back with

"Hello, welcome to alexa world"

Yehhhh, we have created our first alexa skill.

Hope this helps
Thanks.







8 comments:

  1. Hey Sanjay,

    Can you tell me what is intent?

    ReplyDelete
    Replies
    1. An intent is what a user is trying to accomplish. Within the code, this is how you define your function. ‘Intent’ doesn’t relate to the specific words that a user says, but the high-level goal they are aiming for.

      Delete
  2. event manager understand data flow in a tech stack, set up back-end processes and systems correctly and troubleshoot technology before, during and after events thank you for coordinating, hybrid ideas and how to introduce a speaker template

    ReplyDelete