Let’s take a look at how we access session attributes from a Lambda function.
Session attributes are stored in the response that your skill receives from Alexa as a JSON. We can create and edit attribute fields by accessing their key. Inside of the Lambda function, you can access session attribute values using this.attributes
.
this.attributes['yourAttribute'] = 'value';
In the example above, we create a field in attributes
and save the string 'value'
to it. this
references the alexa
object that’s instantiated in the export.handlers
function.
Since the LaunchRequest is usually the first intent that the skill executes, we often set the initial attribute state inside of it.
// Inside of LaunchRequest this.attributes['numberCorrect'] = 0;
In the example above, we set the initial value of our 'numberCorrect'
attribute to 0
, because the user has not attempted any answers yet.
For this skill, we want to use session attributes to keep track of the language we’re practicing, how many questions we’ve answered, and which ones we’ve gotten correct. To help with this interaction, we’ve included a flashcardsDictionary
object that contains information about our flashcards, including the questions and their answers in different programming languages.
In this exercise, think about what initial states would make sense for each session attribute.
Instructions
In the LaunchRequest
, create a language
session attribute and save its value to an empty string.
Below that, create numberCorrect
and currentFlashcardIndex
session attributes and give them each a value of 0
.