You are on page 1of 4

View source

PROJECTS Alexa SDK


files here!
All the files you need for
this tutorial can be found at
https://github.com/darryl-
bartlett/alexa-webupdate

A B O U T T HE A U T H O R
DARRY L
BAR T L E T T
job: Front-end developer
areas of expertise:
JavaScript, PHP, mobile
development
t: @darrylabartlett

ALEXA SDK

CREATE AN ALEXA
SKILL FOR YOUR SITE
Darryl Bartlett demonstrates how you can use the Alexa SDK to
communicate with your website content
Many of us now have some kind of voice WHAT WILL I NEED?
assistant around the home, whether it be an You will need to start by registering for an Amazon
Amazon Echo, Apple HomePod or a Google Home. It Developer account (developer.amazon.com) and an
seems that voice is going to have a huge impact on AWS account (aws.amazon.com).
the way we go about our daily lives and as web Firebase will be used to store our user input,
developers we need to ask ourselves, what are the so make sure you sign up for a Firebase account
ways we can implement voice into our websites and (firebase.google.com). We will also be using the News
web applications? API to get the latest news stories, so get yourself a
In this tutorial, we are going to be building a free API key at newsapi.org.
simple Alexa skill that updates the news content on
a website. The category type will depend on the voice INTENTS, UTTERANCES AND SLOTS
input by the user. It would help if you have some Once you are set up, navigate to developer.amazon.
experience with AWS already but there is a lot of com/alexa/console/ask and click on Create Skill. The
documentation out there to help you out if you get in first thing we need to do is set up Intents, Utterances
a pickle. and Slot Types inside the Alexa Developer Console.

82 december 2018
Alexa SDK

An intent is what the user of the skill is trying to


achieve. Utterances are specific phrases that users KEY SKILLS
will say when talking to Alexa, for example: ‘What
day is it?’. A slot is a variable that relates to an
utterance, for example: ‘What time is it in {place}?’.
This would make {place} the custom slot.
DESIGN PROCESS
Select Intents from the left-hand side of the
dashboard and click Add Intent. Make sure Create
Custom Intent is selected and type ContentUpdate
FOR VOICE
in the text box: this will now become our function Here are a few pointers when designing voice experiences
name later on. for Alexa.
We now move on to utterances, which is where
we are going to need to take the category of the Identify the user intents
news that the user wants to update to. We will be When developing your skill, it’s important to identify a list of all
using category as our slot name and then set up the the things the user can do within the skill before planning the
following utterances: utterances. For example, the user might be able to update website
content, update the website style and read posts.
“Update {category}” Be sure to make use of the built-in intents: you can see a full
“{category} stories” list of them at https://developer.amazon.com/docs/custom-skills/
“Update to {category}” standard-built-in-intents.html.
“Set stories to {category}”
Identify the utterances
Finally, we need to create a slot type, where we Think about how your user may communicate with Alexa. To
will write out a couple of inputs that we expect to update a style, the user might say ‘Update background to red’.
get from the user. From the left-hand side of the It’s also important to plan for miscommunication. Alexa might
dashboard, click Add next to Slot Types. Type in interpret ‘red’ as ‘ref’. A good way to deal with this would be by
‘NewsType’ and click the ‘Create custom slot type’ using the code below:
button. Under Slot Values, you will need to add in
some news categories. We will use Sport, Business, ‘Unhandled’: function () {
Technology and Politics. Once you are finished, make this.emit(‘:tell’, ‘I am sorry, I didn’t understand your
sure you have selected NewsType from the dropdown request. Please try again.’);
as the slot type for category. },

AWS LAMBDA FUNCTION Creating a script


We will now head over to Lambda inside AWS Make sure you plan out the script between the skill and your user.
(console.aws.amazon.com). Select Create Function Be sure to keep interactions short but enough so that the user
and then select the Blueprints radio box. Make sure understands how they need to respond.
you select the alexa-skill-kit-sdk-factskill from the list Below is a good conversation flow between Alexa and user.
and click Configure. Give your function a name and User: ‘Could I update my header to blue?’
Alexa: ‘Sure, I can update your header to blue. Would you like that
change to happen immediately?’
User: ‘Yes please!’
Alexa: ‘Great, that change has gone live!’

Above Here are the utterances we have setup inside the Alexa Developer Console for our skill.
Feel free to add more

december 2018 83
PROJECTS Alexa SDK

then create a new role. When finished, click Create


RESOURCES Function at the bottom of the page. You will need to
select Alexa Skills Kit as a trigger for your function,

GOING FURTHER WITH then we can move onto the function itself. We won’t
be using the built-in code editor for this project;
instead we will be writing out the functions locally

