Building an Alexa Skill

 I recently attended an Alexa skills workshop at the excellent CodeNode venue in Moorgate. The event, hosted by Skills Matter and run by Amazon, was a freebie and I was curious to learn a bit about building skills. I’d briefly looked into it before but never really got anywhere beyond poking around in the Amazon developer portal. Turns out it’s really easy and after the 1.5 day workshop and a couple of evenings dev I was the proud author of a published Alexa skill. With that in mind I thought I’d give a quick 101 on how it’s done.

The following blog assumes a couple of things:

  • A basic knowledge of Node (there are other options but this is the language I chose and the one which the workshop focussed on)
  • Your own Echo or other Alexa enabled device (This is not essential but hard to test properly without it)
  • An Amazon developer account (you can register for the free tier)

Planning your skill

It’s probably best to start off small so, for the purposes of this run through, we’ll go for something very basic. Fact skills are very popular, some might say too popular, on the Alexa platform so let’s further pollute the pool and build one of those. Our CEO Alan hails from Preston and can often be heard extolling it’s virtues so we’ll build ‘Preston Facts’!

The first thing you want to do is work out how the speech interaction is going to work, the workshop supplied some handy work sheets to help with this – here’s a couple I prepared earlier:

This is a very simple exchange and obviously you’ll want to plan a few more flows, we’ll go into more detail about how conversations work later but it gives you an idea. There’s some sample sheets here and here if you want to have a go but for now, let’s forge on.

Initiating your skill

Now we know what we’re building let’s log into Amazon’s developer console and get started.

Click the ALEXA tab at the top and then the ‘Get Started >’ button under Alexa Skills Kit, chances are if you’re following this tutorial you’ll see an empty list but once you’re an accomplished developer you can view your list of skills here.

As we’re building a new one though click ‘Add a New Skill’ at the top right. Now you’ll see the skill configuration screen, this is where you set up your skill, link it up with your code, test it and, ultimately, publish it. We’re building a custom skill so check the first radio button then fill in the fields as per the screenshot below.

I won’t go into details about the different options here, but as a quick overview;

  • we’re initially aiming this skill at the UK so that’s our core language. It’s easy to add more later, though skills can only be published in English, US English and German.
  • The name is just your apps name that users see when they are downloading skills
  • The invocation name is essentially the word that people use when they are interacting with your skill. It’s worth taking a quick look at the invocation name guidelines before you choose your app name.
  • Don’t worry about the the Audio Player option – we’re not going to use it.

Once you’re done, click ‘Next’.

