Skip to content
Advertisement

Alexa: How to assign a slot response to a variable. (Python)

I’m not sure how to assign a slot to a variable in an Alexa skill. I’ve found several tutorials, but most of them are in JS (I wrote this code in Python) or outdated, since even using them precisely as presented does not work.

Alexa is meant to ask for one of my kids’ names so I can implement a personalized answer, but I can’t find a way to make use of the name once she gets it.

Here’s how I call it in my code (look at variable “kids_name” in particular):

@sb.request_handler(can_handle_func=lambda input:
                    currently_playing(input) and
                    is_intent_name("ChoresIntent")(input))
def chores_intent_handler(handler_input):
    session_attr = handler_input.attributes_manager.session_attributes
    original_date = date(2022, 4, 8)
    today = date.today()
    diff = (today - original_date).days
    kids_name = handler_input.request_envelope.request.intent.slots.name.value
    mod = "Error. No input."

That seemed to be how they set the variable in the most recent tutorial I found, but it absolutely will not run for me. I’ve watched tutorials on pulling data from JSON files, but none of their answers look anything like this.

As I understand it, I construct the path from JSON, but I don’t understand the syntax. Here’s the JSON for my skill. I would really appreciate some clarification on how to transfer handler answers from one to the other. While I think I get the basic structure of the dictionaries, the methods I see for accessing them is very confusing for me.

{
                    "name": "ChoresIntent",
                    "slots": [
                        {
                            "name": "childname",
                            "type": "childname"
                        }
                    ],
                    "samples": [
                        "what are {childname} s jobs today",
                        "what are my jobs today",
                        "can you tell me {childname} s chores",
                    ]
                }
            ],
            "types": [
                {
                    "name": "childname",
                    "values": [
                        {
                            "name": {
                                "value": "Matthew",
                                "synonyms": [
                                    "Mattie"
                                ]
                            }
                        },

Thank you in advance! I really appreciate the help I get on here.

Advertisement

Answer

You have 2 options

Without ask_sdk:
kids_name = handler_input.request_envelope.request.intent.slots["name"].value
With ask_sdk:
kids_name = ask_utils.request_util.get_slot_value(handler_input, "name")

# You can also get the slot and then set the value:
slotName = ask_utils.request_util.get_slot(handler_input, "name")
kids_name = slotName.value
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement