Skip to main content
Current Version Build Status https://coveralls.io/repos/github/pycontribs/django-alexa/badge.svg?branch=master Requirements Status Vulnerbilities Status Code Style: black

Amazon Alexa Skills Kit integration for Django

The django-alexa framework leverages the django-rest-framework package to support the REST API that alexa skills need to use, but wraps up the bolierplate for intent routing and response creation that you’d need to write yourself.

Freeing you up to just write your alexa intents and utterances.

Quickstart

Feeling impatient? I like your style.

$ pip install django-alexa

In your django settings.py add the following:

INSTALLED_APPS = [
    'django_alexa',
    'rest_framework',  # don't forget to add this too
    ...
]

In your django urls.py add the following:

urlpatterns = [
    url(r'^', include('django_alexa.urls')),
    ...
]

Your django app will now have a new REST api endpoint at /alexa/ask/ that will handle all the incoming request and route them to the intents defined in any “alexa.py” file.

Set environment variables to configure the validation needs:

ALEXA_APP_ID_DEFAULT="Your Amazon Alexa App ID DEFAULT"
ALEXA_APP_ID_OTHER="Your Amazon Alexa App ID OTHER" # for each app
ALEXA_REQUEST_VERIFICATON=True # Enables/Disable request verification

You can service multiple alexa skills by organizing your intents by an app name. See the intent decorator’s “app” argument for more information.

If you set your django project to DEBUG=True django-alexa will also do some helpful debugging for you during request ingestion, such as catch all exceptions and give you back a stacktrace and error type in the alexa phone app.

django-alexa is also configured to log useful information such as request body, request headers, validation as well as response data, this is all configured through the standard python logging setup using the logger ‘django-alexa’

In your django project make an alexa.py file. This file is where you define all your alexa intents and utterances. Each intent must return a valid alexa response dictionary. To aid in this the django-alexa api provides a helper class called ResponseBuilder. This class has a function to speed up building these dictionaries for responses.

Please see the documentation on the class for a summary of the details or head to https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interface-reference and checkout the more verbose documentation on proper alexa responses

Example:

from django_alexa.api import fields, intent, ResponseBuilder

HOUSES = ("gryffindor", "hufflepuff", "ravenclaw", "slytherin")

@intent
def LaunchRequest(session):
    """
    Hogwarts is a go
    ---
    launch
    start
    run
    begin
    open
    """
    return ResponseBuilder.create_response(message="Welcome to Hog warts school of witchcraft and wizardry!",
                                           reprompt="What house would you like to give points to?",
                                           end_session=False,
                                           launched=True)


class PointsForHouseSlots(fields.AmazonSlots):
    house = fields.AmazonCustom(label="HOUSE_LIST", choices=HOUSES)
    points = fields.AmazonNumber()


@intent(slots=PointsForHouseSlots)
def AddPointsToHouse(session, house, points):
    """
    Direct response to add points to a house
    ---
    {points} {house}
    {points} points {house}
    add {points} points to {house}
    give {points} points to {house}
    """
    kwargs = {}
    kwargs['message'] = "{0} points added to house {1}.".format(points, house)
    if session.get('launched'):
        kwargs['reprompt'] = "What house would you like to give points to?"
        kwargs['end_session'] = False
        kwargs['launched'] = session['launched']
    return ResponseBuilder.create_response(**kwargs)

The django-alexa framework also provides two django management commands that will build your intents and utterances schema for you by inspecting the code. The django-alexa framework also defines some best practice intents to help get you up and running even faster, but allows you to easily override them, as seen above with the custom LaunchRequest.

