Skip to content

Instantly share code, notes, and snippets.

@Jc2k
Last active June 27, 2018 07:02
Show Gist options
  • Save Jc2k/e39a9e6d80f1599f82b6acfec92d477d to your computer and use it in GitHub Desktop.
Save Jc2k/e39a9e6d80f1599f82b6acfec92d477d to your computer and use it in GitHub Desktop.
My first alexa skill

Backend

I used the alexandra python package. See https://pypi.python.org/pypi/alexandra. It abstracts away dealing with HTTP or Lambda and you just use decorators to map Alexa intents to python functions. Here is my test.py:

import alexandra

app = alexandra.Application()
name_map = {}

@app.launch
def launch_handler(session):
    return alexandra.reprompt('What would you like to do?')

@app.intent('MyNameIs')
def set_name_intent(slots, session):
    name = slots['Name']
    name_map[session.user_id] = name

    return alexandra.respond("Okay, I won't forget you, %s" % name)

@app.intent('WhoAmI')
def get_name_intent(slots, session):
    name = name_map.get(session.user_id)

    if name:
        return alexandra.respond('You are %s, of course!' % name)

    return alexandra.reprompt("We haven't met yet! What's your name?")

if __name__ == '__main__':
    app.run('0.0.0.0', 8080, debug=True)

Assuming you have an active virtualenv with alexandra installed you can run this from a terminal with just:

python test.py

I use ngrok to expose a public https url that the skill can use.

Intents

Slots are where you get your parameters, and the slots that are available for an intent (and their data type) are defined in a schema json file:

{
    "intents": [
        {
            "slots": [
                {
                    "type": "AMAZON.LITERAL", 
                    "name": "Ingredient"
                }
            ], 
            "intent": "MyNameIs"
        }, 
        {
            "slots": [], 
            "intent": "WhoAmI"
        }
    ]
}

The default types that are available are:

* `AMAZON.DATE`
* `AMAZON.DURATION`
* `AMAZON.FOUR_DIGIT_NUMBER`
* `AMAZON.NUBER`
* `AMAZON.TIME`
* `AMAZON.US_CITY`
* `AMAZON.US_FIRST_NAME`
* `AMAZON.US_STATE`
* `AMAZON.LITERAL`

See https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/alexa-skills-kit-interaction-model-reference#Slot%20Types

You can define your own custom types in the web console, and these can extend the built-in types. A custom type is just the name of your type and a list of up to 50,000 possibile values. These are converted automatically into a spoken form and an output form, so the value that is sent to your skill might be slightly different to the one you defined - e.g. 2 beers instead of two beers or 1st amendment instead of first amendment.

In our example we used AMAZON.LITERAL. As you will see, that means we have to give lots of examples of that literal in our utterances. This slot type is actually deprecated and we should use AMAZON.US_FIRST_NAME or our own custom type.

There are built-in intents for when you are building sessions. For example, if you needed to ask for confirmation before taking an action you can use the AMAZON.YesIntent. See https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/implementing-the-built-in-intents#Available%20Built-in%20Intents

Utterances

Then we need our sentence data. These are just examples of how a user might invoke a sentence:

MyNameIs call me {john|Name}
MyNameIs call me {frank|Name}
MyNameIs call me {stacey|Name}
MyNameIs call me {helen|Name}
WhoAmI say my name
WhoAmI tell me name

You need to design as many possible utterance variations as you can. But you notice we don't have a sentence "what is my name". That's because of how skills are invoked. You don't say "Alexa, what is my name". You say "Alexa, ask myskill to say my name". See https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/supported-phrases-to-begin-a-conversation.

AFIACT, there is no 'best guess at what they said'. In this example, if you say "Alexa, ask myskill to call me Henrietta" it will think you said Helen.

Wiring it up

See also

https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/getting-started-guide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment