Using action_ask_{slot_name} in rasa 2.x

I have some forms and I want to ask for the next slots using custom actions instead of utter_ask_{slot_name} but when my forms activate, these actions don’t work. using utter_ask_{slot_name} worked for me but because I want to return a json_message, I used custom action. I put these actions in the domain file and I used rasa 2.3.1 and rasa-sdk 2.8.3 on Ubuntu. This is one of my actions:

class AskForNationalId(Action):
    def name(self) -> Text:
        return "action_ask_national_id"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
                "action": "ask_for_national_id",
                "response_id": "234",
                "response": "please enter your national_id"

        return []

I appreciate any help.

To make sure your action runs, just put a print() inside and it’ll appear on the terminal where you ran rasa run actions:

class AskForNationalId(Action):
    def name(self):
        return "action_ask_national_id"

    def run(self, dispatcher, tracker, domain):
        print(">>> I'm here! <<<")

        dispatcher.utter_message(json_message = {
            "action": "ask_for_national_id",
            "response_id": "234",
            "response": "please enter your national_id"

        return []

Also, did you register the action in the domain?

Thank you for your reply. yes, I registed the action in the domian and when I ran rasa run actions, I see this message:

2021-12-08 22:31:26 INFO rasa_sdk.executor - Registered function for 'action_ask_national_id'

I put print in action but in rasa run actions nothing is printed.

Then I ran rasa with rasa shell --debug, I found out when the requested_slot is national_id ‘rasa.core.policies.rule_policy’ predicted ‘action_listen’. But I don’t know why.

Can you show me your rules & stories related to the form?

Okay I just noticed this, make sure Rasa and Rasa SDK have the same minor version. Either upgrade to Rasa 2.8 or downgrade to Rasa SDK 2.3.

I don’t have any stories and I used FormAction for my forms.This is my rule:

- rule: activate citizen account form


    - intent: citizen_account

    - action: citizen_account_form

    - active_loop: citizen_account_form

  - rule: submit citizen account form


    - active_loop: citizen_account_form


    - action: citizen_account_form

    - active_loop: null

It’s probably not gonna solve this problem, but you should change your submit rule:

  - rule: submit citizen account form
    - active_loop: citizen_account_form
    - action: citizen_account_form
    - active_loop: null
  - slot_was_set:
    - requested_slot: null
 - action: action_submit_citizen_account_form

Anyway, did you do the following?

Sorry for my late reply.

yes, I downgraded to Rasa SDK 2.3.1 but, it didn’t work. Is there any way to trigger these actions in the form?

What is action_submit_citizen_account_form in the rule?

Does it print ">>> I'm here! <<<" or not?

It’s the action you want to run when the form is completed. You can call it whatever you like, it can be any custom action or utterance.

Can you show me how you defined the slots and form in the domain?

No, it doesn’t print.

This is my slot in the domain.

    type: rasa.shared.core.slots.TextSlot
    initial_value: null
    auto_fill: false
    influence_conversation: false

I didn’t define any forms in the forms section. I used FormAction for my forms and I registered the name of the form in the name method of form class, in the actions section in the domain.

FormAction or FormValidationAction?

Rasa 2.x and 3.x use the latter. Can you also show its code?

I used FormAction and it’s worked for rasa 2.3.1 . utter_ask_{slot_name} worked but I don’t know why action_ask_{slot_name} didn’t work.

This is my code for the form:

REQUESTED_SLOT = "requested_slot"

class CitizenAccountForm(FormAction):
    def name(self) -> Text:
        return "citizen_account_form"

    def required_slots(tracker: "Tracker") -> List[Text]:
        return ["national_id"]

    def slot_mappings(self) -> Dict[Text, Union[Dict, List[Dict[Text, Any]]]]:
        return {"national_id": self.from_entity(intent="inform", entity="national_id")}

    def validate_national_id(
            slot_value: Any,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: DomainDict,
    ) -> Dict[Text, Any]:
        """Validate national_id value."""
        if len(slot_value) != 10:
            dispatcher.utter_message(json_message={"response": "It's not correct."})
            return {"national_id": None}
            dispatcher.utter_message(json_message={"response": "It's correct."})
            return {"national_id": slot_value} 

    def submit(
            dispatcher: "CollectingDispatcher",
            tracker: "Tracker",
            domain: "DomainDict",
    ) -> List[EventType]:
        slot_list = []
        for slot_name in tracker.slots:
            slot_value = tracker.slots.get(slot_name, None)
            if slot_value is None or slot_name == "requested_slot":
            slot_list.append({'name': slot_name, 'value': slot_value})
        dispatcher.utter_message(json_message={"response": "form submitted successfully"})

        return [SlotSet("national_id", None)] 

Hi. Did you solve that problem? I have same issue. Added action_ask to domain and I made a function inside but still bot is using utter_ask_ resposne and if I delete it it is not working at all. Versions: rasa 2.8.13 rasa-sdk 2.8.6