Alexa Skill with Java — How to build your first Alexa Skill in 20 minutes with Java

Rohit Sharma
7 min readApr 14, 2018

--

You have probably heard of Alexa Echo and other devices in which you can say “Hey Alexa” and it will respond to your voice.

First Alexa Skill with Java in 20 Minutes

So today, we are going to build a “Hello World” Alexa Skill with Java.

I have also published an another article to use persistence and variables with Alexa skill and Java.

But for now, let focus on this.

The things you need are:

  1. Eclipse (Or Any other IDE compatible with Maven)
  2. Maven
  3. An account on Amazon AWS (If you don’t have you can make one its free)
  4. An Amazon Developer Account (Take seconds to make one)

Also have a look at :

Amazon Web Service : Overview (Free for a limited time)

Now make sure you have Maven integration in Eclipse, if you don’t have then follow this tutorial.

So let’s start builder our first Alexa Skill

Step 1: Open Eclipse, Go to File →New →Others and there select Maven

Step 2 : Click Next, and check the Skip Archetype box and click next.

Step 3: Enter any package name and the name of your project in Group Id and Artifact Id after that click Finish.

Step 4 : Open your pom.xml and add this dependency:

<dependencies>
<dependency>
<groupId>com.amazon.alexa</groupId>
<artifactId>ask-sdk</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>

Step 5 : Now there are two things that handle things in Alexa. First are handlers and other a StreamHandler class that works as an entry point to our program. So, make two packages one for handlers and other for the StreamHandler class. Here, I made two packages with name :

com.example.handlers
com.main

Step 6 : First we will code for the handlers. Now Alexa has many inbuilt handlers that we always need to use. The main advantage of these handlers are that we don’t need to code our logic, Amazon is already providing us the solution. For example, there is an intent “StopIntent” that will work when the application stops by the user. So, we are going to make four classes to handle our inbuilt intent which will be:

CancelandStopIntentHandler
HelpIntentHandler
LaunchRequestHandler
SessionEndedRequestHandler

Step 7 : Make a new class in handler package and name it as “CancelandStopIntentHandler” and add this block of code:

package com.example.handler;import java.util.Optional;import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import static com.amazon.ask.request.Predicates.intentName;
import com.amazon.ask.model.Response;
public class CancelandStopIntentHandler implements RequestHandler {

public boolean canHandle(HandlerInput input) {
return input.matches(intentName("AMAZON.StopIntent").or(intentName("AMAZON.CancelIntent")));
}
public Optional<Response> handle(HandlerInput input) {
String speechText = "Bye Bye";
return input.getResponseBuilder()
.withSpeech(speechText)
.withSimpleCard("HelloWorld", speechText)
.build();
}
}

Step 8: Make a new class “HelpIntentHandler” and the following lines of code:

package com.example.handler;import java.util.Optional;import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import static com.amazon.ask.request.Predicates.intentName;
import com.amazon.ask.model.Response;
public class HelpIntentHandler implements RequestHandler {

public boolean canHandle(HandlerInput input) {
return input.matches(intentName("AMAZON.HelpIntent"));
}
public Optional<Response> handle(HandlerInput input) {
String speechText = "I am here to say Hello World to You";
return input.getResponseBuilder()
.withSpeech(speechText)
.withSimpleCard("HelloWorld", speechText)
.withReprompt(speechText)
.build();
}
}

Step 9 : Create a new class in handler package and named it as “LaunchRequestHandler” and add the following lines of code:

package com.example.handler;import java.util.Optional;import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.LaunchRequest;
import com.amazon.ask.model.Response;
import static com.amazon.ask.request.Predicates.requestType;
public class LaunchRequestHandler implements RequestHandler {

public boolean canHandle(HandlerInput input) {
return input.matches(requestType(LaunchRequest.class));
}
public Optional<Response> handle(HandlerInput input) {
String speechText = "Welcome to Hello World, You can say Hello";
return input.getResponseBuilder()
.withSpeech(speechText)
.withSimpleCard("HelloWorld", speechText)
.withReprompt(speechText)
.build();
}
}

Step 10 : Add a new class in handler and name it as “SessionEndedRequestHandler” and add the following lines of code:

package com.example.handler;import java.util.Optional;import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response;
import com.amazon.ask.model.SessionEndedRequest;
import static com.amazon.ask.request.Predicates.requestType;
public class SessionEndedRequestHandler implements RequestHandler {

public boolean canHandle(HandlerInput input) {
return input.matches(requestType(SessionEndedRequest.class));
}
public Optional<Response> handle(HandlerInput input) {
return input.getResponseBuilder().build();
}
}

Step 11 : Now, the inbuilt intents are completed. Then lets start making our custom Intent handling class. This class will be responsible for saying our message to the user. So, create a “HelloWorldIntentHandler” class in handler package and this code:

