Centurion, South Africa info@code2bits.com On Request

How to Build an Alexa Skill - Part 1

This post is the first in the “Building an Alexa Skill” series where I will illustrate how to implement Alexa Skills. In this post, I will show you how to implement a simple “Hello World” Alexa skill making use of the Alexa Skills Kit (ASK) SDK for Java.

Requirements

The following list defines the technologies and libraries I used to implement the sample code:

Introduction

Amazon allows developers to build and publish skills for Alexa using the Alexa Skills Kit (ASK). Alexa Skills Kit is a collection of self-service APIs, tools, documentation, and code samples. With ASK, anyone can leverage Amazon’s knowledge in voice design to build quickly and easily.

The first example of the “Building Alexa Skills” series is a simple Hello World example. This Alexa Skill will respond to certain utterances by saying “Hello World”. There are also default handlers included to handle “Stop”, “Cancel” and “Help” commands.

Example: Hello World (Java Code)

The Hello World Alexa Skill is written in Java making use of the Alexa Skills Kit (ASK) SDK for Java. The example can be downloaded from GitHub from this link.

NB: The java code needs to be compiled and assembled with all the dependencies into a Jar file which will be deployed as a Lambda function in you AWS account.

HelloWorldStreamHandler

The HelloWorldStreamHandler class provides the handler when hosting the “Hello World” skill as an AWS Lamda function. Lambda functions written in Java must have a RequestStreamHandler class for the handler. The HelloWorldStreamHandler class is the entry point for the custom skill from where all inbound requests are routed to the appropriate handlers. All the applicable Handlers are instantiated and registered with the SDK instance created by the Skills builder.

NB: Please note that during the deployment of the Alexa Skill in the Alexa Developer Console, you will get a Skill ID which needs to be added to your code. See comment below.

private static Skill getSkill() {
    return Skills.standard()
        .addRequestHandlers(
            new CancelIntentHandler(),
            new FallBackIntentHandler(),
            new HelloWorldIntentHandler(),
            new HelpIntentHandler(),
            new LaunchRequestHandler(),
            new SessionEndedRequestHandler(),
            new StopIntentHandler())
        .withSkillId("") // Add your SkillID here
        .build();
}

The details of the HelloWorldStreamHandler class can be viewed here.

HelloWorldIntentHandler

The HelloWorldIntentHandler contains the implementation to build a response from the handler input when a HelloWorldIntent occurred. A HelloWorldIntent is triggered when the user say any of the utterances as per the configuration for the HelloWorldIntent.

The HelloWorldIntentHandler is responsible for taking a request (HandlerInput) and generating a response for the “Hello World” skill. A SimpleCard is also generated with a title and the speech text as part of the body.

    private String cardTitle = "Code2Bits: HelloWorld";
    private String speechText = "Hello World";    
    
    @Override
    public Optional<Response> handle(HandlerInput input) {
       return input.getResponseBuilder()
                .withSpeech(speechText)
                .withSimpleCard(cardTitle, speechText)
                .build();
}

The details of the HelloWorldIntentHandler class can be viewed here.

Other Classes

  • LaunchRequestHandler: The details of the LaunchRequestHandler class can be viewed here.
  • CancelIntentHandler: The details of the CancelIntentHandler class can be viewed here.
  • FallBackIntentHandler: The details of the FallBackIntentHandler class can be viewed here.
  • HelpIntentHandler: The details of the HelpIntentHandler class can be viewed here.
  • StopIntentHandler: The details of the StopIntentHandler class can be viewed here.
  • SessionEndedRequestHandler: The details of the SessionEndedRequestHandler class can be viewed here.

Speech Assets

The speech assets contains the configuration that is used to setup the Alexa Skill within the Alexa Developer Console.

  • SampleUtterances.txt: The details of the SampleUtterances.txt file can be viewed here.
  • IntentSchema.json: The details of the IntentSchema.json file can be viewed here.
  • SkillBuilder.json: The details of the SkillBuilder file can be viewed here.

Build & Assemble the Code

The HelloWorld project code should be cloned to your local machine by making use of git. The project contains a maven project file which allows you to build and assemble the code into a deployable JAR file. The project code can be found here.

Use the following command to build and assemble the code and all the dependencies into a single JAR file. This file can be found in the target directory after the command has successfully executed.

$ mvn assembly:assembly -DdescriptorId=jar-with-dependencies package

Deploy the Code

The deployment process needs to take into consideration that you need a Skills ID from the Alexa Developer Console, before you compile your Java code and deploy it on AWS. You can use the following link to see how to deploy Java Code and the Speech Assets to the different environments.

  • Deploy Java Code to AWS.
  • Deploy Speech Assets to Alexa Developer Console.
  • Register Alexa skill with your device.

Test The Code

The following are some of the utterances that can be used to invoke the different intents.

Intent: HelloWorldIntent

  • Alexa, ask 'Hello World' to 'say hello'
  • Alexa, ask 'Hello World' to 'say hello world'
  • Alexa, ask 'Hello World' to 'say hi'
  • Alexa, ask 'Hello World' to 'say hi world'
  • Alexa, ask 'Hello World' to 'how are you'
  • Alexa, open 'Hello World' ... hello.
  • Alexa, open 'Hello World' ... hi.

Separate intents are provided for cancelling and stopping for situations in which your skill needs to distinguish between the two concepts. This let “cancel” have a meaning distinct from “stop.” However, in most skills, the behaviour should be the same for both.

Intent: AMAZON.StopIntent The Stop Intent let the user stop an action, but remain in the skill, or let the user completely exit the skill.

  • Alexa, open 'Hello World' ... Alexa, Stop.
  • Alexa, open 'Hello World' ... Alexa, Off.
  • Alexa, open 'Hello World' ... Alexa, Shut Up.

Intent: AMAZON.CancelIntent The Cancel Intent let the user cancel an action, but remain in the skill, or let the user completely exit the skill.

  • Alexa, open 'Hello World' ... Alexa, Cancel.
  • Alexa, open 'Hello World' ... Alexa, Never Mind.
  • Alexa, open 'Hello World' ... Alexa, Forget It.

Intent: AMAZON.HelpIntent The Help Intent provide help about how to use the skill.

  • Alexa, open 'Hello World' ... Alexa, Help.
  • Alexa, open 'Hello World' ... Alexa, Help me.
  • Alexa, open 'Hello World' ... Alexa, Can you help me?

Summary

This post is the first in the “Building an Alexa Skill” series where illustrate how to implement Alexa Skills. This post explained how to create a simple ‘Hello World’ Alexa custom skill making use of the Alexa Skills Kit (ASK) SDK for Java.