This is part 2 on creating an Alexa Skill using Lambda. Read part 1 for an overview here.
Before Alexa can understand the commands you speak to it, you need to create a custom "skill" in the Alexa Developer Console. Skills are a set of configuration settings which control which voice commands are parsed, and which lambda function handles these commands.
Note that Skills do not involve any code in themselves, they are just collections of config settings. Check out my overview article to see where skills fit into a server-less architecture.
As you create a new skill, one of the first decisions to make is your skill's invocation name. This is the keyword(s) which Alexa uses to know that all commands which immediately follow are for your skill, eg Alexa, open `My Fancy Skill` (where `My Fancy Skill` is your invocation name). Note that the invocation name does not necessarily have to match your skill's actual name, it just has to be a fairly distinctive and short keyword.
Try a few invocation names, the first few I tried did not work too well - Alexa would have difficulty understanding the words and reply with an error message like "unable to find a skill with that name". Distinctive words that are short but multi-syllable seemed to work the best.
Intents are entry-points to your code. Your skill is configured with a list of "sample utterances", which are sample voice commands. These utterances are then mapped to "intents". When Alexa hears your voice command, it sends a dictionary object to your lambda function and one of the keys in this object is event['request']['intent']. Your lambda function can then use the intent name to handle the command accordingly.
Something like this:
Here is what a sample intent schema looks like:
{
"intents": [
{
"intent": "RemainingIntent"
},
{
"intent": "MacroIntent",
"slots": [
{
"name": "Macro",
"type": "LIST_OF_MACROS"
}
]
},
{
"intent": "AMAZON.CancelIntent"
},
{
"intent": "AMAZON.StopIntent"
}
]
}
As you can see, the schema is a dictionary with "intents" as the only key. Its value is an array of one or more intent objects. Some notes:
Utterances are sample voice commands which your skill will handle. Each utterance must map to one intent from your intent schema. Some notes:
In the configuration section, you set which endpoint handles your Alexa voice commands. This could be any normal URL handled by a web server, but I chose to link this to a lambda function.
If you choose a lambda function, ensure your lambda function is in a AWS region which can support "Alexa Skills Kit" as a trigger. This is done in the Lambda configuration, at the time of writing one of the limited options was 'us-east-1'.
Account linking is the process of linking your Alexa skill with some 3rd party application. This is a bit more complicated, and is only required if you use functions that require logging in to another application.
The main setting here is the "Authorization URL", which is the URL the user gets redirected to to link their account when they first add your skill. I created a separate lambda function sitting behind an API Gateway for this, with more detail to come in another article. See how it fit in the overview.
There are 2 grant types you can choose, either implicit grant or auth code grant. I implemented the implicit grant, so the flow looks like this:
https://pitangui.amazon.com/spa/skill/account-linking-status.html?vendorId=ABC123#state=def123&access_token=123456&token_type=Bearer
This should cover the main tricky bits, I found the other sections straightforward. Setting up the lambda functions were a bit trickier though, and I'll expand on those shortly.