Alexa Skill - Part 2

Reading time ~1 minute

The code for the Skill is written in JavaScript and uses npm to manage package dependencies. The ask-sdk-core and ask-sdk-model packages are included when we ran ask new.

lambda/custom/index.js

I’m going to break up the explanation of the code so I can explain it in a digestable fashion.

Let’s start slow. First, I import the ask-sdk-core.

const Alexa = require('ask-sdk-core');

I use the ask-sdk-core when I register the intent handlers (described below). The skillbuild.addRequestHandlers() registers handlers for launch, help, cancel and stop, session end, and the non-built-in Hello intent.

const skillBuilder = Alexa.SkillBuilders.custom();

exports.handler = skillBuilder
  .addRequestHandlers(
    LaunchRequestHandler,
    HelpIntentHandler,
    CancelAndStopIntentHandler,
    SessionEndedRequestHandler,
    HelloIntentHandler
  )
  .addErrorHandlers(ErrorHandler)
  .lambda();

Anatomy of an Intent Handler

The intent handler requires two functions canHandle(handlerInput) and handle(handlerInput)

const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  },
  handle(handlerInput) {
    const speechText = 'Welcome to the Alexa skill for Shepherd of the Valley Lutheran Church.';
    const repromptText = "Say hello."

    return handlerInput.responseBuilder
      .speak(speechText)
      .reprompt(repromptText)
      .withSimpleCard('Hello', speechText)
      .getResponse();
  },
};

The canHandle(handlerInput) function returns true for all the request types it handles. In the case of the LaunchRequestHandler it supports the LaunchRequest request type. The handle(handlerInput) function is the actual response to the intent. The function shows how the handlerInput.responseBuilder function is used to build the response. speak takes a string that is the text of the response. reprompt takes a string that will be used to further the conversation if there hasn’t been any interaction after 8 seconds.

There are two other types of requests I’ll be handling in this tutorial and they are IntentRequest and SessionEndedRequest. SessionEndedRequest is used to cleanup any state we need for the session. I’m not sure there will be much to it as I don’t know if I will need any state for this Skill. I will explore IntentRequest in the next post. It will be used for some default intents, but it is also the mechanism used to do the interesting, custom parts of the skill.

Please send me (brian@yamabe.net) any comments or corrections.

How Do I Document?

I don't have an answer to the question. I'm just riffing here. Trying to figure out how to create value for a yet to be determined audien...… Continue reading

Alexa Skill - Part 4

Published on September 19, 2018

Alexa Skill - Part 3

Published on September 16, 2018