How to check and get Alexa slot value with Python ask sdk
Asked Answered
V

2

11

In my interaction model I defined a slot named city that is optional, not required, to fulfill an intent.

I'm using the python ask sdk, so my handler is this:

class IntentHandler(RequestHandler):
    """
    Intent handler
    """

    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return is_request_type("IntentRequest")(handler_input) and \
               is_intent_name("ExampleIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        access_token = handler_input.request_envelope.context.system.user.access_token

        if access_token is None:
            raise Exception("no access_token provided")

        speech = RESPONSE_MESSAGE_DEFAULT
        handler_input.response_builder.speak(speech)

    return handler_input.response_builder.response

How can I check if the slot was filled by the user and how can I get its value?

Vlada answered 11/1, 2019 at 16:25 Comment(0)
E
14

In your handler, you can do something like this:

slots = handler_input.request_envelope.request.intent.slots
city = slots['city']
if city.value:
    # take me down to the paradise city
else:
    # this city was not built on rock'n'roll

slots is a dictionary of str: Slot values, see the source code for Intent and Slot for more details.

Embryotomy answered 11/1, 2019 at 18:20 Comment(1)
for my use case, using the built in animal slot, I have something like this working : python {'animal': { 'confirmation_status': 'NONE', 'name': 'animal', 'resolutions': None, 'value': 'a dog'} } Then I access this data by doing : python slots = handler_input.request_envelope.request.intent.slots print(slots) animal_slot = slots['animal'] animal = animal_slot.value print(f"ANIMAL REQUESTED: {animal}") Yezd
D
4

Per the docs found here, you could do something like this:

import ask_sdk_core.utils as ask_utils
slot = ask_utils.request_util.get_slot(handler_input, "slot_name")
if slot.value:
    # do stuff
Dome answered 19/3, 2020 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.