The ASK SDK requires intent handlers for each of the expected intents. We will create intent handlers for our application-specific intent (for example, a self-intro intent), the launch intent—as well as help, stop, and cancel intents for the inbuilt intents—a fallback intent, and a session end request intent. We will then create a parent lambda handler class that registers all these intent handlers.
Let's start by defining the dependency of ASK SDK for Java in the POM file, as follows:
<dependency>
<groupId>com.amazon.alexa</groupId>
<artifactId>ask-sdk</artifactId>
<version>${ask.sdk.version}</version>
</dependency>
The POM file also has dependencies for aws-lambda-java-core. The ask.sdk.version property is defined along with other properties in the parent project, POM serverless-cookbook-parent-aws-java:
<ask.sdk.version>2.11.2</ask.sdk.version>
We can create the Java Lambda project with the following package structure:
The SelfIntroIntentHandler class defines the speech text that does the introduction, as follows:
public class SelfIntroIntentHandler implements RequestHandler {
@Override
public final boolean canHandle(final HandlerInput input) {
return input.matches(Predicates.intentName("SelfIntroIntent"));
}
@Override
public final Optional<Response> handle(final HandlerInput input) {
String speechText = "Hello, this is Alexa saying intro for Heartin Kanikathottu. "
+ "Heartin is a senior software engineer and blogger with around 11 years of IT experience. "
+ "He likes to share his technical knowledge through his blogs such as CloudMaterials.com "
+ "and Java J EE dot com. "
+ "He also likes to mentor juniors and take sessions at meetups and conferences.";
return input.getResponseBuilder()
.withSpeech(speechText)
.withSimpleCard("SelfIntro", speechText)
.build();
}
}
The LaunchRequestHandler class defines the speech text for the app launch, as follows:
public class LaunchRequestHandler implements RequestHandler {
@Override
public final boolean canHandle(final HandlerInput input) {
return input.matches(Predicates.requestType(LaunchRequest.class));
}
@Override
public final Optional<Response> handle(final HandlerInput input) {
String speechText = "Welcome to the Self Intro Alexa Skill for Heartin, you may say 'please say intro'";
return input.getResponseBuilder()
.withSpeech(speechText)
.withSimpleCard("SelfIntro", speechText)
.withReprompt(speechText)
.build();
}
}
The HelpIntentHandler class defines the speech text for the inbuilt intent AMAZON.HelpIntent, as follows:
public class HelpIntentHandler implements RequestHandler {
@Override
public final boolean canHandle(final HandlerInput input) {
return input.matches(intentName("AMAZON.HelpIntent"));
}
@Override
public final Optional<Response> handle(final HandlerInput input) {
String speechText = "You you may say 'please say intro'!";
return input.getResponseBuilder()
.withSpeech(speechText)
.withSimpleCard("SelfIntro", speechText)
.withReprompt(speechText)
.build();
}
}
The CancelandStopIntentHandler class defines the speech text for the for the inbuilt intents AMAZON.StopIntent and AMAZON.CancelIntent:
public class CancelandStopIntentHandler implements RequestHandler {
@Override
public final boolean canHandle(final HandlerInput input) {
return input.matches(intentName("AMAZON.StopIntent").or(intentName("AMAZON.CancelIntent")));
}
@Override
public final Optional<Response> handle(final HandlerInput input) {
return input.getResponseBuilder()
.withSpeech("Goodbye buddy")
.withSimpleCard("SelfIntro", "Goodbye")
.build();
}
}
The FallbackIntentHandler class defines the speech text for the fallbacks when no intents match, as follows:
public class FallbackIntentHandler implements RequestHandler {
@Override
public final boolean canHandle(final HandlerInput input) {
return input.matches(intentName("AMAZON.FallbackIntent"));
}
@Override
public final Optional<Response> handle(final HandlerInput input) {
String speechText = "Sorry buddy, I don't know that. You can say try saying help!";
return input.getResponseBuilder()
.withSpeech(speechText)
.withSimpleCard("SelfIntro", speechText)
.withReprompt(speechText)
.build();
}
}
We will also define a SessionEndedRequestHandler class for handling session termination requests, as shown in the following code:
public class SessionEndedRequestHandler implements RequestHandler {
@Override
public final boolean canHandle(final HandlerInput input) {
return input.matches(requestType(SessionEndedRequest.class));
}
@Override
public final Optional<Response> handle(final HandlerInput input) {
//any cleanup logic goes here
return input.getResponseBuilder().build();
}
}
Finally, we will create the Lambda handler class that extends SkillStreamHandler, as follows:
public class SelfIntroStreamHandler extends SkillStreamHandler {
private static Skill skill = Skills.standard()
.addRequestHandlers(
new CancelandStopIntentHandler(),
new SelfIntroIntentHandler(),
new HelpIntentHandler(),
new LaunchRequestHandler(),
new SessionEndedRequestHandler())
.build();
public SelfIntroStreamHandler() {
super(skill);
}
}