With Alexa, Amazon offers an innovative voice guided assistant. It runs on several Amazon devices, the most popular one being the Amazon Echo. Alexa’s voice recognition works impressively well. You can play music and radio, listen to the news, get the weather forecast, ask for opening hours and much more. Moreover, Alexa is constantly improving. Not only does it receive continuous updates, but it also applies machine learning algorithms to the huge amount of user data it receives. Besides the functions integrated by default, Alexa also runs additional apps. Those are called skills and must be enabled by the user. What stands out is that Amazon is making a serious effort to encourage people to create and publish skills. They offer great tutorials and examples to get you started. Furthermore, the free tier of Amazon Web Services (AWS) provides all you need to host and run your skills. So becoming a developer is fun, because it is easy and free! Also Alexa takes care of all the heavy lifting, hence it’s simple to build interactive and useful skills. You simply process JSON inputs containing the voice commands already converted to text. Then, you return text that is automatically translated into a voice:
Getting started with custom skills
There are several ways to build a custom skill for Alexa. Basically, you must create a web service that is accessible via HTTPS. It must be capable of receiving JSON data, processing it and returning a JSON response. The service can be self-hosted or running as a Amazon Lambda function. The latter is provided in the AWS free tier, so it’s ideal for starting right away. Here are some readings to get you going with building custom skills for Alexa using Python (Notice: Currently Lambda only supports Python 2.7):
- Start with this basic guide by Amazon to learn how to set up your first skill without any coding. It explains the creation of AWS Lambda functions, the Amazon developer account and the Alexa Skills Kit.
- I recommend following up the basic guide with this blog post. It offers a well structured guide on coding a useful skill using Python. However, don’t spend too much time on the actual Python code. Read the next section for a simpler method of implementing Alexa functionality in your code.
- Here you can deepen your understanding of how Alexa works. Also, Amazon provides numerous additional references in the links. However, there is a lot of information for a beginner and it is not presented in comprehensive manner. Use this to look up specifics that remain unclear in the other guides.
After finishing with the second tutorial, you should have a good understanding of Alexa’s architecture and the basic process of skill development. However, the method used there has a downside: the code is written in pure Python. Thus, you have to deal with requests, responses and other issues of Alexa yourself. Additionally, you can’t use libraries not included in the Lambda environment. If you are eager to start developing your own Alexa skills, there is a more efficient way: Flask-Ask to the rescue!
Creating your custom skill with Flask-Ask
The Flask-Ask module makes developing Alexa skills even simpler. It is built upon Flask, a micro web-framework. Hence, your Python code gets the ability to run as a web-service. It can be set up on your own web server, but also in AWS Lambda. Moreover, it takes care of tedious tasks like dealing with the requests and responses. This results in the following short code for building a simple skill:
from flask import Flask from flask_ask import Ask, statement app = Flask(__name__) ask = Ask(app, '/') @ask.intent('HelloIntent') def hello(): return statement("Hello World!") if __name__ == '__main__': app.run(debug=True)
Pretty simple, isn’t it? Now you can focus on the exciting stuff: Building cool features for your skill. To learn about the functionality of Flask-Ask, make sure to read the following article.
Combining Flask-Ask and ngrok for easy development and local testing
Another great benefit of Flask-Ask is that it can be used for local development and testing as well as in Lambda functions. Working with in-line code in Lambda functions, as in the second guide, is unhandy. You are restricted to libraries provided by Amazon. Also, during development you need to test changes in your code immediately. Combining Flask-Ask with ngrok makes this possible and is straight-forward. A great article on this can be found here. Basically, your Flask-Ask powered Python code opens up a server on your local machine. Then, ngrok provides you a HTTPS address redirecting requests to your local server. Hence, you simply enter this address as the HTTPS endpoint of your skill in the developer console. As a result, you can edit your code locally and changes will be reflected instantly when testing with Alexa. No need to upload anything!
Quickly deploy your skill to AWS Lambda with Zappa
As mentioned before, Flask-Ask is also great for running your code as a Lambda function. Using Zappa is the easiest method to get this working right away. Zappa creates the WSGI environment in Lambda needed by the Flask framework. In addition, Zappa packs up your local virtual Python environment and makes it available for your Lambda function. Consequently, your skill can use all the helpful libraries that make Python so great. Yay, unlimited possibilities! The following guide is really comprehensive and goes through the process of deploying your skill step by step.
In short, this is how to get on track with developing custom skills:
- Get familiar with the architecture of Alexa. Learn about the Alexa Skills Kit, the Amazon developer console and AWS Lambda functions
- Learn about Flask-Ask and how it makes your life easier by simplifying your skill’s Python code
- Set up your local development and testing environment and connect it to your skill over ngrok
- Learn to use Zappa for deploying your code to AWS Lambda so that you are ready for production
Get started and apply your coding skills to create a cool new feature for Alexa!