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?