Alexa Skill in an hour
Building Alexa Skill using Alexa Skill Kit & AWS
Alexa is Amazon’s cloud-based voice service available on tens of millions of devices from Amazon and third-party device manufacturers. With Alexa, you can build natural voice experiences that offer customers a more intuitive way to interact with the technology they use every day.
In this blog, we will create a trivia skill using which you can play a simple Reindeer trivia game. This is kind of a quiz game regarding Reindeer. You will be asked 5 questions with 4 options each and one correct answer. You need to guess the correct answer. Alexa will tell you your score as well as the correct answer in case you are wrong.
This will be done in 4 steps:
- Creating Alexa Skill
- Creating Amazon Lambda Function backend
- Integrating Alexa Skill with the Lambda function.
- Testing the skill using Alexa Simulator.
Let us Begin!!!
Step I — Creating Alexa Skill
What apps are to mobile applications, skills are to Alexa.
- Login to Amazon Developer Portal for Alexa.
- Select Your Alexa Consoles >> Skills
3. Click on Create Skill
4. Give a name for your Skill. We will be using AWS Trivia.
5. Select Custom model and click on Create Skill button.
With the Alexa Skills Kit, you can create skills with a custom interaction model. You implement the logic for the skill, and you also define the voice interface through which users interact with the skill. To define the voice interface, you map users’ spoken input to the intents your cloud-based service can handle.
6. Now building a skill requires 4 essential steps:
Invocation Name: This is the name that your users will need to say to start your skill.
Intents, Samples, and Slots:
- Intents: An intent represents an action that fulfills a user’s spoken request. There are a few default intents provided by Amazon — Amazon.CancelIntent, Amazon.StopIntent, etc.
For example, in our case one of the custom intents that we will be adding is AnswerIntent which will represent the action of using providing the answer for a question. Intents can optionally have arguments called slots. - Sample utterances: A set of likely spoken phrases mapped to the intents. This should include as many representative phrases as possible. For example, one of the sample utterances in our case can be — The answer is option 2 or the answer is 2. We will add different combination of such utterances for each intent.
Slots: A representative list of possible values for a slot. Custom slot types which are used for lists of items that are not covered by one of Amazon’s built-in slot types.
For example, in our case the options for every question are from 1–4. So in the utterances for AnswerIntent, the possible values can be 1, 2, 3 or 4. So we will create a slot for this — ANSWER and use that in the utterances instead of hardcoded values — The answer is option {answer}
Building the model — Once the custom interaction model is configured for above 2 steps, we need to build the model. This is where the magic happens.
- Invocation Name — We will be using aws trivia. (You can use any name you prefer).
- Intents, Samples and Slots –
We will add 3 custom intents and 4 Amazon default intents: - AnswerIntent
- AnswerOnlyIntent
- DontKnowIntent
- AMAZON.StartOverIntent
- AMAZON.RepeatIntent
- AMAZON.YesIntent
- AMAZON.NoIntent
Alexa Skill Kit provides an easier way to configure this — JSON editor. You can simply create a json template in this editor.
Go to JSON Editor and paste the following json text and click on Save Model:
{
"interactionModel": {
"languageModel": {
"invocationName": "aws trivia",
"intents": [
{
"name": "AnswerIntent",
"slots": [
{
"name": "Answer",
"type": "LIST_OF_ANSWERS"
}
],
"samples": [
"{Answer} is my answer",
"is it {Answer}",
"my answer is {Answer}",
"the answer is {Answer}"
]
},
{
"name": "AnswerOnlyIntent",
"slots": [
{
"name": "Answer",
"type": "LIST_OF_ANSWERS"
}
],
"samples": [
"{Answer}"
]
},
{
"name": "DontKnowIntent"
},
{
"name": "AMAZON.StartOverIntent"
},
{
"name": "AMAZON.RepeatIntent"
},
{
"name": "AMAZON.HelpIntent"
},
{
"name": "AMAZON.YesIntent"
},
{
"name": "AMAZON.NoIntent"
},
{
"name": "AMAZON.StopIntent"
},
{
"name": "AMAZON.CancelIntent"
}
],
"types": []
}
}
}
You will see that the configured Intents, Samples and Slots have been successfully added. Let us see how to manually add these things.
- Click on DontKnowIntent and add few sample Utterances as given below:
- Click on Save Model.
Now let’s add values for answers slot. Click on Slot Types >> LIST_OF_ANSWERS and values 1, 2, 3 and 4. Under Synonyms, add one, two, three and four. Click on Save Model.
Click on the Build link and you will see that 3 out of 4 steps are done and we are left with the final one — Endpoint.
Endpoint is a backend web service which will handle and process the requests from the skills.
Alexa skill kit supports 2 types of Endpoints –
- Amazon Lambda functions endpoint.
- HTTPS web service endpoint.
In this blog, we will look at using Amazon Lambda functions as endpoint. So let’s see how to build this endpoint and once it is ready we will come back to this 4th step to connect our AWS TRIVIA skill to the Amazon Lambda endpoint.
Step II — Creating a Lambda Function endpoint on AWS.
- Login to AWS Console.
2. In the search box, type lambda and click on the search result.
3. Select the right region.
Note: Check your AWS region. AWS Lambda only works with the Alexa Skills Kit in two regions: US East (N. Virginia) and EU (Ireland). Make sure you choose the region closest to your customers. If you choose any other region other than these 2, it will not work.
4. Click on Create function, select Blueprints and in the filter type — alexa-skill-kit-sdk-triviaskill. Select the trivia skill and click on Configure.
5. Add a name for the function — awsTriviaFunction.
If you have already created a lambda function before, you might have existing role — lambda_basic_execution. If yes, under Role, select — Choose existing role and select lambda_basic_execution. If for some reason, this role is not available, clieck on Create a custom role.
6. Once you do that, you will be redirected to a new page. In that, under IAM Role, select — Create a new IAM Role and in Role Name add — lambda_basic_execution and Click on Allow
Note: Make sure that your user has the required permissions to perform these operations, else you will get permission errors.
7. Once these configurations are done, click on Create Function The lambda function will be created.
Copy the ARN available on the top right corner. You will need it later in Alexa Skill Endpoint.
8. Now the next step is to add a trigger for the lambda function.
- Select AWS Skill Kit from the left menu and it will be added in the triggers list.
- Under Configure triggers, enable Skill ID verification and add your Skill ID. This will make sure that only your skill can send requests to this function.
The Skill ID is available on the Skills Dashboard in the Amazon Developer Console.
Click on Add button.
9. Once this is done, click on Save button and your lambda function is ready and configured.
Step III — Integrating Alexa Skill with Lambda function.
Now that our lambda function is ready, let’s configure the 4th point — Endpoint in our Alexa Skill.
Click on Endpoint, select AWS Lambda ARN and paste the ARN of lambda function copied earlier in the Default Region and click on Save Endpoints.
Your Alexa skill is now ready for testing.
Step IV — Testing the Alexa Skill
Click on Test tab to launch Alexa Testing Simulator. Make sure that Test is enabled for this skill. It will ask for the permission to Use your microphone. Click on Allow so that you can test it with voice commands.
You can type in your instructions or use the voice commands by holding on the mic icon. Make sure that you keep the icon pressed till you say and complete your instructions.
Let’s start with the instruction — aws trivia
You will receive a voice response and you can also see corresponding the input and out json requests.
Next instruction — my answer is three
Note: Alexa doesn’t yet support and work well with numeric values. You will have to put the option in word instead of numeric value — as in one, two, three and four.
So instead of typing — my answer is 2, you should type my answer is two. This will be an issue only when you are typing the instructions. If you use voice commands, Alexa will automatically pick it up as two instead of 2.
Voila!!! You have successfully built your first Alexa Skill with AWS just within an hour. Congratulations!!!