# How to implement a repeat intent

**URL:** <https://forum.rasa.com/t/how-to-implement-a-repeat-intent/50865>\
**Category:** Rasa Open Source\
**Created:** [January 25, 2022, 9:41am UTC](https://forum.rasa.com/t/how-to-implement-a-repeat-intent/50865 "2022-01-25T09:41:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dladotreefrog](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@dladotreefrog](https://forum.rasa.com/u/dladotreefrog)\
**Post date:** [January 25, 2022, 9:41am UTC](https://forum.rasa.com/t/how-to-implement-a-repeat-intent/50865/1 "2022-01-25T09:41:26Z")

</div>

We would like to implement an intent in our bot that should do the following:

- User ask things like “Please say that again?”
- The bot respond saying “Did you want me to repeat what I just said a moment ago?”
- The user respond yes
- I repeat the last utter before “Please say that again?”

I’d like to implement that in a way that we don’t have to create all the different casuistics in the stories. Maybe a rule but rules are not allowing several intents. Also, I’d like this implementation to be solid enougth to keep bot memory and state healty once I repeat.

Any ideas?

---

<div class="post-metadata">

**Author:** ![TalissaDreossi](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/talissadreossi/32/21280_2.png) [@TalissaDreossi](https://forum.rasa.com/u/TalissaDreossi)\
**Post date:** [January 25, 2022, 10:51am UTC](https://forum.rasa.com/t/how-to-implement-a-repeat-intent/50865/2 "2022-01-25T10:51:43Z")

</div>

You can do it using rasa action:

```auto
class ActionRepeat(Action):
    def name(self) -> Text:
        return "action_repeat"

    def run(self, dispatcher, tracker, domain):
        if len(tracker.events) >= 3:
            i = 0
            while(tracker.events[i].get('event')!='bot' ):
                i = i-1
            last = tracker.events[i].get('data').get('custom') #I have custom response
            dispatcher.utter_message(json_message={
                #do what you want with your last message 
                #i.e I had a field named text so:
                "text": last.get('text')
            })
        return [UserUtteranceReverted()]

```

---

<div class="post-metadata">

**Author:** ![dladotreefrog](https://avatars.discourse-cdn.com/v4/letter/d/e274bd/32.png) [@dladotreefrog](https://forum.rasa.com/u/dladotreefrog)\
**Post date:** [January 25, 2022, 11:24am UTC](https://forum.rasa.com/t/how-to-implement-a-repeat-intent/50865/4 "2022-01-25T11:24:20Z")

</div>

Hello [TalissaDreossi](http://forum.rasa.com/u/TalissaDreossi) thanks for the quick response! This is quite helpfull.

Ok, I think I understand what you suggest

Also, where do you suggest to add the following steps? Is it possible to add this as a Rule (because I want this to be a generic behaviour)? I didn’t found rules with two intents… but maybe is possible?

- User ask things like “Please say that again?”` (intent: repeat)`
- The bot respond saying “Did you want me to repeat what I just said a moment ago?” `(utter_repeat)`
- The user respond yes `(intent:affirm)`
- I repeat the last utter before “Please say that again?” `(action_repeat)`

---

<div class="post-metadata">

**Author:** ![TalissaDreossi](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/talissadreossi/32/21280_2.png) [@TalissaDreossi](https://forum.rasa.com/u/TalissaDreossi)\
**Post date:** [February 1, 2022, 10:24am UTC](https://forum.rasa.com/t/how-to-implement-a-repeat-intent/50865/5 "2022-02-01T10:24:56Z")

</div>

I think you can do that by searching recursively in tracker the previous of previous message:

```auto
if len(tracker.events) >= 5: #not sure if 5 but it must be greater than 3 cause now you have another question in between
    i = 0
    keepSearching=True
    while(tracker.events[i].get('event')!='bot' or keepSearching):
       i = i-1
       if(tracker.events[i].get('event')=='bot'):
             keepSearching = False  
             #so next time it find tracker.events[i].get('event')=='bot' it will exit while loop
             #giving you the first response and not “Did you want me to repeat what I just said a moment ago?” 

```

I think this can be done in a smarter way 😅 but just to get you the idea this should work 😉
