I am trying to understand properly how slots work programmatically. Before coding anything, I am trying to understand it well by looking at the examples for alexa sdk for python.
Specifically, I was trying to understand the basics in slots in the ColorPicker example (I understood properly the hello_world and did some coding by myself adding things. Worked fine).
I am having a hard time trying to understand how to access these slots values (because I see it is done in two different ways).
def whats_my_color_handler(handler_input):
"""Check if a favorite color has already been recorded in
session attributes. If yes, provide the color to the user.
If not, ask for favorite color.
"""
# type: (HandlerInput) -> Response
if color_slot_key in handler_input.attributes_manager.session_attributes:
fav_color = handler_input.attributes_manager.session_attributes[
color_slot_key]
speech = "Your favorite color is {}. Goodbye!!".format(fav_color)
handler_input.response_builder.set_should_end_session(True)
else:
speech = "I don't think I know your favorite color. " + help_text
handler_input.response_builder.ask(help_text)
handler_input.response_builder.speak(speech)
return handler_input.response_builder.response
What I understand in this function, is that color_slot_key
is the name of the variable in the slot in Alexa (de frontend, Alexa Developer). But then, handler_input.attributes_manager.session_attributes
, by what I understand, is the slots in general, and accessing by its key handler_input.attributes_manager.session_attributes['color_slot_key']
will get the value of the slot with that key.
If I understood this properly, it makes sense to me. If there is a color, alexa speeches it. If not, it doesn't.
Now:
@sb.request_handler(can_handle_func=is_intent_name("MyColorIsIntent"))
def my_color_handler(handler_input):
"""Check if color is provided in slot values. If provided, then
set your favorite color from slot value into session attributes.
If not, then it asks user to provide the color.
"""
# type: (HandlerInput) -> Response
slots = handler_input.request_envelope.request.intent.slots
if color_slot in slots:
fav_color = slots[color_slot].value
handler_input.attributes_manager.session_attributes[
color_slot_key] = fav_color
speech = ("Now I know that your favorite color is {}. "
"You can ask me your favorite color by saying, "
"what's my favorite color ?".format(fav_color))
reprompt = ("You can ask me your favorite color by saying, "
"what's my favorite color ?")
else:
speech = "I'm not sure what your favorite color is, please try again"
reprompt = ("I'm not sure what your favorite color is. "
"You can tell me your favorite color by saying, "
"my favorite color is red")
handler_input.response_builder.speak(speech).ask(reprompt)
return handler_input.response_builder.response
I do not understand why in this function, the value of the color is accessed by:
handler_input.request_envelope.request.intent.slots[color].value
And not by something like in the first function (something like this):
handler_input.attributes_manager.session_attributes[color_slot_key]
What I guess is that the first one just checks if it is there or not in the session attributes (Which I do not really understand what these are), and the other one has something to do with Alexa requests. But why is the format different?
You mixing two different things. Session attributes have nothing to do with slot values. You retrieve slots via:
handler_input.request_envelope.request.intent.slots
and the example, for convenience, stores the retrieved color in a session attribute so it's saved during the whole skill session. Think of session attributes as persistent attributes (key + value) that survive a skill session (they can be whatever you want).
The color picker example use of session attributes is explained here:
https://developer.amazon.com/de/docs/custom-skills/manage-skill-session-and-session-attributes.html#save-data-during-the-session
(you even have a tab to select python)
In the test tab, test the skill and take a look at the incoming and outgoing json. Things will become clear!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With