Using custom actions instead of utter_ask templates in forms

Is there a way to execute custom actions instead of the default utter_ask_{slot_name} templates in a FormAction?

I want to have an Order form, that instead of uttering “What product would you like” sends buttons with product choices. Then the user would select one of the buttons.

The story would be like this if it wasn’t a form:

* buy_product{"item": "shoe"}
    - search_products  # This would send product buttons that match the 'item' the user is looking for
* select_products{"product": "123"}
    - add_products_to_cart
4 Likes

@akelad: Any idea whether this is possible at the moment?

you would have to overwrite this function in your form action rasa_core_sdk/forms.py at master · RasaHQ/rasa_core_sdk · GitHub

2 Likes

Thank you!!! I just posted a Github issue on this, I can take it down now.

This might be a silly question, but wouldn’t we also want to expose the Action Executor to the Form Action in order to schedule the custom action? I feel like it’s bad practice to directly call the run method of the action from inside my form action.

Thanks @akelad, that’s awesome!

1 Like

Overwrite which function… I am unable to understand what ‘this’ is referencing here?

This function needs to be overwritten: request_next_slot(self, dispatcher, tracker, domain ). Inplace of utter_ask_{} you can put your templete.

For example you could do something like this to fill in some variables in your templates:

def request_next_slot(self, dispatcher, tracker, domain):
    """Send customized message"""

    for slot in self.required_slots(tracker):
        if self._should_request_slot(tracker, slot):
            kwargs = {}
            if slot == 'some_slot':
                kwargs.update({"some_template_variable": "some_value"})

            dispatcher.utter_template("utter_ask_{}".format(ask), tracker, **kwargs)

            return [SlotSet(REQUESTED_SLOT, slot)]

Hi @smn-snkl, I want sent the carousel from form.

response = [{

               "title": store_name,
               "subtitle": "this is a test address",
               "image_url": "http://s3.amazonaws.c/15351164test.jpg",
               "buttons": [{
                   "type": "postback",
                   "title": "Select Store",
                   "payload": store_id + " " + store_name,
               }]
           }, {
               "title": store_name,
               "subtitle": "this is a test address",
               "image_url": "http://s3.amazonaws.com/TestCapitalhypermarket.jpg",
               "buttons": [{
                   "type": "postback",
                   "title": "Select Store",
                   "payload": store_id + " " + store_name,
               }]
           }]

dispatcher.utter_custom_json(gt)

How can I write it in utter_ask_car_model

Thanks, that’s exactly what I was looking for too (replacing utter_ask_{slot_name} by a custom action). Should we consider that as a temporary hack or a recommended practice?

I am very confused. I set parameters according to the template of request next slot,But I didn’t get the modified value:“test…slot…”. Could you please help me?thanks

As NT asked a year ago with no response from anyone at Rasa:

So, what IS the answer to that?

Don’t forget that this method will need to handle any other slots in your form that you don’t want to override, as you’re overriding the method that handles them all.

I understand that was just an example, but this code will break the form.