Search code examples
node.jsalexa-skills-kitalexa-sdk-nodejs

alexa sdk pass session attributes


This Alexa Doc shows that the sessionAttributes is one of the response parameters and not within the response object.

I'm using the Alexa SDK and all of the emit response builders only use the response object's parameters. For example:

this.emit(':elicitSlot', slotToElicit, speechOutput, repromptSpeech, updatedIntent)

So how can I use emit() from the Alexa SDK and also pass an updated sessionAttributes in the response?


Solution

  • The Alexa SDK automatically serializes and includes the key-value pairs that you set on the attributes object before you call emit(..).

    More concretely, you could say:

    this.attributes.myAttribute1 = 'some value';
    this.emit(':elicitSlot', slotToElicit, speechOutput, repromptSpeech, updatedIntent);
    

    And the sessionAttributes object will automatically be updated in the response to include:

    'myAttribute1': 'some value`
    

    For reference, you can see the implementation of elicitSlot in the SDK, here: https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/blob/master/lib/response.js#L131-L143