Now we’re taken to the Interaction model page and this is where the dialogue design we did earlier comes in handy. Here we need to architect the way in which our skill interacts with the end user. This is in JSON format and for the purposes of this demo it’s going to be very simple. We’re going to set up a single intent. The ‘DeliverFact’ intent. We’re also going to chuck some out-of-the-box Amazon intents in there, I’ll go through what all these are later. So your intent schema should look something like this:

    "intents": [
    { "intent": "DeliverFact" },
    { "intent": "AMAZON.YesIntent" },
    { "intent": "AMAZON.NoIntent" },
    { "intent": "AMAZON.HelpIntent" },
    { "intent": "AMAZON.StopIntent" },
    { "intent": "AMAZON.CancelIntent" }

Ignore the slots for now, we’ll look at that in part II, and move on to the ‘Sample Utterances’ – this is where you link speech patterns to the intents you’ve defined. For the purposes of this demo we just have one intent: ‘DeliverFact’. We’re basically telling Alexa what action to fire when she hears certain speech patterns. You can add as many of these as you like but I’m keeping it simple and just using 3 for now as you can see in the below screenshot. When you’re done hit ‘Next’ again.

The next screen is the configuration page where you link your skill up to some code – so we’re going to need to write some. The easiest way to get up and running is by using an AWS Lambda and that’s what we’re going to do for this tutorial. In a new tab open up the AWS console – again, you’ll need to register for a free account if you don’t already have one.

When you’re in, make sure that you select the ‘EU (Ireland) region in the top right, this is one of only a couple of regions where the Alexa skills kit is available:

Next select ‘Services’ from the menu bar and then ‘Compute > Lambda’. Here you’ll, if you have any, see a list of Lambdas you’ve created previously. To create a new one click ‘Create a Lambda function’ at the top of the page. For your blueprint select ‘alexa-skill-kit-sdk-factskill‘. On the next screen click the empty box with the dashed border and select ‘Alexa Skills Kit’. If you’re having trouble finding any of these options it’s probably because you’re not in the right region (remember, you need to be in ‘EU (Ireland)’).

Now click ‘Next’.

Add the name you want for your Lambda function. For the sake of simplicity you might just want to use your Skill name in camel case, e.g. PrestonFacts. Replace the description placeholder with your own and leave the runtime as is. Now we need some code. Once your code becomes more complex you’ll probably want to abstract it and store it elsewhere, again we’ll go into that in part II, but for now we’re going to write it inline. We’ll replace all the code that’s prefilled, for the purposes of this demo we need to use the ‘alexa-skill-kit-sdk-factskill’ template so that the ‘alexa-sdk’ node module is available to us. In the future if you abstract your code you’ll have to upload this module yourself.

Anyway, code:

'use strict';
const Alexa = require('alexa-sdk');
const prestonFacts = [
  ‘Preston was the birthplace of the UK’s motorway network’,
  ‘Preston invented the word teetotal’,
  ‘The first KFC outside North America was built in Preston’,
  ‘Butch Cassidy had family from Preston’,
  ‘Preston was the first UK town to be lit by gas’,
  ‘Preston is believed to have the UK’s longest row of red phone boxes’,
  ‘Prestons bus station is a celebrated piece of Brutalist architecture’

Here we’ve required the Alexa SDK module and built an array of facts that can be delivered. You can add as many of these as you want or even link up to an API or database – it’s just Node. Now we need to define our handlers. These are the intents we created earlier:

const handlers = {
 'LaunchRequest': function () {
   this.emit(':ask', 'Hello, would you like a fact about preston?', 'Would you like a fact about Preston?'); },
 DeliverFact: function () {
 'GetFact': function () {
   const factIndex = Math.floor(Math.random() * prestonFacts.length);
   const randomFact = prestonFacts [factIndex];
   const anotherFact = 'Would you like another fact about Preston?';
   this.emit(':ask', `${randomFact}, ${anotherFact}`, anotherFact);
 'AMAZON.HelpIntent': function () {
   const helpMessage = 'You can ask me for a random fact about Preston';
   this.emit(':ask', helpMessage, helpMessage);
 'AMAZON.YesIntent': function () {
 'AMAZON.NoIntent': function () {
   this.emit(':tell', 'Goodbye');
 'AMAZON.CancelIntent': function () {
   this.emit(':tell', 'Goodbye');
 'AMAZON.StopIntent': function () {
   this.emit(':tell', 'Goodbye');
 'SessionEndedRequest': function () {
   this.emit(':tell', 'Goodbye');

So here we’ve configured our responses – let’s take a look at one: this.emit(‘:ask’, ‘Hello, would you like a fact about preston?’, ‘Would you like a fact about Preston?’);. There are 3 arguments to the ‘emit’ function call – type, message and follow up. We use the ‘:ask: type in order to ensure the session is kept open when Alexa has finished talking, the other type we’ll use for this skill is ‘:tell’, this will deliver it’s message and then close the session. When Alexa has spoken the original message, she’ll wait 8 seconds for a response and then deliver the follow up message, if there is still no response after another 8 seconds the session will end.

Now finally we need to register our handlers and execute the function, and that’s the coding side done!

exports.handler = (event, context) => {
  const alexa = Alexa.handler(event, context);

You can leave all the other settings on this page as they are, the only other one we care about for now is the ‘Lambda function handler and role’. Here we define the permissions for executing our functions. To keep it simple you can just choose ‘Create new role from template(s)’ from the the ‘Role’ dropdown, give it the same name as your function, so PrestonFacts in my case, and leave the templates field blank. Click ‘Next’ and review your Lambda on the next page, then, when you’re happy, click ‘Save’.

When the save is complete you should see an ‘ARN’ at the top right of the page. Copy it to your clipboard, then head back over to your Amazon skill and, on the ‘Configuration’ page, select the AWS Lambda option then the ‘Europe’ check box and paste it into the input field that appears. Finally, hit ‘Next’ and that’s basically it, presuming you succeded in all the previous steps you now have a fully functional Alexa skill ready for testing and publishing.

You can now test your skill by typing sample questions into the ‘Enter Utterance’ field and clicking the ‘Ask ’ button. You’ll see the request object that is passed to your Lambda on the left and the response on the right. In part II we’ll go into more detail on how you can use this JSON to test your Lambdas directly. There’s a handy tool here that you can use to emulate Alexa more realistically and you can also, of course, test on your Alexa device where the dev skill should automatically be enabled providing you’ve used the same account to register it as you did to create your AWS account.

Finally you just need to fill in some details on the ‘Publishing Information’ and ‘Privacy Compliance’ tabs and then you’re ready to publish. I look forward to trying out your skill!

Thanks for reading and please check out part II when I get round to writing it where we’ll cover custom slots, session attributes and maintaining your code remotely. Oh, and finally, don’t forget to check out Preston Facts on the Alexa skill store!

Leave a Reply

Your email address will not be published. Required fields are marked *