How to build a skill with Amazon Alexa


Hello guys,

Recently, I started a new project, for the Spanish market. At this moment the market it’s in the beginning phase, I’ll probably disclosure more details in the future posts, but for the moment it’s not very important. The idea of this article it’s to help you to develop a skill from scratch or based on a starter pack, this is an example alexa-skill-starter-pack-typescript from my github account. I dont’ want to discuss too much about this starter pack, because you can find more details how to use it or where to start from in the README.md

When I recently started the research, I had few questions about we will learn below:

Where to start from ?

When I started it was very tough for me to understand where to start from, what to read first or how to know what I need to learn. If you are here, probably you started already to read some documents from Amazon and guess what ?! you already discovered that Amazon have a comprehensive documentation about how to build a custom skill and the documentation explains very well how to do it. You don’t have to read all, just to understand basics, keep reading this for help.

I started to read about what it’s an Invocation Name, Intents, Intent Slots, Utterances, Slot Types. So if you check the links, you’ll find the official definitions from Amazon, but let’s explain more simple than that.

Invocation Name

The invocation name represents the name used to invoke your skill. So if for example you want to build a skill which presents news about technology, a good invocation name can be “technology news” or “technology facts”, so this it’s the name which will open your skill, like you enter the name of a website in the web browser to open that website.

There are some requirements about choosing the invocation name and you can read them here.

Intents

So for the moment you know how to open the skill, but now we have the skill, how you can ask Alexa to open a section? Referring to the example presented before, we have a skill about technology news, but this it’s a generic subject. If you want that your skill to be able to present informations about Artificial Intelligence, Programming Languages, New Smartphones etc.., you can create an intent for each of these departments. So when users says ‘Alexa, ask technology news’, Alexa will start a new session for my skill, but after that let say that user wants news about Artificial Intelligence and he will say something like ‘Give me news about Artificial Intelligence’ (this sentence it’s an utterance, we’ll discuss in the next chapter about them), defined under Artificial Intelligence intent, so Alexa will know, that my intention it’s to get information about Artificial Intelligence.

Let’s recap, an intent represent an action wanted by user, what he needs. Here you can find some rules about how to choose the best name for your intents. As well, there are some built in intents which can be found here.

Utterances

As I already told you, an utterance represents a sentence which it’s part from an Intent and define particularly that Intent. This it’s a very important part, that you should focus on and think for all variations which point to the same idea or multiple ways that a user can ask the same thing. For example, if I want news about Artificial intelligence, you can say it as below:

  1. Offer me news about Artificial Intelligence
  2. Give me informations about Artificial Intelligence
  3. I want news about AI
  4. More information about AI

Don’t be lazy and please not overrate your user, because not all of them are smart or maybe they are not used with AI, so please have in mind that your user can be not so clever and in that way, you’ll make it better. Add as much as possible utterances you can to cover all the variations that a user can ask for an intent.

If you want to know best practices and recommendations please visit this or rules check here.

Intent Slots

So now, you know what it’s an intent and an utterance, but an intent slot it’s like a variable. For example if we have an intent to get distance between two points, point a to point b, we will have utterances like:

  1. Give me distance from Paris to New York
  2. How long it’s between Budapest to Bucharest

So we can identify two variables, pointA and pointB, now we can rewrite rules like:

  1. Give me distance from {pointA} to {pointB}
  2. How long it’s between {pointA} to {pointB}

Alexa will match the value in his slot. You can read more about how to identify slots inside utterances or best practices here. A slot can have a type, so if you want to know how to assign a slot type to a slot read here.

Slot Types

A slot type represents the type of slot, it’s like a variable type. There are default value builds by Amazon. If you want to discover best practices you can find here

You can exercise more by doing on your own in the Developer Console

How to run a server locally ?

You can do it simple, just by downloading [ngrok](https://ngrok.com/) and create a server with the following code

import * as express from "express";
import * as bodyParser from "body-parser";
import { LambdaHandler } from "ask-sdk-core/dist/skill/factory/BaseSkillFactory";
import { RequestEnvelope } from "ask-sdk-model";
import { AddressInfo } from "net";

import { handler } from './lambda/custom/index'

// Convert LambdaFunction to RequestHandler

function ConvertHandler(handler: LambdaHandler): express.RequestHandler {
    return (req, res) => {
        handler(req.body as RequestEnvelope, null, (err, result) => {
            if (err) {
                return res.status(500).send(err);
            }
            return res.status(200).json(result);
        });
    };
}

// create server
const server = express();
const listener = server.listen(process.env.port || process.env.PORT || 3000, function () {
    const { address, port } = listener.address() as AddressInfo;
    console.log('%s listening to %s%s', server.name, address, port);
});

// parse json
server.use(bodyParser.json());

// connect the lambda functions to http
server.post("/", ConvertHandler(handler));

Take a look at this code.

Open Terminal and run

$ ./ngrok http -bind-tls=true -host-header=rewrite 3000

Copy the https link and go to Alexa Console, under Endpoint section, select TTPS and paste the link in Default Region input field. From the below dropdown, choose My development endpoint is a sub-domain of a domain that has a wildcard certificate from a certificate authority.

Every time when you run the ngrok, you need to update the endpoint url.

Should I choose Lambda Function or HTTPS server ?

Depends by your skill, but probably the most of you can use Lambda Function.

Why to choose Lambda:

  • you don’t need to administrate any resource
  • don’t need SSL certificate
  • don’t need to verify where requests come from
  • AWS Lambda run only when a request comes
  • First 1M requests per month are free. Read more about pricing

Read here more about connecting Alexa with AWS Lambda or here to create a Lambda for Alexa.

Requirements for HTTPS Web Service:

  • The service must be Internet-accessible.
  • The service must adhere to the Alexa Skills Kit interface.
  • The service must support HTTP over SSL/TLS, leveraging an Amazon-trusted certificate.
  • The service must accept requests on port 443.
  • The service must validate that incoming requests are coming from Alexa.

If you want to read more about configurations and requirements check here.

Where to start coding from ?

Before to start any code, I suggest you to choose an SDK, if you want to code in javascript, SDK v2 it’s a better option than v1 and after that, you can take a look throw other open-source projects. You can find basic examples on Alexa’s Github profile, but can search for more: alexa-typescript-hello-world, alexa-romanian-radio, alexa-lambda-typescript.

Resources