How to change default re-asking of slot if slot validation fails?

I want to customize the message when the user inputs wrong answer for a slot request. For example: If the bot asks “what kind of training are you looking at?” and the user gives wrong answer, then the bot should reply: “This is not a valid training type. Please enter correct training type.” and not just repeat the same sentence: “what kind of training are you looking at?”. I used to handle these in RASA 1.7 using request_next_slot method of the form action. But, in RASA 2.2 I am not sure how to do that.

@akelad, Can you please help me out on this?

You can do this with a custom action that validates the input. If validation fails, set the slot to None, which will prompt the form to ask for it again.

You can find example code here in the docs: Forms

Thank you for the quick reply. The example code does not show how to customize the message when slot validation fails. This is the scenario I was to implement: user: i need training bot: Can you please select one of the below training types?(buttons displayed) user: Types some word which is not one of the training types and validation falis bot: This is not a valid training type. Please choose/enter the correct training type. The last bot message is the custom message that I want to send when the slot validation fails. I used to do this in request_next_slot method of FormAction in RASA 1.7. I am not sure how to do that in RASA 2.2. Please let me know if I have to clarify more.

Resolved it. Below is the action that I am using to resolve it.

class AskForTrainingTypeAction(Action):

def name(self) -> Text:
    return "action_ask_training_type"

def run(
    self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
    requested_slot = tracker.get_slot("requested_slot")
    training_form_config = domain.get("forms",
                                   {}).get("enroll_training_form", {})
    training_form_required_slots = list(training_form_config.keys())
    count_val = 0 if tracker.get_slot("count") is None else int(
        tracker.get_slot("count"))
    for slot in training_form_required_slots:
        if count_val > 2:
            logger.debug(
                "*************end the form*****************************")
            dispatcher.utter_message(
                template='utter_second_wrong_' + slot)
            dispatcher.utter_message(template='utter_ask_something_else')

            return [ActiveLoop(None)]
        if requested_slot not in training_form_required_slots:
            logger.debug('coming to if condition')
            dispatcher.utter_message(
                template=f"utter_ask_{slot}")
            return []

        dispatcher.utter_message(
            template=f"utter_first_wrong_{requested_slot}")

        return []
1 Like