Keep the Session Open

The event handler is a component within Alexa that forwards gadget input to your skill. This topic discusses the lifecycle of a typical skill session and how an active event handler can affect it. For example, an active event handler can lengthen the session and, under certain circumstances, cancel a reprompt.

Skill session lifecycle

Skill sessions have the following lifecycle:

  1. A skill session begins when a user invokes your skill. This causes Alexa to send your skill a request. This is usually a launch request, although a user may invoke your skill with a specific intent also. The request contains a session object that uses a Boolean value called new to indicate that this is a new session.
  2. Your skill receives the request and formulates a response for Alexa to speak to the user.
  3. What happens next depends on the value of the shouldEndSession parameter in your skill's response:
    • true – The session ends. Alexa, not the skill, handles any further speech from the user. If the user re-invokes the skill, Alexa creates a new session (that is, go back to step 1).
    • false – The session stays open and Alexa opens the microphone to indicate that Alexa expects the user to respond. If the user provides a response, Alexa maps their speech to one of the intents that the skill defined, and sends the response to the skill. However, if a few seconds elapse without a response from the user, Alexa closes the microphone. If the skill specified a reprompt, Alexa reprompts the user to speak and opens the microphone for a few more seconds. After the microphone closes, the behavior of the session is the same as if the skill had left shouldEndSession undefined, as described next.
    • undefined – In this case, the session's behavior depends on the type of Echo device that the user is interacting with and whether there is an active event handler. If the device has a screen, the session stays open for up to 30 more seconds, without opening the microphone to prompt the user for input. If the user speaks and precedes their request with "Alexa," Alexa sends the request to the skill. Otherwise, Alexa ignores the user's speech. If an event handler is active, the session continues to stay open until the skill calls CustomInterfaceController.StopEventHandler or the event handler expires.

How an event handler affects the skill session lifecycle

An active event handler affects the skill session as follows:

  • Session lengthening – As long as an event handler is active, the skill session remains open for the duration of the event handler, unless the skill sets shouldEndSession to true in any of its responses. In other words, even if the user does not provide voice input, the session stays open until the event handler expires, the skill calls CustomInterfaceController.StopEventHandler, or the skill sets shouldEndSession to true.
  • Gadget input can open the microphone – When handling gadget input events, a skill can have Alexa open the microphone and request voice input from the user by setting shouldEndSession to false in its response.
  • The reprompt can be cancelled – When there is an active event handler, button activity can cancel a reprompt. An example is when your skill requests voice input by setting shouldEndSession to false in a previous skill response, and is also listening for gadget events. If the user interacts with the gadget and the skill receives a gadget input event, any pending voice reprompt is cancelled.

    This behavior can be surprising when your skill handles multiple events. For example, suppose your skill receives two events, and the event handler for the first event sets a reprompt for voice input. The second event cancels the reprompt that the first event handler set, even if the skill does not respond to the second event.

Was this page helpful?

Last updated: Feb 14, 2022