About session attributes
The Tic-Tac-Toe skill keeps the skill session open to conduct a back-and-forth interaction with the user. While the session is open, the user doesn't need to use your invocation name to talk to your skill. The Alexa Skills Kit SDK provides an attributesManager to add session attributes to your response, and then retrieve the attributes from an incoming request. This is accessible through handlerInput, an object passed into each handler method.
In the sample Tic-Tac-Toe skill, there is a sessionAttributes object that contains various information, such as the number of games won by the user or Alexa.
const STARTER_SESSION_ATTRIBUTES = {
…
"winsUser": 0,
"winsAlexa": 0,
…
};
The Tic-Tac-Toe skill sets and gets session data within handlers in the following manner.
Get a reference to attributesManager
from the handlerInput
:
var attributesManager = handlerInput.attributesManager;
Set Session Attributes to the values in variable STARTER_SESSION_ATTRIBUTES
:
attributesManager.setSessionAttributes(STARTER_SESSION_ATTRIBUTES);
This code sample is from the LaunchHandler
, so we are setting the starter session attributes. Other intent handlers later in the skill session will get the session attributes, update them, and set them to the new reference, before returning an Alexa response.
Get Session Attributes:
var sessionAttributes = attributesManager.getSessionAttributes();