question

Bailey avatar image
Bailey asked

Setting and Using Session Data

Hello, I am having trouble using session data I have set. I appear to be running into a ClassCastException: java.util.LinkedHashMap cannot be cast to com.amazon.speech.slu.Slot: java.lang.ClassCastException java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.amazon.speech.slu.Slot at com.philosowaffle.skill.bible.BibleSpeechlet.readChapter(BibleSpeechlet.java:356) at com.philosowaffle.skill.bible.BibleSpeechlet.onIntent(BibleSpeechlet.java:76) at com.amazon.speech.speechlet.SpeechletRequestDispatcher.dispatchSpeechletCall(SpeechletRequestDispatcher.java:63) at com.amazon.speech.speechlet.SpeechletRequestHandler.handleSpeechletCall(SpeechletRequestHandler.java:69) at com.amazon.speech.speechlet.lambda.SpeechletRequestStreamHandler.handleRequest(SpeechletRequestStreamHandler.java:81) I have followed the code examples in the Session and WiseGuy example code. When the user makes an initial request and it is missing some data I set the known pieces of data into the session object like so: session.setAttribute("Book", intent.getSlot(bookSlot)); // both of these objects are of type Slot session.setAttribute("Chapter", intent.getSlot(chapterSlot)); SpeechletResponse response = new SpeechletResponse(); response.setShouldEndSession(false); response.setOutputSpeech(outputSpeech); response.setReprompt(reprompt); return response; Later, once the user has been reprompted, I try to access my saved session information like so: Slot book = (Slot) session.getAttribute("Book"); At this point I get the stack trace you see at the beginning of this post. I have logged out my session attributes at this point, it looks like this: INFO Session: {Book={name=Book, value=genesis}, Chapter={name=Chapter, value=2}} It looks like, under the hood, Slot is just a type of Map, but I still don't understand why I can't cast it to Slot when that is what I set? Any suggestions or more detailed examples around using Sessions?
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
Off the top of my head... I believe all session attributes should be strings. [code] session.setAttribute("Chapter", [b]intent.getSlot(chapterSlot)[/b]); [/code] this sets it to a slot. Shouldn't you need to do: [code] session.setAttribute("Chapter", intent.getSlot(chapterSlot)[b].getValue()[/b]); [/code] I may not be remembering the API right...
10 |5000

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

Bailey avatar image
Bailey answered
I must have missed that requirement in the API since the method signature accepts an Object, but I will try only passing String's tonight.
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 just know that, ultimately, it spits out JSON. So support for anything other than a String or Number would require a serializer and deserializer and I really don't think their APIs are that sophisticated.
10 |5000

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

Nick Gardner avatar image
Nick Gardner answered
Jjaquinta is correct, the session attribute should be a string, and you'll get that error if you pass something that's not a string into the session object. -Nick
10 |5000

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

Kissell avatar image
Kissell answered
Can session attributes be used to store information between sessions? Like I have alexa open my skill, I interact with it and then the skill is done. Where can I store that information so that next time the skill is opened it can pick up where it left off without having to gather the user provided information again?
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
J. Kissell - no. Session attributes only store while the skill is running. If you want to persist across invocations of the skill you will need Dynamo, S3 or some other storage layer.
10 |5000

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

justin avatar image
justin answered
Thanks Jonathan! J. Kissell -- feel free to check out our Score Keeper sample for persisting values across different sessions for a user: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/using-the-alexa-skills-kit-samples
10 |5000

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