Quickstart - Building Alexa Bots


Introduction

Some time ago I got a Echo Dot to experiment with building bots and serverless computing. I have to say, the Echo Dot is the perfect device for starting with that since the development for it is pretty intuitive, well documented and, best of all, you can easily run your app on AWS.

Needless to say, make sure you have an Amazon account with which you can register for Amazon Developers and AWS.

So, let’s have a look how to get started with building bots quickly.

Skill Types, User Interaction and Components

Choosing the Proper Skill Type

Before starting to code anything you should have a clear idea what your skill is going to do and how your user is going to interact with it. Since this is a simple introduction you don’t have to make anything super complicated. Make a simple trivia game or a weather bot for example.

There are multiple skill types you can choose from:

  • Custom Skill
  • Smart Home Skill
  • Video Skill
  • Flash Briefing Skill

For a more in-depth look of the different types of skills you can check the Alexa Skills Kit documentation.

For these kind of bots you’d have to go for a “custom skill”, since what we’re trying to do is not related to any of the other supported uses.

User Interaction and Components

When going for a custom skill you have to define the way the user interacts completely by yourself. This means taking care of things like:

  • Intents
  • Slots
  • Sample utterances
  • Interaction model

So, what’s what?

Component Function
Intents What a user can do with a skill. For example - get weather for a specific city, set default city, etc. You would usually name them the same way in your code - “GetWeatherForCity” or “SetDefaultCity”, etc.
Slots Essentially the variable in your intent. For example the “GetWeatherForCity” and “SetDefaultCity” intents will require a city to be defined. In this case we will have a “City” slot.
Sample utterances A list of phrases a user might use when communicating with the skill. For example a sample utterance might be “weather in Amsterdam”.
Interaction model The mapping of intents and sample utterances. For example the utterance “weather in my city” will be mapped to the “GetWeatherForCity” intent.

Before you start building any code think about how the user is going to interact with your bot. Start by:

  1. Making intents based on the different actions users can take.
  2. Defining the variables in the intents and making slots for them.
  3. Thinking of sample utterances.
  4. Mapping your intents to the sample utterances.

The Alexa Skills Console

This is where we’re going to do all of the necessary configurations of the skill and build our interaction model. You can access it through the Amazon Developers portal.

The process is pretty simple and straightforward:

  1. Create a new skill by clicking on the “Create Skill” button.
  2. Give it a name and a locale.
  3. Choose a model - on our case “custom”.
  4. Click on “Create Skill”.

After the skill has been created you will be presented with a console where you can build, test and publish your skill. In the beginning page there is a handy checklist which I recommend you to follow to get started easily. It will guide you through:

  1. Configuring an invocation name.
  2. Configuring your intents, sample utterances and slots.
  3. Building the interaction model.
  4. Configuring the endpoint for your code.

The code

When you’re ready with designing the next step is to put everything in our code.

I recommend having a look at some of the sample code which is available on the Alexa organisation in GitHub. Depending on your preferences have a look at:

I personally work mainly with Ruby and Python, and since Ruby is not an option for AWS Lambda I’m going to be using Python.

What I usually do is split the helpers, intents and main functions in separate files. This makes it more readable in my personal opinion.

Have a look through the sample code and tinker around with it. You can familiarise yourself with the request and response JSON interface on the Alexa developers documentation. The documentation details the request format (what is going to be sent to your code) and the response format (what is going to be sent back to your Alexa bot), and that is, at the end of the day, what is the most important part. The rest of the code is up to your preferences.

AWS Lambda

You can run your code on AWS Lambda which makes it extremely simple. You can connect it easily to your Alexa bot - just provide the ARN of your Lambda function and tada, you’re done. 🎉

Make sure to package your code and any extra modules you may have used to a zip file and upload it directly to Lambda or S3. You can automate it pretty easily with a Makefile or something of the sorts. The AWS documentation has a page about creating NodeJS and Python deployment packages for AWS Lambda which are pretty useful.

Testing

Its pretty easy to test your bot. Use the Alexa developers console to test your both using voice, text or JSON input. You can also connect it to an Echo device you own and test it there as well.

Conclusion

So, this is how to get started building Alexa bot in a nutshell. Have fun creating awesome bots and coming up with new ideas. 😃