>>> python manage.py alexa_intents
{
    "intents": [
        {
            "intent": "StopIntent",
            "slots": []
        },
        {
            "intent": "PointsForHouse",
            "slots": [
                {
                    "name": "points",
                    "type": "AMAZON.NUMBER"
                },
                {
                    "name": "house",
                    "type": "HOUSE_LIST"
                }
            ]
        },
        {
            "intent": "HelpIntent",
            "slots": []
        },
        {
            "intent": "LaunchRequest",
            "slots": []
        },
        {
            "intent": "SessionEndedRequest",
            "slots": []
        },
        {
            "intent": "UnforgivableCurses",
            "slots": []
        },
        {
            "intent": "CancelIntent",
            "slots": []
        }
    ]
}
>>> python manage.py alexa_utterances
StopIntent stop
StopIntent end
HelpIntent help
HelpIntent info
HelpIntent information
LaunchRequest launch
LaunchRequest start
LaunchRequest run
LaunchRequest begin
LaunchRequest open
PointsForHouse {points} {house}
PointsForHouse {points} points {house}
PointsForHouse add {points} points to {house}
PointsForHouse give {points} points to {house}
SessionEndedRequest quit
SessionEndedRequest nevermind
CancelIntent cancel
>>> python manage.py alexa_custom_slots
HOUSE_LIST:
  gryffindor
  hufflepuff
  ravenclaw
  slytherin

There is also a convience that will print each of this grouped by app name

>>> python manage.py alexa
... All of the above data output ...

Utterances can be added to your function’s docstring seperating them from the regular docstring by placing them after ‘—‘.

Each line after ‘—’ will be added as an utterance.

When defining utterances with variables in them make sure all of the requested variables in any of the utterances are defined as fields in the slots for that intent.

The django-alexa framework will throw errors when these management commands run if things seem to be out of place or incorrect.

Contributing

  • The master branch is meant to be stable. I usually work on unstable stuff on a personal branch.

  • Fork the master branch ( https://github.com/pycontribs/django-alexa/fork )

  • Create your branch (git checkout -b my-branch)

  • Install required dependencies via pipenv install

  • Run the unit tests via pytest or tox

  • Run tox, this will run black (for formatting code), flake8 for linting and pytests

  • Commit your changes (git commit -am ‘added fixes for something’)

  • Push to the branch (git push origin my-branch)

  • If you want to merge code from the master branch you can set the upstream like this: git remote add upstream https://github.com/pycontribs/django-alexa.git

  • Create a new Pull Request (Travis CI will test your changes)

  • And you’re done!

  • Features, Bug fixes, bug reports and new documentation are all appreciated!

  • See the github issues page for outstanding things that could be worked on.

Credits: Kyle Rockman 2016

Metadata

Release files for django-alexa 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-alexa 0.0.9
File Size Uploaded Provenance
django-alexa-0.0.9.tar.gz 38.0 kB Without provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for django-alexa 0.0.9
File Interpreter ABI Platform
django_alexa-0.0.9-py3.6.egg Legacy Egg format - - Details
django_alexa-0.0.9-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 99.0 kB

Release files / django-alexa-0.0.9.tar.gz

Download URL django-alexa-0.0.9.tar.gz
Size 38.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fa8f264da108a03e33b5b3dd018ee666c49328208ed181f4a1a539ade9f3f8fb
BLAKE2b-256 checksum
How to use checksums
89db35d2399c2dd9df294abf724b9eaeeb65d149c0bd9872b60590049a4e6e16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.7

Release files / django_alexa-0.0.9-py3.6.egg

Download URL django_alexa-0.0.9-py3.6.egg
Size 40.8 kB
Tags Egg
SHA-256 checksum
How to use checksums
fffa2cedeebfd0dff421b52ab0d535af160d92bc6f4d4ea731a1400c3eeb0451
BLAKE2b-256 checksum
How to use checksums
6efb2ff47b02fa034fbf178f7b7ce41c1cc31e1a85e27d2a4cb0b32c05fe8466
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.7

Release files / django_alexa-0.0.9-py2.py3-none-any.whl

Download URL django_alexa-0.0.9-py2.py3-none-any.whl
Size 20.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3892f18975fcd341161a7dca2d4a7a4476a24058213e720b6ffd2b3870aeac79
BLAKE2b-256 checksum
How to use checksums
b569a2c354e0c434860d72f64d088026180215f4e092d1fd1de2fb34aee3068e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.7

Security for django-alexa 0.0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page