Not long ago, smart assistants were thought of as fun, gimmicky devices used to tell jokes and check the weather. Since the increasing availability of developer kits and tools, these devices have grown in their capabilities and are supporting many more useful and powerful applications from banking support to smart home communication to integrated applications with mobile. Alexa is one of the most popular smart assistants and offers over 25,000 skills to help make tasks simpler and more engaging for users. With this rise in natural language processing, it becomes more necessary for developers to know how to create and publish Alexa skills.
In this tutorial, we will cover the basics of getting started with Alexa development and we will create a simple skill that will recite available ice cream flavors for an ice cream truck.
Key Terms
Intent
An intent represents an action that fulfills a user's spoken request. Intents can optionally have arguments called slots. (ex: ReadFlavors)
Utterances
Utterances are a set of likely spoken phrases mapped to the intents. This should include as many representative phrases as possible. (ex: "Tell me my ice cream options.")
Custom slot types
Custom slot types are a representative list of possible values for a slot. Custom slot types are used for lists of items that are not covered by one of Amazon's built-in slot types. Some built-in slot types include date, location or name.
For more information, visit Amazon's guide.
Prerequisites
- Log in to or create an AWS account. Go here to create one.
- IDE of choice. I will be using Visual Studio Code.
- Installed Node.js. More information on Node.js.
- Access to the Alexa Developer Portal. Click here to sign in.
Create a Function in AWS
When you log in to the AWS management console, select Lambda from the list of services. Lambda is the serverless computing option that AWS provides and is used to run and communicate with your Alexa skill.
Note: Make sure the region in the top right of your console is set to US East (N. Virginia). This region supports lambda functions triggered by Alexa skills.
From the lambda console, choose Create function and complete the following steps:
- Select "Author from Scratch."
- Name your function. I named mine "Ice Cream Truck."
- Select "Node.js 6.10" as the runtime.
- Select "Create a custom role" if you don't have one for lambda functions already. Then select "Lambda basic execution."
- When you are brought to your lambda function, copy the ARN. This amazon resource name is what you will use to connect your skill's intents and utterances to your function.
- Select "Alexa Skills Kit" from the list of triggers to trigger your function. Then click "Add" in the bottom right and "Save" in the top right to save your function's configuration.
Configure your Skill in the Developer Portal
- Sign in to the developer portal.
- Click "Alexa."
- Click "Get Started" under the "Alexa Skills Kit" option.
- Click "Create Skill."
- Enter a name for your skill and select "Custom."
The left toolbar will walk you through everything you need to create your skill. We will start with the invocation name which is used to invoke your skill through the device. I will call it "ice cream truck."
Next, we will configure the Intents. I created one called "ReadFlavors" with sample utterances. The full JSON file for my schema can be found here.
When you have configured your intents with utterances, click "Save model" and make sure there are no errors. If all goes well, build your model by clicking "Build Model."
In the left sidebar under "Endpoints," paste the ARN you copied from AWS previously next to "Default Region" and click "Save Endpoints."
Write and Upload the Code
var Alexa = require('alexa-sdk');
var flavors = ["strawberry", "chocolate", "vanilla", "mint chocolate chip"];
exports.handler = function(event, context, callback){
var alexa = Alexa.handler(event, context);
alexa.appId = 'YOUR ALEXA ID GOES HERE';
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
//Handles the launch request
'LaunchRequest': function () {
this.emit(':ask', 'Welcome to the ice cream truck! Ask to hear the flavors', 'Try asking what the flavors are.');
},
//Reads the list of ice cream flavors
'ReadFlavors': function () {
this.emit(':tell', 'Our ice cream flavors are ' + flavors.join(", "));
},
'AMAZON.StopIntent': function () {
// State Automatically Saved with :tell
this.emit(':tell', `Goodbye.`);
},
'AMAZON.CancelIntent': function () {
// State Automatically Saved with :tell
this.emit(':tell', `Goodbye.`);
},
'SessionEndedRequest': function () {
// Force State Save When User Times Out
this.emit(':saveState', true);
},
// Provide help function
'AMAZON.HelpIntent' : function () {
this.emit(':ask', 'You can say tell me flavors to hear ice cream flavors. What would you like to do?', `What would you like to do?`);
},
'Unhandled' : function () {
this.emit(':ask', `You can say tell me flavors to hear ice cream flavors. What would you like to do?`, `What would you like to do?`);
}
};
Note: You will need to copy your Alexa Skill ID from the developer portal so you can include it in your lambda function. We do this for 2 reasons: It ensures the skill you created is the only one invoking your code and it creates a connection between your skill and your lambda function to help trigger your function.
Since we are using the Alexa SDK, we will need to make sure the node modules are included in our projects .zip file. When you compress your project to upload it, include the node_modules
folder.
After you have uploaded the project to your lambda project and have ensured that it can access the index.js
file, we can test our skill in the Alexa developer portal and invoke it within the "Test" section of your skill builder.
Note: If your Alexa device is connected to your developer account, you will be able to invoke your skill directly from your device.
Congrats, you have finished your first skill! Next, we will cover the publishing process for when you create your own unique skill.
Publishing Process
When your skill is ready for publication, you can submit it to Amazon for review. The Submit for Certification button becomes available once all required fields are completed. These fields follow the testing portion of your Alexa development.
1. Log in to the Developer Portal. Navigate to the Alexa section by clicking Apps & Services and then click Alexa in the top navigation. This displays a list of your existing Alexa skills. Find the skill to submit in the list and choose Edit.
Note: There should be a green check mark next to each section. If any sections are missing the check mark, this means that at least one required field is not complete. Review the section and complete all required fields.
2. Click the Submit for Certification button. When prompted to confirm, click Yes.
3. After Amazon completes the review, you will receive an email at the address associated with your developer portal account:
If the skill has been certified, the email will provide an estimate of when it will become available to end users. If the skill could not be certified, the email provides information about the issues you need to correct. You can make any necessary changes and then resubmit.
Future Modifications
One change you can make to the lambda function is to include other AWS services, such as dynamoDB. Instead of storing your data statically, you would want it in some sort of database so you can easily make modifications to it without risking changing your code. Your lambda function would connect to your database instead and read the flavors from it.
Conclusion
After reading and working through this tutorial, you should have a basic understanding of how to create and publish an Alexa skill. The source code for this can be found here.
Next in Alexa Blog Series
In the next blog post in this Alexa series, Nick Cipollina will walk us through accessing location with Alexa.