Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 307
Nicolò Gasparini Avatar asked Jan 11 '19 16:01

Nicolò Gasparini


People also ask

How to get the value of city slot in lambda function?

One line of code. So if you want to access the city slot value simply put this line of code in your lambda function. Thank you, Peter for the pointer! Why is Sunday not a valid input for the day slot?

How to delegate the dialog to Alexa?

To explain them we must take a look at the Alexa feature ‘Dialog.Delegate’, this feature can delegate the dialog to Alexa if the user didn’t provide all the needed values for the slots. If a user did not provide a value for a slot from our skill, we can define some speech prompts in the slot menu.

What is slots in ask_utils?

slots is a dictionary of str: Slot values, see the source code for Intent and Slot for more details. import ask_sdk_core.utils as ask_utils slot = ask_utils.request_util.get_slot (handler_input, "slot_name") if slot.value: # do stuff

Does Alexa have any good built-in features?

In conclusion Alexa has some pretty nice built-in features like ‘ Dialog.Delegate ’, but without better explanations and code snippets these didn’t arrive in many skills which could use them to save much code and easily upgrade the user experience.


1 Answers

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.

like image 101
Milan Cermak Avatar answered Sep 17 '22 00:09

Milan Cermak