Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding slots and getting its values in Alexa Skills Kit

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_keyis 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?

like image 858
Louis Storming Avatar asked Apr 16 '19 07:04

Louis Storming


1 Answers

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!

like image 110
German Avatar answered Sep 21 '22 13:09

German