April 11, 2020
Before kicking things off, let's go over what this post does not cover. I won't be going over how to set up an AWS account or how to create an account in the Alexa developer console. I'll leave you to figure those things out, unless you've set them up already, of course. Additionally, I won't be covering what a lambda function is so, if you've never worked with one and/or know little about them, it's up to do some Googling if you want to learn more. When you're ready, head on back here to learn how to set up your first Alexa skill.
To create an Alexa skill there are 2 things you'll need to do:
- Create a skill in the Alexa developer console.
- Create a lambda function in AWS.
Creating a Skill in the Alexa Developer Console
Head to the create new skill page and start by giving your skill a name. If you're not sure what you actually want to create yet, just copy what I do so you can become familiar with the flow. When you have an idea, you can simply repeat the process.
I'm going to call this demo skill 'Super Awesome Facts' and set my language to English (AU). Feel free to select whatever language you'd like. You can add extras once your skill has been created.
Next, select the model for your skill and choose hosting method. I've opted for 'Custom' for my model and Node.js to host my skill's backend resources.
And it's as simple as that! You're now ready to create your skill! Click the 'Create skill' button in the upper right-hand corner.
On the next page you get to choose the template that you'll use to start building out your skill. If you're following along with me, select the 'Fact Skill' and click 'Choose'.
It will take about a minute for your skill to be created. When it's ready, you'll find yourself on the following screen:
Take a moment to watch the video and poke around if you'd like. When you're back we'll change our invocation name, briefly go over what intents are before heading to AWS to create our lambda function.
The invocation name is essentially the name of your skill. Think about what you want your users to say to get Alexa launch you skill and add that to the 'Skill Invocation Name' input field.
I've given my demo skill the invocation name of 'super awesome facts' which means, anytime someone wants to use my skill they would say something like, 'Alexa, open super awesome facts' or 'Alexa, launch super awesome facts' and Alexa would then start my skill and say whatever I set as my welcome message.
Intents are actions that Alexa takes in response to something the user says. For example, if you were building a joke skill, a user might something something like, 'tell me a joke'. This would fire off an intent, probably called something like
TellJokeIntentand Alexa would then do whatever we have specified for her to do within that intent in our lambda function.
If you click on the 'Intents' dropdown in the left-hand panel you'll see that there are 2 different types of intents:
- Built-In Intents: these are intents that are attached to every Alexa skill. They allow users to do basic things like ask for help when using a skill, stop a skill, navigate home etc. These intents can be modified if that is something that you need to do for your skill but you may find you never need to alter them.
- Custom Intents: custom intents are the intents that are unique to your skill. If you're creating a simple skill, you may only have one custom intent but if your skill is complicated and requires a lot of back and forth interactions between Alexa and the user, you'll have multiple.
With our fact skill demo, we have a 'GetNewFactIntent' that was automatically generated when our skill was created. This intent will allow a user to say something to Alexa and for Alexa to reply with a fact.
What a user says is known as an 'utterance' in the Alexa realm and, as you can see, a number of sample utterances have been automatically created for us. Now, if you're really planning to build a fact skill you'll probably want to delete some from the list of utterances that currently exist. No sense giving the user the option to utter, 'give me a space fact' if your fact skill focuses on facts about dinosaurs.
Additionally, you will may want to add extra utterances. The more utterances you have for an intent, the better the user experience. If a user says something that you don't have mapped to an intent, Alexa won't understand what the user wants her to do and will not be able to fire the correct intent. Come up with as many variations as you can, ask friends and family what they would say to get Alexa to respond with a certain intent, and add all of those to your list of sample utterances.
Click 'Save Model' to save any changes you've made to the invocation name and intents, then click 'Build Model' so your skill is updated with the changes you have made.
Creating a Lambda Function in AWS
First things first, before we kick off our lambda function, make sure you're in the correct region. Check what it says next to your name in the upper right-corner and change if needed.
Once that's taken care of, in the AWS console, type 'lambda' into the search field and click the top result.
On the next screen, click the orange 'Create function' button to go to the following screen:
Click on 'Browser serverless app repository' from the options listed at the top. This will allow us to build a lambda function that can work with Alexa.
If you're following along but building your own skill, browse through the available options to select a skills kit that best meets your needs. For those of you creating a fact skill with me, go ahead and click on the 'alexa-skills-kit-nodejs-factskill' option.
The last thing you need to do before deploying your lambda function is to give it a name. For simplicity, I will give my function the same name of my skill.
Within seconds of clicking 'Deploy', your lambda function will be created. To view your function, click on the first resource listed under 'Resource'.
Scroll down to see your function code. We'll edit this soon but let's connect our function to our Alexa skill first.
Connecting Our Skill to Our Function
Back up near the top of the screen on the right-hand side, you'll see your function's ARN. An ARN, Amazon Resource Name, is a file naming convention used to locate resources in AWS public cloud. We'll need this to connect our Alexa skill to our function. Click the clipboard icon and head back over to the Alexa developer console.
In the panel on the left, click on 'Endpoint' and then delete the existing ARN that's in the default input field, and paste in the ARN you just copied from your lambda function.
Scroll back up and click the 'Save Endpoints' button. You should then see the following success message pop up in the bottom corner of your screen. If you get an error message, I recommend going back through this post carefully to see what you have missed.
Hooray! Your function and skill are now connected! It's time to edit your function code.
Editing Your Lambda Function
Your lambda function will already have most of the code you need for your skill but it will be missing any custom intents or utterances that you added in the Alexa console. You can always manually edit your code or you could take the easy option and use the Alexa Code Generator.
To automatically generate the code needed for you lambda function, click on 'JSON editor' in the panel in the Alexa console and then copy the JSON code you see to the right.
Go to the Alexa Code Generator, paste the JSON into 'Language Model JSON' box and then click 'GENERATE CODE' over to the right. Voila! Your code for your lambda function has been created for you. Copy that, and head back to AWS.
Delete the existing code in your index.js file and replace it with your automagically generated code. Now, before saving this there are 2 changes you'll need to make (assuming these haven't been fixed by the time you read this post):
- change aws-sdk to aws-sdk-core: there is no longer a aws-sdk package
- if the line
const skillBuilder = Alexa.SkillBuilders.standard();exists in your code, change it to
const skillBuilder = Alexa.SkillBuilders.custom();
Click 'Save' just above your file to install the required dependencies and save the changes to your lambda function. Now you're ready to test your skill!
Testing Your Alexa Skill
Back in the developer console, click on 'Test', then in the dropdown select 'Development'. This is a really handy feature that makes it easy to test your Alexa skill if you don't have a device handy.
In the input field, type what a user would say to open your skill e.g. 'Alexa, open Super Awesome Facts' and hit enter. Alexa should then response with the greeting currently set in your
LaunchRequest_Handlerintent. You can easily change this so that Alexa introduces your skill to your users in whatever way you see fit. It's a good idea to prompt a user with what to say next so first time users know how to use the skill correctly.
Now that your skill is open, you can interact with Alexa by using any of the built-in or custom intents e.g. 'Alexa, tell me a fact'.
Obviously this is where we would want Alexa to return a fact to our users but we haven't yet changed the code to support this. We have, however, got our Alexa skill and lambda function up and running! Where you take your skill from here is up to you.
I will be writing a series of Alexa related posts so stay tuned! The first 2 in my pipeline include:
- how you can set your lambda function up on your machine and deploy locally so you're not stuck working in the lambda console
- debugging your lambda function.