Build an Alexa Skill in 7 Minutes Flat with Node.js and StdLib

Written by keithwhor | Published 2017/02/07
Tech Story Tags: amazon-echo | alexa | nodejs | serverless

TLDRvia the TL;DR App

At StdLib (“Standard Library”) we allow developers to deploy functions written in Node.js as infinitely scalable API endpoints, which they can then share with others via our library. This functionality, paired with our other features like automatic SDK generation and documentation, has made StdLib a perfect match for the Amazon Echo Dot we received while attending AWS re:Invent. We’re excited to share our product with you— StdLib is the fastest, easiest way to get started building Alexa Skills, from newbie to veteran developers.

StdLib + Echo Dot = ❤

Our enthusiasm with our Echo Dot first culminated in a Dolores Abernathy Alexa Skill demo during the Westworld Season Finale. As a result of that video, we received a lot of questions from developers asking how to implement their own Alexa Skills. Based on the overwhelming response of the developer community, we thought it best to put together a 7 minute guide on how to build an Alexa Skill with StdLib. Let’s get started!

What You’ll Need Beforehand

Minute 1: Preparing Your Alexa Skill

To begin with, we’re going to prepare your Alexa Skill on the Amazon Alexa Developer Portal. Visit developer.amazon.com/alexa and sign in to begin. Click the “Alexa” button at the top to find this page:

Get Started with the Alexa Skills Kit

Click Get Started > under the Alexa Skills kit to begin. Next, on the top right of the screen, click Add a New Skill.

Skill Information

This is the information used to identify your skill and how your skill will interact with Alexa. It’s pretty straightforward to get started with.

Fill in these fields as indicated above; Skill Type is “Custom Interaction Model” so we can define the skill behavior, Name is “Test” to easily identify the skill, and Invocation Name, or the phrase we’ll use to get Alexa to execute our skill is “Test Bot” — i.e. “Hey Alexa, ask Test Bot [a phrase]”. Press Next on the bottom right when you’re ready.

Minute 2: Prepare Your Interaction Model

Next, the interaction model defines how Alexa will interpret things you ask of her. We’re now looking at defining the phrase, sentence or question at the end of “Hey Alexa, ask Test Bot [whatever you’d like]” and how we expect Alexa to parse it.

Intents

Intents can be thought of as different functions that your skill can refer to. They can interpret sentences based on Utterances (see below) and from these utterances can populate slots (variables). We’ll cover slots in a future tutorial, for now let’s get our first Intent and Utterance set up:

Our Intent needs an Intent Schema, so we’ll make a basic one that just has a “HelloWorld” intent. You can copy the setup on the left. This is going to be linked to a very simple Utterance (function trigger).

Utterances

Utterances are trigger phrases to ask Alexa to refer to a specific intent. In order to trigger our HelloWorld intent, we’ll use the phrase, “how are you.”

Very Simple Utterance Example

This is all we really need to set up our Interaction Model to begin with. We’re now ready to actually write code for our Alexa Skill.

Minute 3: StdLib Account Setup

To build a function and deploy it as an infinitely scalable web service, you’ll first need to setup a StdLib account, you can do so by signing up on the StdLib Homepage. Just click “Sign Up” and once you’ve registered, you’re all good to go!

Click “Sign Up” to Create an Account — Takes Less than a Minute!

Minute 4: Create a StdLib Workspace

If you don’t have Node.js installed, download version 6.9.x or higher from the Official Node.js Website. Once complete, open up your Terminal or Command Line and install the StdLib Command Line Tools, available on GitHub: stdlib/lib (full documentation available).

$ npm install lib.cli -g

You’ll now use the StdLib command line tools to create an Alexa Skill. First, create a directory for your StdLib services and initialize a workspace — you’ll be asked to log in with the account you already registered.

$ mkdir stdlib$ cd stdlib$ lib init

Once your workspace is initialized, you’re ready to create your StdLib service.

Minute 5: StdLib Service Creation and Deployment

You’re now ready to create your Alexa Skill. StdLib provides really simple service templates that allow you to work with Alexa without having to write any additional code. Create a service with the “alexa” template by typing the following:

$ lib create -t alexa

Follow the on screen instructions to continue, and enter the directory associated with your Skill.

Alexa Skill Function Creation Workflow

You can test your skill immediately with:

$ lib .

However, you’ll get an error stating that the “Intent name is required” — execute the “HelloWorld” intent specifically with:

$ lib .intents.HelloWorld

The response should look something like this;

HelloWorld Intent Response

Awesome! The JSON response you see is what Alexa expects in order to talk to you when your skill is accessed. If you want to make any modifications to the Skill or Intent, simply look in the /functions/intents/HelloWorld.js folder. You can now deploy to StdLib using:

$ lib up dev

An example output showing where your Alexa Skill is Available via HTTPS

Once deployment is complete, you’ll see the above message (with your username and service name) and can execute it live (in the cloud) using the URL provided, or you can test it from the command line using:

$ lib <username>.<service>[@dev].intents.HelloWorld

Great! Let’s combine this endpoint with the Interaction Model we generated earlier.

Minute 6: Assigning Your StdLib Endpoint to Alexa

Now that you’ve deployed a function as an infinitely scalable web service on StdLib, you’re ready to connect it to Alexa. Go back to the Amazon Developer Portal and on the Configuration option for your Skill, enter the following:

Endpoint Setup via HTTPS

Note that you’ll replace https://keith.lib.id/alexa@dev/ with your endpoint as retrieved in the last step — please include the trailing slash! Once complete, hit Next in the bottom right corner and you’ll be taken to SSL configuration options. Choose “My development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority” to continue (StdLib takes care of SSL for you).

Development Endpoint is a StdLib Subdomain

Hit Next again in the bottom right corner to finish your skill preparation.

Minute 7: Congratulations! Your Alexa Skill is ready!

All that’s left to do is test your skill — you should be on the Test page already. First make sure your Skill is enabled for testing:

On the “Test” Page, make sure your skill is enabled

Scroll down a bit an enter in an Example Utterance.

“how are you” is one of our Utterances

Type “how are you” (we set this up in a previous step) and hit Ask Test. You should see the following result:

Hit “Listen” to hear a voice demo of your response! Your Skill is now live for testing through your personal Echo or Echo Dot. If your Dot is powered on, connected to WiFi and listening, simply ask it:

Hey Alexa, ask Test Bot how are you?

You should get the following response, as shown in this demo:

Alexa “Test Bot” Skill Demo

Yep — that easy! That’s all you need to start playing and testing Alexa Skills on your own, we certainly hope you found it helpful!

That’s It! Your Alexa Skill is ready!

Thanks for following along! We’re happy to have been able to share how fast and easiest it is to build Alexa Skills on StdLib, and we’re excited to see what you’ll build. Stay tuned for more updates by signing up on StdLib, star us on GitHub at stdlib/lib, or follow @StdLibHQ on Twitter.

We plan on releasing more material shortly to show you how to build more in-depth Alexa Skills. Integrations with other StdLib services or 3rd party tools are simple, enabling you to even more powerful things with your Echo Dot using only a few lines of code. Read our documentation on GitHub to familiarize yourself in the meantime, and join our Developer Community Slack by click Community > Slack on stdlib.com.

Keep in touch, and comment here or e-mail me at keith [at] stdlib [dot] com if there’s anything specifically you’d like to know about! Let us know if you’ve built anything exciting that you would like us the feature or share. Feel free to follow me on Twitter as well, @keithwhor.


Published by HackerNoon on 2017/02/07