/ aws

python-alexa: A Python framework for Alexa Development

A little while ago I got to experience Amazon's Echo and Alexa for myself when it arrived during my time in New York (Thank you, Amie!). Before it arrived, I had challenged myself to develop something for it. In the Amazon Echo/Alexa world, this is known as a skill and is made up of code that Alexa can call and return responses based on user input and other factors.

To get things going, I decided to make something that could interact with an AWS account, such as telling me how many running and stopped instances there is or what RDS databases are in an account. I called it VoiceOps and it worked as intended after a bit of trial and error. It would be cool to spend some time fleshing this out into something more substantial but I always have in the back of my head "when will the first outage happen because Alexa heard the wrong word"... scary.

The process of getting this Skill developed involved reading about and putting into practice Amazon's Alexa Developer documentation. I spent a lot of time going through it all to understand what makes up a skill and how to design one. During the development process of my little skill, I noticed a couple of things.

First of all, Alexa skill development documentation is pretty detailed and that a skill is made up of many components. I love the detail you can find about designing a voice interface vs a GUI and how to think about responses - i.e reading a long list of items is easy, remembering a list of more than 5 items by hearing it is a pretty tricky - How do you design around this?

Secondly, to make a simple skill, you need a lot of bits and pieces to make it work such as Intents, utterances, question vs statement response as well as input parameters - such as session information, user ids and more.

I wanted to try and tackle the second point by using my understanding of the first. So I set off to try and develop a framework that would make Alexa development super easy. I started off by looking at what else there was and came across a few options, such as flask-ask

I liked these options but what I was looking for specifically was something that rolled a few of the features of each, into one. I liked the simplicity of flask-ask and how it used Python decorators to implement functionality but wanted the AWS Lambda focus of some of the other frameworks. Also, I wanted to practice the development of a framework with the intention for it to be released on GitHub for others.

So over the next couple of weekends, I got version 1 of the framework completed and now use it in about 10 different skills I've made. I wanted to write about it here and share it with others as I think it is a great way to simplify Alexa development when deploying to AWS Lambda - You can focus on writing the code to make your skill do something cool and let the framework handle request routing, response generation as well as user, session and intent specific things.

Take a look at the below Hello World example of the framework in use.

"""Alexa Skill description."""
from alexa import Alexa
import json
alexa = Alexa("skillname")
response = alexa.response

def lambda_handler(event, context):
    """Lambda Handler method."""
    print json.dumps(event)  # Log the input event/request
    r = alexa.route(event)  # Get the response after routing the request
    print json.dumps(r)  # Log the response JSON being returned
    return r

def launch():
    """Launch Request method."""
    return response.statement("Hello World")

# Below is only used when running this file locally, i.e not in Lambda - this allows easy local testing
if __name__ == '__main__':

    with open('./test_events.json') as d:
        e = json.load(d)  # Load our test events json file

    # Make request and print output

The example above, when deployed to AWS Lambda and configured in the Alexa Skills developer console, would result in something that can launch and return a response with Alexa saying "Hello world"

Hopefully, you can see from this, it doesn't take much to get something working. With the inclusion of the last bit:

if __name__ == '__main__'

We can do some local testing by passing in sample requests. This shortens the time of development by removing the need to upload your code and run it each time and instead allows you to run it as often as you like, as quickly as your machine can handle.

You can build on this example to add more depth to the Skill, such as asking your Skill a question or making a statement to it - "Alexa, Tell Hello World I love you" - this could then trigger an intent called Love which generates a response saying "Obviously. I love you too!".

This could be as simple as adding the following to the example above:

@alexa.intent("Love", mapping={"component": "component"})
def skill(s, component):
    """Skill intent."""    
    return response.statement("<p>Obviously<b> I love you too</p>")

All of this is packaged together into a zip file for AWS Lambda, I then use the Cloudreach developed tool called Sceptre to deploy my AWS Lambda function and code to my AWS account. This is a great tool that allows you to generate Cloudformation stacks by writing templates in code and is used in some of our biggest projects at Cloudreach - I'll write another post about it in the future, as it deserves its own spotlight.

With all this together, we end up with a working skill in AWS that can then be configured in the Developer Console and eventually published to the skill store. I have done this with 1 skill (the others I have don't meet copyright requirements) and its all built using what I've talked about above.

If you haven't yet got an Amazon Echo device but would like to try out development for it, I would highly recommend checking out EchoSim, it is a web-based app that lets you interact with Amazon's Alexa service from your browser. It connects to your Amazon Developer account and has access to all the skills you create - plus the other things Alexa can do.

To find out more and use the framework, check it out on my GitHub Here

To try out my Alexa Skill, add it to your own Echo account and search for 'Taps Aff' - it can also be found here

And finally, if you want to see how the Taps Aff skill works and uses the framework, take a look here

Let me know what you think about the Skills development process in the comments below and if you have had a go doing any development/publishing yourself - How did you find it? I'm also open to any comments about the framework itself and how it could be improved - go easy on me, it's my first public "released" project and I haven't done much on it in a little while but would like to keep working on it.

 Neil

Neil Stewart

Neil Stewart

Cloud Systems Developer at Cloudreach. Passion for DevOps and Cloud Infrastructure development. AWS Certified in 5 areas and working towards Chef certification. Apple lover and general tech enthusiast

Read More