Create your first skill for Alexa: part 2

In this part we will see how to create your first skill to know the weather in any city in Europe and at the end of this article you will be able to leave with it, we promise.
Now that you've understood all the important concepts associated with creating skills for Alexa, we can finally get to the heart of the matter. If this is not the case, do not hesitate to read again the first part of this article.
The prerequisites for this part :
– An account on the Amazon developer portal (https://developer.amazon.com/edw/home.html#/)
– Your test environment
– Node 6 and npm installed (https://nodejs.org/)
– The basic concepts of ES6
– Your favorite code editor

1. Installing libraries

Here, we keep it simple.
alexa gif

mkdir MyAwesomeSkill
cd MyAwesomeSkill
npm init
npm install —save alexa-skill-kit yahoo-weather

A little explanation is in order: Alexa-skill-kit offers a first layer of abstraction for the official Amazon library, in order to make development as simple and intuitive as possible.
Yahoo-weather, on the other hand, is simply a wrapper for Yahoo's Weather API
For more information, you can check the repo https://github.com/stojanovic/alexa-skill-kit et https://github.com/mamal72/node-yahoo-weather

2. Code

index.js

const alexaSkillKit = require('alexa-skill-kit')
const intent = require('./lib/Intents')
exports.handler = function(event, context) {
	alexaSkillKit(event, context, parsedMessage => {
	    // console.log(JSON.stringify(parsedMessage))
	    return intent(parsedMessage)
	})
}

lib/Intents.js

const weather = require('yahoo-weather')
module.exports = function(parsedMessage) {
	if (parsedMessage.type === 'IntentRequest' && parsedMessage.intent.name === 'GetWeather') {
        return weather(parsedMessage.intent.slots.Location.value)
          .then(info => info.item)
          .then(item => item.condition)
          .then(condition =>; {
            //console.log('info', condition)
            return `It is currently ${condition.text} with ${condition.temp} degrees in ${parsedMessage.intent.slots.Location.value}`
          })
          .catch(() => {
            return `Hm, I can't find any weather data for ${parsedMessage.intent.slots.Location.value}.`
          })
}

You remember the concept of Intent, Slots and Utterances, right? If this is not the case, I invite you to re-read the corresponding sections in the first part of this article.
In the Intent Schema, we had defined the GetWeather function which took as Slot a Location object of type AMAZON.EUROPE_CITY

{
    "intents": [
        {
            "intent": "GetWeather",
            "slots": [
                {
                    "name: "Location",
                    "type": "AMAZON.EUROPE_CITY"
                }
            ]
        }
    ]
}

And in the Sample Utterances file, we described different ways to call this function from Alexa.

GetWeather what is the weather in {Location}
GetWeather weather in {Location}

So when the skill is called by the user, he will know which function to perform.
When the user says " Alexa, ask My Awesome Skill for the weather in Paris », our skill will return « It is currently Sunny with 20 degrees in Paris ».

All you have to do is zip your project to move on.
Quick tip for Mac and Linux users: From your terminal, from the project directory, enter the following command to zip the project.

 zip -r ../MyAwesomeSkill.zip * 

 
The code is also available here: https://github.com/jzarca01/alexaskill-jsrepublic

3. Send the skill to Amazon servers

Resources created in Amazon Web Services are each uniquely identified by an Amazon Resource Name (ARN).
In this part, we'll create a Lambda function on Amazon's servers and push our zipped project there.
Take your Amazon Developer credentials and go to the following address: https://console.aws.amazon.com/?nc2=h_m_mc
Then click at the top left on “Services” then “Lambda”
Alexa AWS Lambda
All you have to do is click on “Create a Lambda function” then select “Empty function”.
Select "Alexa Skills Kit" from the list of triggers.
Alexa Kit AVS Skill
It is in the "Configure a function" tab that our code will be uploaded to the Amazon servers.
Fill in a name and choose your runtime environment. Here, choose “NodeJS 6.10” since our code uses ES6 syntax.
The name doesn't matter, it won't be used again later.
Then in the next section, choose from the “Type of code entry” drop-down list the option “Upload a ZIP file”.
Alexa ZIP
Alexa ZIP Recap
Finally, after the summary page, all you have to do is click on "Create the function" to go to the next step.
Oh and one last thing, write down the ARN address of your function, you will need it for the next part of this article.

4. Register your skill with Amazon

Take your Amazon Developer credentials and go to the following address: https://developer.amazon.com/edw/home.html#/skills
If you haven't created your account yet, now is the time.
Alexa Developer Console
Then click on the “Add a New Skill” button at the top right of the screen.
From here, you are guided through the process to provide all the information needed to set up your new skill.
Alexa Skill Information
In the “Interaction Model” section, you will find the “Intent Schema” as well as the “Sample Utterances” which we have been talking about since the first part of this article.Alexa Intent Schema
Alexa Sample Utterances
It is quite possible to create a multilingual skill, but we will not discuss it in this article. However, if you are interested in the subject, here is the link to the official documentation (https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/developing-skills-in-multiple-languages)
Finally, in the “Configuration” section, you are asked if your skill is hosted on Amazon servers or on a private server. Here, we tried to go to the simplest, at Amazon. This is where the ARN address you wrote down earlier will come in handy. It indicates the address where your skill is executed.
Alexa Global Fields ARN
If everything went well, the result of the simulator will correspond to what we expected.
Alexa Service Simulator
One last step to take full advantage of your hard-coded skill, activation.
As explained in the first part of this article, skills are not downloaded to your device. They are simply activated from your dashboard.
By default, your skill is activated, but if it was not, you would have to connect via the Alexa mobile application to activate it from the 'Skills' section.
 
And as long as they are not submitted for validation by Amazon, only the developer account has access to them.
In the meantime, unleash your creativity to create ever more wonderful skills.

Thank you for reading me, and if you liked the article, do not hesitate to share it on social networks.
Jérémie, JS-Republican @JSRepublic
[actionbox color=”default” title=”” description=”JS-REPUBLIC is a service company specializing in JavaScript development. We are an approved training center. Find all our technical training on our partner site dedicated to Training” btn_label=”Our training” btn_link=”http://training.ux-republic.com” btn_color=”primary” btn_size=”big” btn_icon=”star” btn_external =”1″]