question

listening-for-echos avatar image
listening-for-echos asked

attributes will not persist

I am unable to get my session attributes to persist beyond a single intent request. It works fine if the attribute was set by the last intent called, but otherwise fails. I can see that the attributes are not expressed in the request generated by the service simulator. The most recent thread on this topic suggested that it is a sim problem, but at the end the Amazon rep commented that the sim was now fixed (doringme: Oct 15, 2015). Apologies if the text is badly formatted: I tried to paste the code here but when I do the "Preview," it shows no text at all, can't figure out why. I also tried to paste the link to the previous thread but again that causes Preview to display no text at all. I was able to use the "Spell Check" to see it, but the URL still pastes incorrectly. Suggestions? function handleYesRequest(intent, session, callback) { var sessionAttributes = {}; var cardTitle = "yes Response"; var speechOutput = "I heard you answer yes, but I don't know what question you were responding to."; var repromptText = ""; if(session.attributes && session.attributes.yesNoQuestionSource) { if (session.attributes.yesNoQuestionSource == "Welcome") { repromptText = ""; speechOutput = "OK, we are moving on."; } } callback(sessionAttributes, buildSpeechletResponse(cardTitle, speechOutput, repromptText, false)); } function buildSpeechletResponse(title, output, repromptText, shouldEndSession) { return { outputSpeech: { type: "PlainText", text: output }, card: { type: "Simple", title: "SessionSpeechlet - " + title, content: "SessionSpeechlet - " + output }, reprompt: { outputSpeech: { type: "PlainText", text: repromptText } }, shouldEndSession: shouldEndSession }; } function buildResponse(sessionAttributes, speechletResponse) { return { version: "1.0", sessionAttributes: sessionAttributes, response: speechletResponse }; }
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.

jjaquinta avatar image
jjaquinta answered
If by "I am unable to get my session attributes to persist beyond a single intent request" you mean that "I return some session attributes in one call to my web service, I get them back in the next call, but not in subsequent calls" then this is expected. Alexa only persists what you send it back. You have to write every single session attribute you want to save each return. And it only persists for that session. If the line drops, poof, all your session variables go away. You are fall better off maintain state yourself. Unless your skill is utterly trivial, session attributes are a very fragile mechanism.
10 |5000

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

listening-for-echos avatar image
listening-for-echos answered
How do you maintain state? No global variables and the attributes do not persist.
10 |5000

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

jjaquinta avatar image
jjaquinta answered
If you are using Lambda, you will need to persist it to an off-line store. Say DynamoDB. Unfortunately, since background threads are terminated when your Lambda function returns, you need to do it in the foreground, which adds 1-2 seconds to your response time. If you are not using Lambda, you have more options. For stuff you want to persist for a while, but not forever, you can just cache it in memory. That will keep until you restart your service. For stuff you want to persist forever, you need to use some sort of store. A quick and cheap way is to save your memory cache to disk, and read it back in on startup. But a more proper way would be to persist it to an off-line store, same as above. The difference is that you can also cache it in memory, which will minimize DB hits and increase responsiveness. And you can also do the saving in an off-line thread, which will also increase responsiveness. I've used each of these solutions in different skills as it made sense. My most complex skill, Starlanes, uses a hybrid of several of these systems, depending on the information.
10 |5000

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

Galactoise avatar image
Galactoise answered
This is actually a really important question within this ecosystem. There are a few global variables, user id being the one most helpful for cross-session persistence, but in general the system isn't really built for long term persistence. User id is actually much more ephemeral than what the name implies - it is not consistent across skills for the same user, and when a user disables and reenables a skill the user id is rotated. There's another running thread about this design pattern, and it was something I had hoped to bring up on my call with them today, but we ran out of time. One thing that I did get from them - which is by no means an official stance (obviously I can't speak on their behalf anyway) - is that they generally prefer that developers build skills that are stateless. They've built in the active functionality understanding that there are cases like Starlanes where an active session is needed, but some of the people on the call voiced the opinion that one-shot skills tend to result in a better user experience for most use cases. I realize that's not a helpful answer to your question, to which I guess I'd say "use a database and with user id being the key to the session table". Just thought it was an important tidbit to put out there, based on what I heard today.
10 |5000

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

listening-for-echos avatar image
listening-for-echos answered
Thanks, these are very helpful points. I understand the idea that stateless code can produce cleaner user experiences, but loss of any persistence at all is a high price to pay.
10 |5000

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

Galactoise avatar image
Galactoise answered
Yeah, totally agree. We had to work pretty hard to make our case during the call today that active sessions were important for our skill (which isn't of the traditional form where state would be used).
10 |5000

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

jjaquinta avatar image
jjaquinta answered
>I understand the idea that stateless code can produce cleaner user experiences I completely disagree. Purely stateless skills are toys. Seems rather a waste of Alexa's resources and capabilities. My skill usage graph has a peak at 6pm today of [i]one user[/i] interacting with Starlanes [i]481 times[/i]. This isn't someone just flipping their lights on and off, or having their fortune read. This is someone spending quality time engaging with Alexa for hours at an end. This is the potential power of this platform. And you aren't going to get there via stateless one-shot interaction skills. I just wish Amazon would see that.
10 |5000

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

Galactoise avatar image
Galactoise answered
> This isn't someone just flipping their > lights on and off, or having their fortune read. I have to say, I like the mental image of some dude throwing a light-switch-rave by yelling at his Echo to switch his lights 481 times in a row.
10 |5000

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

jjaquinta avatar image
jjaquinta answered
>I have to say, I like the mental image of some dude throwing a light-switch-rave >by yelling at his Echo to switch his lights 481 times in a row. Amazon's perfect customer!
10 |5000

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