Distinguishing Sessions

Now that we have saved the session state, let’s use it to deliver a different response to a first time user versus a returning user.

For returning users, the session attributes are retrieved from the database automatically, are available for use in the current session, and can be accessed in the same way you have been, like:

var myLanguage = this.attributes['currentLanguage'];

Checking for New Users

It is good skill design practice to check for new users and deliver an introductory response as compared to returning users — this is similar to the sign-up flow when you first open a mobile app.

To determine if the user is new, we can check if the attributes key in the JSON request is empty. If it is, we can assume that the user has never opened the skill. The following syntax checks to see if an object is empty by checking the length:

Object.keys(this.attributes).length === 0

If the attributes object is not empty then we can assume it is not the first time that the user has launched the skill. So, we can generate a different response that welcomes them back.

if (Object.keys(this.attributes).length === 0) { this.attributes['currentIndex'] = 0; this.response.speak('Welcome to Codecademy!' + 'What is your name?'); } else { this.response.speak('Welcome back!' + 'Are you ready to start where you ' + 'left off?'); }

In the example above, we provide two different responses for users who are new or returning.



In the LaunchRequest, write a conditional statement that will check to see if it’s a user’s first time opening this skill:

  • if the attributes object is empty, use the existing code to initialize the flashcards session attributes
  • make an an empty else block for now

Make sure you leave the ‘:responseReady’ emit outside of the conditional statement.


If it’s the first time a user opens the skill, have Alexa .speak:

'Welcome to Flashcards. Do you want to test your knowledge in Ruby, Python, or Javascript?'

and .listen with:

'Which language would you like to practice?'

If it’s a later session, have Alexa .speak with:

'Welcome back to Flashcards. You are currently working on ' + currentLanguage + '. You\'re on question ' + currentFlashcardIndex + ' and have answered ' + numberCorrect + ' correctly.' + ' Do you want to test your knowledge in Ruby, Python, or Javascript?'

and .listen with:

'Which language would you like to practice?'

You may have noticed that we included some variables in the ‘else’ statement speech output. So let’s define them! You will create variables for currentLanguage, currentFlashcardIndex, and numberCorrect.

In the else statement, create a currentLanguage variable and set it equal to the value of the currentLanguage key in our flashcards session attribute object.


Set currentFlashcardIndex and numberCorrectto the corresponding values inside the flashcards.languages object.

Folder Icon

Take this course for free

Already have an account?