package com.example.handler;import java.util.Optional;import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.dispatcher.request.handler.RequestHandler;
import com.amazon.ask.model.Response;
import static com.amazon.ask.request.Predicates.intentName;
public class HelloWorldIntentHandler implements RequestHandler {

public boolean canHandle(HandlerInput input) {
return input.matches(intentName("HelloWorldIntent"));
}
public Optional<Response> handle(HandlerInput input) {
String speechText = "I am alive Hello World";
return input.getResponseBuilder()
.withSpeech(speechText)
.withSimpleCard("HelloWorld", speechText)
.build();
}
}

Step 12 : Now time to make our final class, in the main package create a class with name “HelloWorldStreamHandler” and add the following lines of code. But, here you have to do one more thing, you have to write your Amazon app id as well.

package com.main;import com.amazon.ask.Skill;
import com.amazon.ask.SkillStreamHandler;
import com.amazon.ask.Skills;
import com.example.handler.CancelandStopIntentHandler;
import com.example.handler.HelloWorldIntentHandler;
import com.example.handler.HelpIntentHandler;
import com.example.handler.LaunchRequestHandler;
import com.example.handler.SessionEndedRequestHandler;
public class HelloWorldStreamHandler extends SkillStreamHandler {

private static Skill getSkill() {
return Skills.standard()
.addRequestHandlers(
new CancelandStopIntentHandler(),
new HelloWorldIntentHandler(),
new HelpIntentHandler(),
new LaunchRequestHandler(),
new SessionEndedRequestHandler())
.withSkillId("Your Amazon Id")
.build();
}
public HelloWorldStreamHandler() {
super(getSkill());
}
}

Also have a look at :

Step 13 : Now to get your Amazon Skill Id, go to Amazon Skill Kit Dashboard. Now create a new skill, you can name it whatever you want. I am naming it “SayHelloWorld”. In the next page select the custom model and click create skill.

Step 14 : In the left side menu, click on “EndPoint” and then select “AWS Lambda ARN”. It will open a lots of field, from there you can see your skill id. Copy it and paste it in the “HelloWorldStreamHandler” class where it says “Your Amazon Id”. Keep the Alexa Skill Kit page open because we need to fill the Lambda information later.

Step 15 : Open your terminal or Power Shell (For Windows) and locate the project root directory. This is the directory where your “pom.xml” file is located. After getting into the directory through Power Shell or any other terminal, run this command:

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

Step 16 : This command will create a “target” folder into the project directory. There you will see there are two .jar files and some other folders. Now we have to use the .jar file which named as :

HelloAlexa-0.0.1-SNAPSHOT-jar-with-dependencies

Step 17 : Go to Amazon Lambda Console, here name your function anything you like. Select “Java 8” as runtime. For role, create a new role, enter a name and from policy select “Simple Microservice Permission”. After that click on Create Function and this will take you to next page.

Step 18 : In the left side of Menu, where it says triggers, select “Alexa Skill Kit”. Now scroll down a bit, it will show a text field which will say, “Skill id”. Here, copy and paste the Alexa skill Id, which you can copy again from the Alexa skill page. After that in the upper right corner, click on save.

Step 19 : Click on “SayHelloWorld” in the Lambda Management Console and it will show a box below which say “Function Code”. In this upload the .jar file which we created in Step 16. In the handler, we have to right the path of our class which is the entry point in the program, that is “HelloWorldStreamHandler”. So add the following line in the handler box and click save:

com.main.HelloWorldStreamHandler

Step 20 : After the save is complete, from the upper right corner copy the ARN of Lambda. Now go to the Alexa Skill Kit page, hopefully you haven’t close this window and add this ARN in the “Default Region” Box. After that click on “Save Endpoints”.

Step 21 : From the left side of menu, click on “Invocation”. Here you have to write skill invocation name which can be any thing. Here, I am writing the name as “clover”. After that click on Save Model.

Step 22 : Again from the left side, click on the “Add” button that is in front of Intent. Then enter the intent name as “HelloWorldIntent” and click on Create Custom Intent.

Step 23 : It will open a page for “Sample Utterances”. Here add these lines, after that click on save model and then build model :

say hello world
say i am alive
say are you there
say is it working

Step 24 : After the build is complete, click on the “Test” button and write or say “ tell clover to say is it working”. If you follow all the steps above then it will say “I am alive Hello World”.

Step 25 : CONGRATS !! You just made your first Alexa Skill with Java. Also have a look at:

Bonus Step:

Add persistence to your skill and for that, I have written an another article which you can read here.

This eBook is your step-by-step guide to this revolutionary approach to hosting and managing your web applications. Host Your Web Site in the Cloud ($29 Value FREE For a Limited Time)

--

--

Rohit Sharma

A Java Developer who loves to learn new things and making new friends.