Building An Alexa Skill Is Easier Than You Think!

Intro

I recently bought an Echo Dot and after reading these two tutorials (Step by Step Guide & Build a Skill in Under an Hour) I thought I’d give it a shot.  My idea was to build a skill called Goat Compliments which would tell the user a random compliment along with some screaming goat sound effects to add some humor.  Overall, I was surprised how easy the whole process was – hopefully this post will motivate others to try it out as well!

screenshot_2016-11-02-10-13-22-1.png

Going into this, my only advantage was that I was already familiar with JavaScript.  I didn’t know anything about the technology behind Echo skills, haven’t done much with Node development and didn’t know anything about the various AWS technologies such as Lambda functions or S3 storage.  So if those terms seem unfamiliar, don’t worry!

Key Components of a Skill

As the articles above explan, a skill is made up of 2 main parts:

  • The JavaScript code which you’ll likely want to deploy as a Lambda function on AWS.  This basically allows you to run your code on Amazon’s “cloud” and it’s free unless you happen to build an incredibly popular skill that generates a lot of traffic.  This code, and configuring the Lambda function is where you’ll spend 90% of your time.
  • Alexa Skill Kit.  This part is pretty simple and after the initial config, there isn’t much that you’ll need to change.  Setting the icons, description, category, etc are all pretty much a one time thing.
    • The “Test” tab for the skill is very useful for debugging.  You can send requests to your skill, check the request/response JSON, and even hear the response with the “Listen” button in the lower right cornerskill-test
    • The “Interaction Model” tab is where you define the utterances your skill will respond to, and it’s a good idea to have it respond to a wide range of possible phrases.  Here’s an example of how I set mine up – everything after the “GetGoatComplimentIntent” prefix is what Alexa is listening for.

utterances

v1 – Bare Bones Functionality

There are a lot of examples that Amazon has posted on GitHub – check out the Samples or main Alexa page.  The easiest way to get up to speed is to look at their samples, and then reuse parts of their samples.  By reusing Amazon’s code as opposed to random code found somewhere else, when you submit the skill to be reviewed you can add a note saying it’s based on one of their samples and hopefully this makes the approval go quicker.

For example, with my skill Goat Compliments I used some code from the Space Geek sample (how to retrieve a random value from an array), and some code from the Tide Pooler sample (using SSML to play a sound along with text).

By reusing as much of the sample code as possible, I had my first version of the app built, deployed and running in developer mode on my own Echo in probably about 90 minutes.  After cleaning up a few more things with the code and doing more testing, I then submitted the skill for certification & publishing.  I was surprised to see it only took 3 days to hear back and it passed certification!

v2 – Enhancements

Soon after Goat Compliments was published, I thought of 2 key areas that I wanted to improve:

  • I didn’t like having the array of compliments hard-coded in the app since this made it more difficult to update those values.
    • I decided to upload a JSON file to AWS S3 storage which holds the array of compliments.  This allows me to easily update that list, upload the new version to S3 and Goat Compliments can immediately start using it.  In case anyone is wondering how to do this, here’s a code snippet:
var params = { Bucket: '(-- your bucket name--)', Key: '(--your file name--)' };
new AWS.S3().getObject(params, function (err, json_data) {
   if (!err) {
       var result = JSON.parse(new Buffer(json_data.Body).toString("utf8"));
   }
   else {
       // handle error condition
   }
});
  • I wanted some customized statistics to get an idea of how often my skill was actually being used.
    • This article shows a very quick & easy way to tie your Alexa skill into Google Analytics.  This gives me a way to see how often compliments are being generated, but I wish there was also a way to see how many users are enabling (i.e. downloading) the skill to their Echos.

I was expecting to need to go through the certification process again for the changes to take effect, but as soon as I uploaded the changes to my Lambda function the production app started using the compliments from S3 and generating hits on Google Analytics.  Which brings me to my next topic…

Lessons Learned

  • By default, your dev and published skill will point to the same Lambda function so if you have problems (syntax errors, etc) while making improvements you’ve also taken your production skill offline!
    • This setup can be good since you don’t need to go through the certification process again and wait days for the changes to take effect.
    • But this can also be bad if you incorrectly assume (like I did) that the Lambda function code for  your published skill was locked down until you submitted another certification request.
    • While I was working on an improvement to read the compliments from S3 storage, I accidentally introduced an error.  I initially thought this was limited to my development version of the skill, but this quickly turned into an “oh shit!” moment once I realized it was also impacting the published skill.
    • Apparently there’s a way to have multiple versions of the Lambda function – this allows you to have 1 locked down for production and 1 that you can safely mess with during development.  This forum post has some more info, but I haven’t tried that yet.
  • Vague & unhelpful error messages
    • I wish Amazon would improve the error messages, because this caused me a lot of trial and error.  There were a few times I saw weird errors like “unexpected token < in JSON at position 2” when I was trying to edit my Lambda function.  I’m pretty sure this was caused by me leaving my browser open, letting my session expire and then coming back later and trying to pick up where I left off.  Reloading the page and logging back in fixed this.
    • Another problem I ran into was that I changed the description of my Lambda function and all of a sudden it stopped working.  I didn’t touch the JavaScript logic, and the ARN didn’t change, but my skill started giving the error “The remote endpoint could not be called, or the response it returned was invalid.”  Luckily I was able to fix this by re-uploading the .zip file with the skill logic.
  • S3 files must be marked public!
    • After uploading a file to S3, make sure you right-click on it and select “Make Public” so that your Alexa skill can access it.  It’s easy to forget to do this if you’re uploading a new version, so make sure you do this every time you upload a file.

Hopefully this article explained the basic process for building a skill.  Be sure to check out  Goat Compliments and rate it if you like it!

I’m already thinking about ideas for my next project – I may try to build a voice controlled robotic bartender.  If you want to hear about how that progresses, be sure to follow me on Twitter!

P.S.  If you’re still undecided about whether to build a skill or not, Amazon is running a promotion where they’ll give you a free hoodie if you publish a skill – this was originally a T-shirt when I published mine, so it seems like the promotion keeps getting extended.

 

 

 

Advertisements

2 thoughts on “Building An Alexa Skill Is Easier Than You Think!

  1. Thank you, thank you, thank you! For posting about the “unexpected token < in JSON at position 2” error. I've been trying for hours to figure out what was wrong with my code. Refreshing my console did the trick.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s