# Delay in dispatcher.utter\_message()

**URL:** <https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687>\
**Category:** Rasa Open Source\
**Created:** [April 19, 2022, 1:13pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687 "2022-04-19T13:13:25Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![lis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/lis/32/18141_2.png) [@lis](https://forum.rasa.com/u/lis)\
**Post date:** [April 19, 2022, 1:13pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/1 "2022-04-19T13:13:25Z")

</div>

Hi, I’m trying to introduce a small delay between messages that are sent using the dispatcher.utter\_message() using time module. But what I see is that dispatcher sends them out in batches. Example:

I have a bunch of replies to be made one after the other in an array. So I’m doing:

```auto
for reply in replies:
       time.sleep(2)
       dispatcher.utter_message(text=reply)

```

But dispatcher ends up waiting for 2\*len(replies) time, and post this it sends all the dispatcher messages together. How can I introduce a small delay between messages?@ChrisRahme any idea?

---

<div class="post-metadata">

**Author:** ![itsjhonny](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/itsjhonny/32/20201_2.png) [@itsjhonny](https://forum.rasa.com/u/itsjhonny)\
**Post date:** [April 19, 2022, 1:36pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/2 "2022-04-19T13:36:53Z")

</div>

Hum… in the past i made a solution but i chosen frontend way

I put delay while javascript inserting news mensagens in chat

I have some ideas but i need to test first 😂 😂

I will back with my results

---

<div class="post-metadata">

**Author:** ![lis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/lis/32/18141_2.png) [@lis](https://forum.rasa.com/u/lis)\
**Post date:** [April 19, 2022, 1:41pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/3 "2022-04-19T13:41:40Z")

</div>

sure, thanks @itsjhonny 😄

---

<div class="post-metadata">

**Author:** ![itsjhonny](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/itsjhonny/32/20201_2.png) [@itsjhonny](https://forum.rasa.com/u/itsjhonny)\
**Post date:** [April 19, 2022, 4:45pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/4 "2022-04-19T16:45:41Z")

</div>

Hi! I’m still trying 😂

Its little hard because we need to process asynchronously and use an external event to respond to the user. Customs Actions can’t wait more than (i guess 20 seconds) to return. Else, action return `TimeOut error` 😢

Maybe [reminders](https://rasa.com/docs/rasa/reaching-out-to-user/#reminders) can be an way but i’m not sure this is a good way to use in this case

I will try first somethings using `threading.Timer` but i believe the best way is build delay in frontend or using [Custom Connectors](https://rasa.com/docs/rasa/connectors/custom-connectors/)

I will try more 😃

@edit -----

#### [thread](https://github.com/RasaHQ/rasa/issues/1882) don’t work ☹

```auto
threading.Timer

```

> <https://github.com/RasaHQ/rasa/issues/1882>
>
> \*\*Rasa Core version\*\*:
> rasa-core (0.7.6)
> \*\*Python version\*\*: 
> Python 3.6.1
> \*…\*Operating system\*\* (windows, osx, ...):
> Windows 10
> \*\*Issue\*\*:
> I defined a custom action where a bot answers a question and then, after a short delay, asks if a client has more questions. To make a delayed response without pausing the main thread, I am using \`threading.Timer\` object. For some reason the dispatcher doesn't utter templates when executed in a separate thread. Here's the action:
> 
> \`\`\`python
> from rasa\_core.actions import Action
> from rasa\_core.events import SlotSet
> import threading
> 
> class ActionProvideHelp(Action):
> def name(self):
> return "action\_provide\_help"
> 
> def run(self, dispatcher, tracker, domain):
> self.utter\_template(dispatcher, 'utter\_provide\_help')
> t = threading.Timer(5.0, self.utter\_template, args=\[dispatcher, 'utter\_more\_questions'\])
> t.start()
> 
> def utter\_template(self,dispatcher, template):
> dispatcher.utter\_template(template)
> print('DEBUG')
> \`\`\`
> When this action is triggered, the bot utters just the contents of \`utter\_provide\_help\` template, but 'DEBUG' is printed twice. No error messages.
> 
> The following executes as expected, i.e both contents of \`utter\_provide\_help\` and \`utter\_more\_questions\` are uttered by the bot.
> 
> \`\`\`python
> def run(self, dispatcher, tracker, domain):
> self.utter\_template(dispatcher, 'utter\_provide\_help')
> self.utter\_template(dispatcher, 'utter\_more\_questions')
> \`\`\`
> 
> Is it expected that calling dispatcher methods in a separate thread shouldn't work? And what is then the best way to delay response without resorting to anything that stalls the main thread (like \`time.sleep()\`)?

---

<div class="post-metadata">

**Author:** ![lis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/lis/32/18141_2.png) [@lis](https://forum.rasa.com/u/lis)\
**Post date:** [April 19, 2022, 6:47pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/5 "2022-04-19T18:47:43Z")

</div>

Thanks @itsjhonny, gonna check if asyncio in python is gonna help me.

---

<div class="post-metadata">

**Author:** ![itsjhonny](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/itsjhonny/32/20201_2.png) [@itsjhonny](https://forum.rasa.com/u/itsjhonny)\
**Post date:** [April 19, 2022, 7:13pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/6 "2022-04-19T19:13:09Z")

</div>

I have no success here 😢I think the best way is do it in frontend

Maybe @nik202 or Chris knows some other ways

---

<div class="post-metadata">

**Author:** ![nik202](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/nik202/32/16098_2.png) [@nik202](https://forum.rasa.com/u/nik202)\
**Post date:** [April 19, 2022, 8:11pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/7 "2022-04-19T20:11:08Z")

</div>

@lis what is your front end, it depends on the front end not related to rasa.

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [April 19, 2022, 8:43pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/8 "2022-04-19T20:43:02Z")

</div>

You can’t use `sleep()` inside a same action.

`dispatcher.utter_message()` adds an event under the hood, like the ones you put inside the `return []` of custom actions. And events are ran after the action.

What you can do is make two actions, `action_a` and `action_b`. `action_a` prints the first message and at the end, you `return [FollowupAction("action_b"])`. In `action_b`, you introduce the delay and print a second message.

I am not sure this will work though! I never tried it. If it doesn’t work, I would use Reminders as @itsjhonny suggested. But try this way first since it’s easier.

---

<div class="post-metadata">

**Author:** ![itsjhonny](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/itsjhonny/32/20201_2.png) [@itsjhonny](https://forum.rasa.com/u/itsjhonny)\
**Post date:** [April 19, 2022, 9:00pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/9 "2022-04-19T21:00:07Z")

</div>

I tried this solution. Its working for two message if has more, its show `TimeoutError` and `Circuit breaker tripped`

my action.py

```auto
from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import FollowupAction,BotUttered,UserUtteranceReverted,ReminderScheduled,ActionExecuted

import time

class ActionDelayMsg1(Action):
    current_msg_number = 0
    def name(self) -> Text:
        return "action_delay_msg1"

    async def run(
      self, dispatcher, tracker: Tracker, domain: Dict[Text, Any]
    ) -> List[Dict[Text, Any]]:
        msgs = ["teste1","teste2","teste3"]
        if(self.current_msg_number > 5):
            self.current_msg_number = 0
            return []

        self.current_msg_number +=1
        return [FollowupAction('action_delay_msg2')]

class ActionDelayMsg2(Action):
    def name(self) -> Text:
        return "action_delay_msg2"

    async def run(
      self, dispatcher, tracker: Tracker, domain: Dict[Text, Any]
    ) -> List[Dict[Text, Any]]:

        time.sleep(3)
        dispatcher.utter_message("teste")
        return [FollowupAction('action_delay_msg1')]

```

Console output

```auto
Your input -> hi                                                                                     
teste
teste
2022-04-19 17:59:25 ERROR asyncio - Task exception was never retrieved
future: <Task finished name='Task-3' coro=<configure_app.<locals>.run_cmdline_io() done, defined at /Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/rasa/core/run.py:130> exception=TimeoutError()>
Traceback (most recent call last):
  File "/Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/rasa/core/run.py", line 134, in run_cmdline_io
    await console.record_messages(
  File "/Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/rasa/core/channels/console.py", line 185, in record_messages
    async for response in bot_responses:
  File "/Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/rasa/core/channels/console.py", line 140, in _send_message_receive_stream
    async for line in resp.content:
  File "/Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/aiohttp/streams.py", line 35, in __anext__
    rv = await self.read_func()
  File "/Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/aiohttp/streams.py", line 311, in readline
    return await self.readuntil()
  File "/Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/aiohttp/streams.py", line 343, in readuntil
    await self._wait("readuntil")
  File "/Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/aiohttp/streams.py", line 304, in _wait
    await waiter
  File "/Users/johnguimaraes/miniforge3/envs/rasa3/lib/python3.8/site-packages/aiohttp/helpers.py", line 721, in __exit__
    raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError
2022-04-19 17:59:27 WARNING rasa.core.processor - Circuit breaker tripped. Stopped predicting more actions for sender 'bae92dd506394cc2a6209257ea47b3a9'.

```

---

<div class="post-metadata">

**Author:** ![ChrisRahme](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/chrisrahme/32/15572_2.png) [@ChrisRahme](https://forum.rasa.com/u/ChrisRahme)\
**Post date:** [April 19, 2022, 9:01pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/10 "2022-04-19T21:01:56Z")

</div>

Interesting, thanks for testing it out 🙂

Just for documentation purposes, can you edit your post and add the full error?

---

<div class="post-metadata">

**Author:** ![itsjhonny](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/itsjhonny/32/20201_2.png) [@itsjhonny](https://forum.rasa.com/u/itsjhonny)\
**Post date:** [April 19, 2022, 9:08pm UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/11 "2022-04-19T21:08:51Z")

</div>

Sure! 😊

I will test reminders. This is will be interesting to use for facebook, telegram, whatsapp and other platform But just in this case. If has own frontend, is think better build the solution in frontend

I didn’t have success with custom connector too ☹

---

<div class="post-metadata">

**Author:** ![lis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/lis/32/18141_2.png) [@lis](https://forum.rasa.com/u/lis)\
**Post date:** [April 20, 2022, 4:27am UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/12 "2022-04-20T04:27:52Z")

</div>

Yep, @itsjhonny I’m using slack as my connector. Which is why I’m struggling. If I had another frontend, I would definitely tried it there ☹Like you said, I couldn’t do it with connector also.

---

<div class="post-metadata">

**Author:** ![itsjhonny](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/itsjhonny/32/20201_2.png) [@itsjhonny](https://forum.rasa.com/u/itsjhonny)\
**Post date:** [April 20, 2022, 11:02am UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/13 "2022-04-20T11:02:32Z")

</div>

Hi Lis 😃

So… [reminder](https://github.com/RasaHQ/rasa/tree/main/examples/reminderbot) work very well for it The logic is very similar with my last post contenting actiion.py file

Reminders don’t work with Rest but, i think it will work for slack

channels available

```auto
output_channel	
string
Enum: "latest" "slack" "callback" "facebook" "rocketchat" "telegram" "twilio" "webexteams" "socketio"

```

I will build the solution using [botfront widget Socket.io](https://github.com/botfront/rasa-webchat) and share the project in this thread 😃

Reference: [Rasa Reminders are not working in custom UI - #2 by Tobias\_Wochinger](http://forum.rasa.com/t/rasa-reminders-are-not-working-in-custom-ui/29003/2)

[inject an intent into a conversation](https://rasa.com/docs/rasa/pages/http-api/#operation/triggerConversationIntent)

---

<div class="post-metadata">

**Author:** ![lis](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/lis/32/18141_2.png) [@lis](https://forum.rasa.com/u/lis)\
**Post date:** [April 21, 2022, 6:03am UTC](https://forum.rasa.com/t/delay-in-dispatcher-utter-message/52687/14 "2022-04-21T06:03:55Z")

</div>

Thanks @itsjhonny
