Custom Alexa Skill with JavaScript – Part 1

Custom Alexa Skill with JavaScript – Part 1

Recently I starting messing around with Alexa Custom Skills. I created a Custom Alexa Skill with JavaScript that is a basic Grocery List app that allows you to create grocery lists with Alexa (Amazon Echo unless you have Alexa running on your own device). I chose a shopping list because it allowed me to get exposed to several different aspects of Alexa Voice Services. Alexa does come with a shopping list by default, but I made my own to try and learn a bit.

This post assumes that you are familiar with Amazon Developer (Alexa), AWS Lambda, and AWS DynamoDB. Below are links for all three if you are not. If there are enough requests I can post videos on setting up all three. I have Alexa running on a Raspberry Pi which I set up watching this video, he also goes through creating your Lambda and Alexa accounts as well.

Amazon Developer (Alexa)
AWS Lambda
AWS DynamoDB

Cloning The GitHub Repo

I have a GitHub Repo up that you can clone to get you started with creating a Custom Alexa Skill with JavaScript.

1
2
git clone https://github.com/jacobscarter/Alexa-Skill-List-Keeper.git
cd Alexa-Skill-List-Keeper

Next we will go quickly through uploading your code to AWS Lambda and assigning it to your Custom Alexa Skill. Then we will go through the code files individual to understand what they do in Part 2 of creating a Custom Alexa Skill with JavaScript.

Uploading to AWS Lambda and Assigning to your Alexa App

Create a new Lambda Function.

This is a four step process. You can skip (just click next) the first step which is “Select Blueprint”.

Configure Triggers

Here we are going to set up our Alexa Skill to trigger our Lambda Function. From the Configure Triggers screen click on the dotted box and select “Alexa Skills Kit”. We will finish setting up this trigger from the Alexa Skill Set Developer Portal later.

configure lambda triggers when creating Custom Alexa Skill with JavaScript

Configure Function.

Choose a name and description for your function under the Configure Function section. We will be choosing a runtime of Node.js 4.3.

In the Lambda Function Code section we will choose the entry type of “Upload a .ZIP file”. Now we need to create our zip file. Select all of the .js files in your GitHub Repo and add them to a ZIP. IMPORTANT: DO NOT zip a folder containing the files, you must zip only the files. *note*: we will go over the code in part 2 of this post but you will need to change two parts of these files before uploading them so they will work for you, you need to enter your APP_ID and DYNAMO_TABLE_NAME into index.js Line 6 and storage.js Line 33 and 61 respectively.

DO THIS:
Proper way to compress your filed when creating a Custom Alexa Skill with JavaScript and AWD Lambda

AND NOT THIS:
Incorrect way to compress your filed when creating a Custom Alexa Skill with JavaScript and AWD Lambda

The next section is the Lambda function handler and role section. Here select index.handler as your function handler. We are choosing index.handler because we export a handler module from our index.js file. We will go into more detail about this later. For your role choose “Create A Custom Role”, you can use all the defaults for this (just click next).

Lastly are all the advances settings which we will keep at their defaults.

Creating Our Alexa Skill with Alexa Skill Kit

Next we are going to Add A New Skill in our Alexa Skill Kit Portal. Find the Add a New Skill button in the upper right.

This is a 6 step process, but since we are not covering publishing your app to the App Store we will skip some things. This post will allow you to run your app on your own device for testing.

Skill Information

For skill type select “Custom Interaction Model”, next name your skill, and lastly choose an Invocation Name. This one is important. This is the name you will use to tell Alexa you are about to use your app. For example if you call your skill “Grocery List” you will say “Alexa, start Grocery List….” to kick of your custom skill.

Interaction Model

Next is your Intent Schema, Custom Slots, and Sample Utterances. Lets go through each of those.

Intent Schema:

Copy and paste the contents of intent-schema.json into this section.

When your Lambda Function is called via your Custom Alexa Skill it is passed an argument (javascript object). We will go more into what that looks like later, but one of the properties on that object is the intent. We have different code to handle different intents. It is through our intent schema that we define what types of intents we have, and the slots or key words that we pass into them. For our example today we have the intent types of:

  • AddItemIntent
  • DeleteItemIntent
  • SearchListIntent
  • HelpIntent
  • AMAZON.StopIntent
  • ReadListIntent

The AMAZON.StopIntent is one of Amazon’s Built In Intents.

Slot Types:

Under the Slot Type section choose “Add Slot Type”, in accordance with your Intent Schema name your new slot type GROCERY_LIST_ITEMS. Next add the four item of bread, milk, apple, and orange. Each separated by a new line (no commas at the end of the line). From Amazon’s documentation, custom slot types are defined as:

A custom slot type is used for items that are not covered by Amazon’s built-in set of types and is recommended for most use cases where a slot value is one of a set of possible values.

When using Amazon’s example of horoscopes, custom slots are straight forward because of the finite number of options, when doing something more complex Amazon suggests putting a representative list of words with properly proportioned representative word counts. For a more comprehensive explanation of that look at Amazon’s Custom Slot Values docs.

Sample Utterances:

Before I mentioned that the intent type is passed into our custom Lambda Function. It is via the Sample Utterances that the intent type is determined. Copy and paste the contents of sample-utterance.txt into this section.

The pattern here is the Intent Name followed by the utterance with the variable to be used in our function {itemName}. It is by matching the audio input to these utterances that Alexa determines how to build our request model that is passed into our Lambda Function.

Configuration

Here is where we connect our Lambda function with our Alexa Skill. For the Endpoint option choose Lambda ARN. Enter your Lambda ARN, you can find that by going to your Lambda page in AWS Console, selecting your function, and choosing “Show ARN” under Actions.

lambda-show-arn

For “Account Linking” choose No.

Test

We will go over the Test section in more detail in Part 2 when we are going over our code. As long as test is enabled (toggle is at the top of this step) you can begin messing with your function on your device you set up when creating your Amazon Developer account, either the Echo you own or your single board computer if going the Raspberry Pi route.

Setting Up Your Dynamo DB

The last and easiest step of getting all set up to use your Custom Alexa Skill is creating your DynamoDB in AWS that your list is stored on.

From the DynamoDB page in your console choose “Create Table”. Give the table a name and a Primary Key. For our Primary Key we will use customerId as a string. Use all the default and click create. That’s it!

The table name you selected is the name you will enter on line 33 and 61 of storage.js

Custom Alexa Skill with JavaScript – Continued in Part 2 we will dive into our custom skill code

2 thoughts on “Custom Alexa Skill with JavaScript – Part 1

Leave a Reply

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