Announcement: The Alexa Skills Community Is Moving To Stack Overflow

For improved usability and experience, Alexa skills related forum support will be transitioned to Stack Overflow. Effective January 10, 2024, the Amazon Developer Forums will no longer be available. For continued Alexa skills support you can reach out to us on Stack Overflow or via Contact Us.

question

bxio avatar image
bxio asked

Session Attributes trouble

I'm struggling with understanding how session and session attributes work. Say I have the following code: ====== function handleFirstIntent(intent, session, callback){ sessionAttributes = { questionsAsked: 0, questionsWanted: 0, questionBank: [], } sessionAttributes.questionsWanted = intent.slots.NumToAsk.value; var cardTitle = "Welcome"; var speechOutput = "Asking "+sessionAttributes.questionsWanted+" questions."; // If the user either does not reply to the welcome message or says something that is not // understood, they will be prompted again with this text. var repromptText = null; var shouldEndSession = false; callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); } function handleSecondIntent(intent, session, callback){ var sessionAttributes = session.attributes; var cardTitle = "Welcome"; var speechOutput = "Answering "+sessionAttributes.questionsWanted+" questions."; // If the user either does not reply to the welcome message or says something that is not // understood, they will be prompted again with this text. var repromptText = null; var shouldEndSession = false; callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession)); } I invoke handleFirstIntent, which should set session attributes to whatever the intent's NumToAsk slot's value is. Immediately after, I invoke handleSecondIntent, yet sessionAttributes.questionsWanted is undefined. What am I doing wrong?
alexa skills kitsubmission testing certification
10 |5000

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

Jon avatar image
Jon answered
Hi. You are not updating session.attributes. session.attributes.questionsWanted = intent.slots.NumToAsk.value; should work better. Don't forgot to check NumToAsk if it exists and is what you expect as well.
10 |5000

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

bxio avatar image
bxio answered
Thanks for the reply. Even after hard coding in a value like this: session.attributes.questionsWanted = 4; in handleFirstIntent, trying to access the value in handleSecondIntent still returns invalid because session.attributes.questionsWanted is undefined. Am I missing something?
10 |5000

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

Jon avatar image
Jon answered
When I was first learning, I put this code at the top of all my functions and intent handlers: console.log("session attributes stored: " + JSON.stringify(session.attributes)); Are you changing you callback back to "session" from sessionAttrinutes? Also, how come you are calling buildSpeechletRespone directly rather than using the response object that is in all the examples? Message was edited by: Jonathan P.
10 |5000

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

bxio avatar image
bxio answered
I started with the alexa favourite colour example provided on lambda. I realized now it's very different from the examples provided. Still, even when I load the wise_guy skill verbatim, it fails to handle the WhosThere intent, because session.attributes.stage is undefined. I'm beginning to suspect I misconfigured lambda somehow. Any idea what's going on?
10 |5000

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

memo@amazon avatar image
memo@amazon answered
Hi bxio, Are you doing your testing using the Service Simulator or an Echo? Thanks,
10 |5000

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

April L. Hamilton avatar image
April L. Hamilton answered
bixio - See my Session Attributes in Javascript Guide. http://lovemyecho.com/wp-content/uploads/2015/07/SessionAttributesInJavascript.pdf
10 |5000

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