How to create Alexa Skill using Python?

Piotr Rogulski
stanwood
Published in
3 min readNov 8, 2018

Lately, I’ve built a couple of applications for Alexa devices. I found out that a lot of people started to be interested in Alexa skill kit in terms of building or using the applications. If you will go to Alexa Skill Shop you can find a lot of apps starting on simple voice bot through smart home skills and ending with top music app players.

Let’s build an application that can tell a joke from icndb.

Alexa allows using HTTP webhooks or lambda functions as a communication between your system and end-user device. It is recommended to use lambda functions if the whole system is built on top of Amazon Services.

In this tutorial, I would like to show you how simple it is to create webhooks and pin them up in Alexa Skill console.

You can find source code on github here.

Requirements:

Getting started

Install alexa-skill package to simplify Alexa usage:

pip install alexa-skill

Each Alexa skill needs to specify build-in intents.

Implement a key intent which will return random joke using BaseIntents class:

Mapped methods should return Alexa response and True if the response was successfully handled, otherwise False.This is very helpful when analytics tools need to be connected. For chatbots, I recommend to use Chatbase.

All requests which are coming from a platform should be handled by one webhook. To do this I will use falcon web framework:

Testing local webhook

Alexa requires an endpoint secured with https. To develop and test webhook served from a local machine I recommend to use ngrok. It simply exposes local servers behind NATs and firewalls to the public internet over secure tunnels.

  1. Download ngrok
  2. Setup local server
$ pip install gunicorn 
$ gunicorn webhook:app

3. Start ngrok

$ pip install gunicorn $ gunicorn webhook:app

$ ./ngrok http 80http://127.0.0.1:4040 Forwarding                    http://0374b9cf.ngrok.io -> localhost:8000 Forwarding                    https://0374b9cf.ngrok.io -> localhost:8000  Connections                   ttl     opn     rt1     rt5     p50     p90                               0       0       0.00    0.00    0.00    0.00

4. Copy and use forwarding url

Alexa console setup

  1. Go to alexa console and create skill
  2. Go to Intents tab and add new intent

3. Add sample utterances, something like Tell me a joke, Make me laugh etc…

4. Add ngrok webhook url

5. Go to test tab and enable tests for a skill

6. From now on your webhook is ready to use

7. You can test it on simulator or Alexa device

Summary

Alexa skill development is very simple. All you need to know is basic Python Programming language knowledge, one of the web frameworks like Django, Flask, Falcon or other… and that is all!

You don’t need to know Amazon Web services to create a voice application but you can become AWS Alexa skill developer. You can easily develop and test your Alexa skill using ngrok which will expose your local Python server to the web without deploying it to the remote server.

A good idea, user-friendly utterances and well defined Alexa responses are the keys to success.

Originally published at rogulski.it on November 8, 2018.

--

--