Utter a different message when repeating a question in a form

I had the same problem today and this is how I solved it:

My use-case is for when users provide an answer that needs to be of a certain length.

:warning: Pre step: Handle your utter_ask_<slot_name> in the action server instead. In other words, convert it into an action_ask_<slot_name>. Let’s call it action_ask_answer for this example.

  1. My validate function goes as normal. If the response is less than 5 words long, it fills the slot with “None”. This also dispatchers an utterance, let’s call it utter_too_short.

  2. Step #1 prompts rasa to activate action_ask_answer again, so the action server receives a request to run it.

  3. Inside action_ask_answer, I extract the second to latest event to see if utter_too_short ever happened using a try/except block:

     try: 
         template = tracker.events[-2]["metadata"]["template_name"]
     except: 
         template = None
    
  4. I only repeat the message when template is not utter_too_short:

     if template != "utter_too_short":
         dispatcher.utter_message(template="utter_ask_answer")
    

This also works great when you are validating for other things that may require the repeated message. For example, when my users answer in a non-English language, I want to be able to say utter_not_english followed by utter_ask_answer