Build Your First Alexa Skill – Create the Intent Handler

This is the fourth post in a step-by-step Alexa skill creation guide:

In the last post we created the interaction model for our skill.  That model will determine what action the user wants to perform, then it will call code (the intent handler) to fulfill the user’s request.  In this post we’ll write the code to pick a random fact and return it to the interaction model.  The code will run in AWS, in something called a lambda function.  You can think of a lambda function as being a self-contained chunk of code that will run in AWS without requiring us to set up any servers or environments.


 

Set Your Region « Back to Top » 

Head over to the aws.amazon.com web site and sign in to your AWS account.  After you sign in, you’ll be on the starting page of your AWS account.  First things first, one of the components we need (the Alexa Skills Kit Trigger) is only available in specific regions.  Check the region your account is using in the upper right corner and make sure it is one of these:

  • US East (N. Virginia)
  • US West (Oregon)
  • EU (Ireland)
  • Asia Pacific (Tokyo)


 

Create the Lambda Function « Back to Top » 

In the AWS services search input, enter “lambda” and select the Lambda service:

On the AWS Lambda service page, look for a button labeled, “Create a function”, and create a new function.  In the “Create function” screen, select “Blueprints”, filter the blueprints by typing “alexa” into the “Add filter” input and hitting enter.  Select the “alexa-skill-kit-sdk-factskill” blueprint:

Scroll down and click “Configure”.  In the function configuration screen, enter a name and select the existing role, “service-role/basic-lambda-role”:

Scroll down and click, “Create function” and you’ll  be on the configuration page for your function:

Scroll down to the code editor and delete the starting code to start with a blank slate.  If you don’t remember the keyboard shortcut to “Select All”, right-click in the code area and choose “Select All” from the menu.  Then you can easily delete all of the code.

Now copy the code below into the empty code window:

After copying the code, your code editor should look like this:

Now you’ll make the following changes:

  • On line 3, enter your skill name between the single quote marks.  In my case, I’ll replace 'Your Skill Name' with 'Naked Mole Rat Facts'.
  • Replace lines 6, 7, and 8 with your own facts.  Note that each fact line starts with double quotes and ends with double quotes and a comma.
  • Since we’re using double quotes to start and end a fact, avoid using them in the fact itself.  If your fact has double quotes, like, J.J.'s favorite saying is, "Dynomite!", just remove them, and Alexa should read it the same as with double quotes.

After updating the skill name and entering my list of facts, my lambda function’s code looks like this (click the image to see it full size):

For folks comfortable with JavaScript, feel free to use single quotes to delimit your fact strings.  I use double quotes here to avoid having to escape single quotes.

If the right margin of the editor shows any error icons, make sure each line starts with a double quote and ends with a double quote and comma.  The images below show a missing starting double quote, a missing ending double quote, and a missing comma respectively.  Note that if a comma is missing, the error icon appears on the line below the one missing the comma.

 

 


After you’ve made those changes, scroll back up and click the “Save” button.

Now that we’ve entered the code for our skill handler, we’ll create a test event and test it.  At the top, to the left of the Test button, click “Select a test event…” and choose “Configure test events”.

In the “Configure test event” dialog, choose the “Alexa Intent – GetNewFact” event template and name it.  You can enter any name you’d like, but try to make it descriptive.  Here I entered “GetNewFact”:

Scroll down and click “Create”.  Back on the lambda function designer, check that you have the “GetNewFact” test event selected and click the Test button.  If you entered your facts correctly, you’ll see a message indicated that the test succeeded.  If your test fails, go back and double-check your list of facts, making sure each fact starts with double quotes and ends with double quotes and a comma.  Also, make sure you’ve saved any changes you have to your code.  If the Save button next to the Test button is not grayed out, it means you have changes to save.


 

Configure the Trigger « Back to Top » 

Now that we’ve entered the code to return a fact to our interaction model, we’ll configure the Alexa Skills Kit trigger that will serve as an intermediary between the interaction model and the lambda function.  On the left side of the lambda function designer, click on “Alexa Skills Kit” under “Add triggers”.  If you don’t see the Alexa Skills Kit trigger, you didn’t create the function in a region that supports it.  Go back to the top of this post and review the list of regions that support the ASK trigger.

Scroll down to the trigger configuration panel.  Here’s where you’ll enter the Skill ID you made note of from the last post.  If you don’t remember it, go back to the Amazon developer site, then to the Alexa dashboard, and the Alexa Skills Kit page.  Find your skill and copy the Skill ID.

Click “Add”, and then click “Save”:

We’re done setting up our intent handler and now we can go back to the skill in the developer site and finish things up.

Before we leave our lambda function, copy down the Amazon Resource Name (“ARN”) so we can finish configuring our skill:

 

Next Steps « Back to Top

In the next post, we’ll go back to our skill and configure it to call the lambda function we just created.  After that’s done, we’ll be able to test our skill.

Leave a Reply