concept handler in category alexa

appears as: handler, The hdler, hdler, hdler, hdlers, hdlers
Voice Applications for Alexa and Google Assistant

This is an excerpt from Manning's book Voice Applications for Alexa and Google Assistant.

In the next chapter, you’ll learn how to implement your own skill. What you need to know for now is that there is a handler (WordInfoIntent here) that maps to an intent with the same name. The handler is a function that assembles what the voice assistant is going to say (in this example, by combining the length and first letter of the word with explanatory text), and then emits that for the assistant to share with the user.

Think about how you might respond to the question, “Do you really plan to sleep 14 hours?” The two answers that come immediately to mind are the most simple: “yes” or “no.” To capture those responses, we can create handlers for the built-in intents that map to yes and no responses. These are pretrained by Amazon, so they can handle the base answers, but they can also map “sure,” “nope,” “yeah,” and others to the correct intent.

Listing 5.3 Handling an affirmative user response (./lambda/custom/index.js)
const TooMuchYesIntentHandler = {
  canHandle(handlerInput) {
    const intentName = "AMAZON.YesIntent";
    const attributes = handlerInput.attributesManager.getSessionAttributes();    #1  

    return handlerInput.requestEnvelope.request.type === "IntentRequest" &&    #1  
      handlerInput.requestEnvelope.request.intent.name === intentName &&    #1  
      attributes.state === states.TOO_MUCH_CONFIRMATION;    #1  
  },
  handle(handlerInput) {
    const speech = "Okay, " + pluck(WellRestedPhrases.tooMuch);

    const attributes = handlerInput.attributesManager.getSessionAttributes();    #2  
    delete attributes.state;    #2  
    handlerInput.attributesManager.setSessionAttributes(attributes);    #2  

    return handlerInput.responseBuilder
      .speak(speech)
      .getResponse();
  }
};

exports.handler = Alexa.SkillBuilders.standard()
  .addRequestHandlers(
    TooMuchYesIntentHandler,    #3  
    WellRestedIntentHandler,
    StopOrCancelIntentHandler,
    LaunchRequestHandler
  )
  .lambda();

#1   Checks that all conditions are fulfilled
#2   Resets the attributes to remove the state
#3   Registers the new handlers

The first handler is for when users respond in the affirmative. Its canHandle method requires that the intent is AMAZON.YesIntent and that the user is in the “too much sleep” state. Because the handler has this extra level of specificity arising from the state, we register the handler at the very beginning. In the fulfillment of the intent, we’ll create the response as normal and remove the state from the session attributes, because the user has now closed the conversation by saying that Alexa heard correctly.

This handler’s most important job is to initiate the tracking. It sets the time at which tracking started, and it sets the number of times the user rose to 0. The handler also responds with a message that serves two purposes. First, it indicates that tracking has started, and it guides the user on how to end the tracking.

The handler for waking up is where the response guides the user.

Listing 5.14 The intent for when a user wakes up (./custom/lambda/index.js)
const WakingUpIntentHandler = {
  canHandle(handlerInput) {
    const intentName = "WakingUpIntent";

    return handlerInput.requestEnvelope.request.type === "IntentRequest"
    && handlerInput.requestEnvelope.request.intent.name === intentName;
  },
  async handle(handlerInput) {
    const data = await handlerInput
                        .attributesManager
                        .getPersistentAttributes();
    const attributes = handlerInput.attributesManager.getSessionAttributes();

    if (data.sleepStart) {
      attributes.state = states.WAKING;
      data.timesRisen = data.timesRisen + 1;    #1  

      const speech = "Are you up for good?";
      const reprompt = "Should I stop the sleep timer?";

      handlerInput.attributesManager.setSessionAttributes(attributes);

      handlerInput.attributesManager.setPersistentAttributes(data);
      await handlerInput.attributesManager.savePersistentAttributes(data);

      return handlerInput.responseBuilder
        .speak(speech)
        .reprompt(reprompt)
        .getResponse();
    } else {
      const speech = "Oops, by my measure you were already awake.";

      return handlerInput.responseBuilder.speak(speech).getResponse();
    }
  }
};

exports.handler = Alexa.SkillBuilders.standard()
  .addRequestHandlers(
    ...
    GoingToBedIntentHandler,    #2  
    WakingUpIntentHandler,
    ...
  )
  ...

#1   Tracks the number of times the user wakes up
#2   Registers the new handler

Waking up isn’t the end of the cycle; it’s just a waypoint due to our assumption that people could wake up several times a night. The handler increments timesRisen by 1, while placing the user in the WAKING state and asking if this is indeed the end of the slumber. This is only if the user had started sleeping in the first place.