question

ashish avatar image
ashish asked ·

Session attributes not persisting while switching intent

I just started out with Alexa Skill development course on Codecademy. I was experimenting with session attributes with a very basic example. I have a LaunchRequest intent in which I am setting a session attribute to a particular value. And, I am using this attribute in another intent which is triggered on some utterance.

However, the value of session attribute when switching intent is undefined. Somehow, the session attribute value is not persisting between intents. I am testing my skill in simulator in the developer console.

This might sound to be a very amateur. Any help or resources will be highly appreciated. You can find my handler code below in Node.js

var handlers = {

  // Launch request
  'LaunchRequest': function() {
    this.response.speak('Hey there. How are you?');
    this.attributes.variable='hey';
    this.emit(':responseReady');
  },
  //Session intent to use the previously defines attribute value

  sessionIntent: function(){
      this.response.speak('Variable value is '+this.attributes.variable);
      this.emit(':responseReady');
  }
};
alexa skills kithow-tosessionattributes
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Daniel M. | DigiVoice.io avatar image
Daniel M. | DigiVoice.io answered ·

Don't know, but maybe there is a sytax error on Codecademy?

Try:

this.attributes['variable']='value'; // this is to save
var read = this.attributes['variable'] // this is to read

Don't forget to enable DynamoDB access in your lambda function and set this on top of your lambda nodejs skill code

alexa.dynamoDBTableName = 'TableName'; // that's it
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Amazon_Bernardo Bezerra avatar image
Amazon_Bernardo Bezerra answered ·

Hi @ashish and thanks for your question.

The code snippet you posted is from the V1 of the SDK. A new version has been launched in April 2018. While you can still code in V1, we recommend you use the newest SDK V2, which has some more intuitive coding.

This is how your code would look like in V2:

const LaunchRequestHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
  },
  handle(handlerInput) {
    // get session attributes
    const attributes = handlerInput.attributesManager.getSessionAttributes();
    // set 'variable' (you can name whatever you want) attribute
    attributes.variable = 'hey';
    // save session attributes
    handlerInput.attributesManager.setSessionAttributes(attributes);

    return handlerInput.responseBuilder
      .speak('Hey there. How are you?')
      .withShouldEndSession(false)
      .getResponse();
  },
};

const SessionIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === 'IntentRequest'
    && handlerInput.requestEnvelope.request.intent.name === 'sessionIntent';
  },
  handle(handlerInput) {
    // get session attributes
    const attributes = handlerInput.attributesManager.getSessionAttributes();
    // read session attributes
    let read = attributes.variable;

    return handlerInput.responseBuilder
      .speak("Variable value is: " + read)
      .getResponse();
  },
};

You can find more samples and tutorials on the Alexa's GitHub repository.

Feel free to contact us if you have any further questions.

Regards,
Barry

2 comments
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Is ".withShouldEndSession(false)" in the LaunchRequestHandler really necessary?

0 Likes 0 ·
Amazon_Bernardo Bezerra avatar image Amazon_Bernardo Bezerra ♦♦ newuser-4a78fcf8-5a79-4d18-b7f0-01605f9e8d6a ·

Hello and thank you for your question.

If you want the session to remain reopen after the LaunchRequest, you can do it by either having the

.withShouldEndSession(false)

or

.reprompt('some speech')

Otherwise, the session will be closed.

Regards,
Barry

0 Likes 0 ·