Skip to content
Single file template, examples and utilities for creating Amazon Alexa's skills (ASK) using Python
Branch: master
Clone or download

README.md

ask.py

Single file template, examples and utilities for creating Amazon Alexa's skills (ASK) using Python

Why?

Amazon provides pretty cool tutorials and examples but most of them are written in Node.js. I thought I'd share my findings with other developers who may prefer Python instead.

How to get started?

  • In line 44, replace MYMODEL for your model class name. Tip: if you have a separate class for the tutorial, you can create and instance of your tutorial class.
  • Implement your skill by inheriting from BaseModel. See MYMODEL for an example. Tip: try to keep the text in separate functions/templates so you can reuse it or override in subclasses; I use this approach for the tutorial.
  • Start by defining the translation layer and finite state machine, see the examples provided in MYMODEL for get_service_translation_layer() and get_fst().

How to get it up and running?

This is intended to work on AWS infrastructure. It uses ASK, Lambda and DynamoDB.

  • Setup your skill, when it comes to configuring the endpoint, go to Lambda.
  • Create a new lambda using the Python Alexa template. Select edit code inline and copy/paste the code from your single-file-template.py file.
  • Copy the ARN to the skill.
  • Create your Dynamodb table.

What else is included?

What I did:

Check my skills, all based on this template:

What I learned:

  • Use Amazon's intents but translate them to your actions. For example, I translate both StopIntent and CancelIntent to do_cancel. See get_service_translation_layer().
  • Use a finite state machine to keep track of what is going on and remap actions accordingly. For example, when the user is playing, do_cancel may prompt the user if they really want to exit. While in the exit prompt, do_cancel may go back to playing. See get_fst() or visit the project page for the Posted Messages skill for more information on how to define a voice interaction using a finite state machine: https://www.hackster.io/josep-valls/alexa-message-board-now-with-slack-324b84#toc-voice-user-interface--vui-6
  • Serialize things in a portable way so you can store your state in either the Alexa session object, DynamoDB or somewhere else. A Python dictionary should work (and are stored nicely in a DynamoDB map), just remember to use strings as keys so it can be serialized to json easily. See my serialize() and unserialize() methods.
  • DynamoDB is expensive. For multi-turn interactions, don't serialize to DB after every request. Serialize to the session property in your response and persist to DB once you receive a SessionEndedRequest.
You can’t perform that action at this time.