Writing Skills for Amazon Alexa

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

fromflaskimportFlaskfrompylexa.appimportalexa_blueprintfrompylexa.intentimporthandle_intentfrompylexa.responseimportPlainTextSpeechapp=Flask(__name__)app.config['app_id']='amzn1.echo-sdk-ams.app.my_app_id'app.register_blueprint(alexa_blueprint)@handle_intent('Echo')defhandle_echo_intent(request):returnPlainTextSpeech(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:

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

1

web: gunicorn echo.app:app

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: