Writing skills for Amazon Alexa

An introduction to pylexa, a Python library that makes it easy to develop Amazon Alexa Skills.

by Patrick Smith

Amazon's Alexa Voice Service, the service behind their Echo devices, enables developers to build skills that allow users to interact with services using only their voice.

For Promptworks' first foray into Alexa Skill development, we decided to create a skill that allows one to post to Slack by simply dictating to an Echo device the channel and message to be posted. For example, you might want to post to the "development" channel, "It's standup time!" You could do that by telling your Echo device, "Alexa, tell Chat Bot to post 'It's standup time!' to development.'"

The skill, Chat Bot for Slack, was recently published and we're excited to introduce you to pylexa, a Python library that aims to simplify Alexa Skill development.

Writing a skill with pylexa

Amazon provides tutorials on writing skills using AWS Lambda, but sometimes Lambda just won't cut it. The pylexa library allows quick skill development in Python using Flask. Paired with Heroku, you can write your own Alexa Skill in no time!

Let's say you want to write a simple skill that echoes whatever the user says back to them. So far, you've created an Alexa Skill on the Amazon Developer Portal. After installing pylexa and Flask with pip install pylexa flask, we can now write a simple Flask app using pylexa that handles a single intent, Echo, to repeat the user's input:

from flask import Flask

from import alexa_blueprint
from pylexa.intent import handle_intent
from pylexa.response import PlainTextSpeech

app = Flask(__name__)
app.config['app_id'] = ''

def handle_echo_intent(request):
    return PlainTextSpeech(
        request.slots.get('message', 'Nothing to echo'))

That's all that's necessary for the application code! To get everything running on Heroku, we can add the following requirements.txt:

cryptography # necessary to enable openssl on heroku

And the following Procfile, pointing gunicorn to the file containing our defined app:

web: gunicorn

Now, all that's necessary is to push to Heroku and configure our Alexa skill to point to our Heroku app.

If you haven't been playing along, you can find all the necessary code on our github.

Skill configuration

The pylexa library also helps with the configuration of the skill by including a generate-alexa-conf command. The Alexa Skill configuration requires an intent schema, custom slots, and sample utterances. With pylexa, we can define one YAML file to define each of these requirements and have the necessary parts generated for us.

Our skill is pretty simple, so the configuration is simple as well. We'll handle just one intent and include a single sample utterance. Save the following in schema.yml:

  - Echo:
      message: AMAZON.LITERAL

    - echo { something | message }

Then, we can run the following command to generate the necessary configuration:

$ generate-alexa-conf schema.yml

The generate-alexa-conf utility put our intent schema in intent_schema.json and the sample utterances in utterances.txt:

$ tree conf
├── intent_schema.json
├── slots
└── utterances.txt

1 directory, 2 files

The contents of those files can be copied and pasted in to the configuration pages of your Alexa skill and you're ready to go!

You can use the "Service Simulator" on the "Test" page of your Alexa Skill to send requests to your skill and watch it in action.

The pylexa library is available on github and on PyPI. For more motivation, check out the code for our published skill.

Thanks to Dustin Ingram for reading drafts of this post.