ALEXA SKILLS and then uploading a zip file. Make sure you copy the
contents of the index.js file inside the Lambda editor,
as we will paste this inside our local project.
If you want to learn more about Alexa skills development
after this tutorial, then check out the compiled list of CREATE A LOCAL PROJECT
resources below. We will start by creating a new Node project locally.
Inside our own index.js file, we will paste the contents
we just took from Lambda. We need to import
Firebase and the Alexa SDK using NPM.

npm install alexa-sdk


npm install firebase

Make sure you include the references at the top of


your index.js file.

const Alexa = require(‘alexa-sdk’);


Alexa Developer Home var Firebase = require(“firebase”);
https://developer.amazon.com/alexa-skills-kit
This is the best place to get started when building Alexa skills. It’s You should have some default code already there,
full of useful documentation, videos and has all the latest news on one of which is called ‘LaunchRequest’. This is used
Alexa development. to welcome the user to the skill. All you need to do
here is change the welcome message to ‘Welcome to
GitHub: Alexa Skills Kit SDK for Node.js website update’.
https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs
Here you will find lots of technical documentation and sample ’LaunchRequest’: function () {
projects for the Alexa Skills Kit SDK. this.emit(‘:ask’, ‘welcome to website update’);
},
Comprehensive Alexa Skill Development Course
https://www.udemy.com/comprehensive-alexa-skill-development- If you use the :tell command then Alexa will end
course the skill after the message, whereas if you use :ask
This Udemy course created by IOT Wonders will introduce you to then Alexa will listen for eight seconds for the next
Alexa development in video format. You will get the opportunity to prompt. We will be using :ask, so that Alexa is ready
learn by building out several skills and it is aimed at developers of to listen for our update prompt.
all levels.
FIREBASE CONFIGURATION
Essential React Native development tools Next, we need to add our Firebase configuration
https://aws.amazon.com/lambda/getting-started details at the top of the index.js file.
When developing Alexa skills, you are going to need a good
knowledge of AWS Lambda. The official website for AWS is a great var config = {
place for you to learn more. apiKey: “<API_KEY>”,
authDomain: “<PROJECT_ID>.firebaseapp.com”,
databaseURL: “https://<DATABASE_NAME>.firebaseio.
com”,
projectId: “<PROJECT_ID>”,
storageBucket: “<BUCKET>.appspot.com”,
messagingSenderId: “<SENDER_ID>”
};
Firebase.initializeApp(config);

84 december 2018
Alexa SDK

CONTENT UPDATE FUNCTION


If you remember earlier in the tutorial, we created
an intent called ‘ContentUpdate’. This means we
need to create a ContentUpdate function, where we will
respond to the user input based on the Utterances
we created. So, if the user said ‘Update to sport’
, then
this function would be called. We begin by creating
a variable called categoryType, which takes the voice
input of the user. We then store the category type
inside the Firebase database and get Alexa to tell us
the name of the category we updated to.

’ContentUpdate’: function() {
var categoryType = this.event.request.intent.slots.
category.value;
if(Firebase.apps.length === 0) {
Firebase.initializeApp(config);
}
Firebase.database().ref(‘/’).set({
preference: categoryType
}).then((data)=>{
Firebase.app().delete().then()
}).catch((err) => {
console.log(err);
})
this.emit(‘:ask’, ‘you updated to ‘ + categoryType);
},

After you save your index.js file, you will need to zip
up the project. Navigate to the project folder from
the command line and type the following command.

zip -r index.zip *
Top The Alexa simulator
is a great way of testing
Head back to your function inside Lambda and scroll your skill on your machine
without needing an Amazon
down to the Function Code section. From the drop- You will also need to enter your News API key from
Echo device
down box for code entry type, select ‘Upload a .zip newsapi.org on line 11 of the main.js file.
file’. You will now be able to upload your zip file. Above Here is what
the final HTML page will
Before you test your project inside the Alexa var myAPIKey = “<YourAPIKey”>; look like. It will display
the category that you
simulator, take the ARN on the top right of the
requested through Alexa
Lambda page and input this inside the Endpoint If you now open the index.html file, it should load in
section of the Alexa console. To test your skill, all some news stories from your chosen category. Every
you need to say is ‘Open web update’ and Alexa will time you tell Alexa to update with a new category
respond with ‘Welcome to web update’. If you now (sport, technology, business or politics), the news
say ‘Update to sport’, Alexa should say ‘you updated story category on the page will change.
to sport’. The word ‘sport’ should also appear under
preference inside your Firebase database. CONCLUSION
This is only a small example of what you could
NEWS PAGE achieve with Alexa and hopefully gives you a good
Finally, download the news page I created (index. foundation for any future voice projects. You could
html). Get the file from https://netm.ag/2EjFwQb always add in more slot values to get more categories
inside the Alexa Project – HTML folder. from the API or even update the project so that it
Open up the main.js file and enter in your own pulls in your own website news/posts. I can’t wait to
Firebase configuration details on lines 1-8, like we see how web and app developers will include Alexa in
did earlier in the tutorial. their projects.

december 2018 85

You might also like