question

April L. Hamilton avatar image
April L. Hamilton asked

Session Attributes

I'm working on a javascript app in AWS Lambda. I've never had to work with session attributes before because there are no session variables exposed to the GUI in client-side javascript, which is what I'm used to writing. I want to set 3 global variables (one array and two specific values), which in this context would appear to be handled as "session attributes", right at the start of my session, not dependent on any user input or action, then update them from within my functions based on logic within the functions. I've tried figuring out how session variables work in the Favorite Color sample app code, but I'm very confused. I see this declaration within the setColorInSession(intent, session, callback) function, which would seem to be declaring a local variable: var sessionAttributes = {}; - - - - - - - - - - - - - - - - - - - - - - - Later in that same function this line appears: sessionAttributes = createFavoriteColorAttributes(favoriteColor); - - - - - - - - - - - - - - - - - - - - - - - Then a totally separate function does this: function createFavoriteColorAttributes(favoriteColor) { return { favoriteColor: favoriteColor }; } - - - - - - - - - - - - - - - - - - - - - - - And finally, the getColorFromSession(intent, session, callback) function includes this: if(session.attributes) { favoriteColor = session.attributes.favoriteColor; } - - - - - - - - - - - - - - - - - - - - - - - Because the word "session" is used in function names, variable names [i]and[/i] in what appear to be references to actual session attributes, I'm having trouble telling what's a true session attribute operation (declaring/assigning/updating a session attribute) and what's a local variable, or developer-created function, that just happens to include the word "session" in it. Can session attributes be declared/assigned/updated/referenced anywhere in the code, and what is the correct syntax for those four actions: declaring a session attribute under a specific name, assigning a value to the session attribute, updating the value of the session attribute, calling/referencing a session attribute? Does the syntax vary when the session attribute is an array vs. a value vs. the output of a function? I've tried Googling "session attributes" but I get pages about Java, Spring MVC, ASP.net and developer-defined libraries, and I'm not sure which of those, if any, is applicable here. Thanks in advance for any help with this! :)
alexa skills kitdebugging
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
Okay, since I'm not seeing any reply on my session attributes question and I only have time to work on my Echo apps during the weekend, I'm going to go ahead and try using globals instead. AWS Lambda documentation says Javascript is supported, Alexa App Kit documentation does [i]not[/i] say globals [i]aren't[/i], and since none of my globals are dependent on spoken Intents it may work. I'll report my findings after test.
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
Globals [i]do[/i] work in my AWS Lambda code. Maybe it's only necessary to use session attributes for storage when you need to store and/or manipulate input from the user. If you can set your global variable/array at the outset and your code manipulates that global according to logic that's not at all dependent on user input, no session attributes seem to be required. Only an Amazon Echo team person could say if this is true for certain, but I'm running an app with a declared global array at the top of the script, changing its makeup over and over again through repeated calls to the same function, and referencing the revised array over and over again through repeated calls as well. AWS Lambda Javascript Coders: declare and manipulate your globals with confidence! :)
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
**UPDATE** I was wrong. A certain element in my code was creating the illusion that a global was being manipulated when it really wasn't. Globals are read the first time the code executes, but to persist a value and manipulate it throughout a session, across multiple functions, it appears a session attribute must be used. I've yet to find a clear example demonstrating the correct syntax to set, reference and update a session variable using javascript in AWS Lambda. Seems like it should be pretty easy, I just can't find the correct syntax to use, some kind of analog for setting, referencing and updating an array like you can do in javascript: aryMyArray = [DA, DK, DQ, DJ, D10, D9]; --- defines the array variable aryMyArray[0] --- references the element at index 0 in the array variable aryMyArray.splice(2, 1) --- removes the element at index 2, "DQ", from array variable aryMyArray.slice() --- if used after splice, returns mutated array variable I've posted a new question about it in troubleshooting/debug, hopefully someone will respond. ='/
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
I was wrong - see my follow-up comment in the thread
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
10 |5000

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