Messenger Game: Delay Bot Responses

Hello everyone,

We are currently developing a messenger game with Rasa and we are searching for a solution to delay the bots’ responses. In order to create more immersion and human-like behavior, it would be great if we could delay the responses several seconds.

We already tried reminders and a delay in the custom action, but that doesn’t work as expected.

Am I missing something? Any ideas?

Thanks, Max

1 Like

As you said, you could use sleep() inside custom actions. To make it more human like, you can sleep() for the amount of time it would take a human to write the text :slight_smile:

Let’s say the average typing speed is 200 CPM. So it would take length/speed minutes to write the text (or 60*length/speed seconds).

cpm = 200
text = '...'
sleep(60 * len(text) / cpm)
dispatcher.utter_message(text)

Maybe play a bit with the value of cpm to find the one that seems the most natural.

Thanks for the quick help. I think I haven’t specified my question enough, sorry for that.

Delay works for single utterances. Our exact problem comes with multiple utterances in a row without user input. If we put a delay in between the utterances everything is sent together as a package.

Hope this was described well enough.

1 Like

Ah, got it.

You could still use sleep() for individual utterances, but create multiple custom actions for every utterance in the same “group”, linked by the FollowupAction event. In the custom action’s run() method, add FollowupAction('next_action_name') to your return list.

Or, if you rather keep every “group” in the same action, you could create a slot that counts the number of individual utterances and utters the next phrase accordingly:

utterance_number = tracker.get_slot("utterance_number")
text = ""
events = []

if utterance_number == 0:
    text = "text 0"
else if utterance_number == 1:
    text = "text 1"
else if utterance_number == 2:
    text = "text 2"
else:
    text = "text 3"
    
if utterance_number < 3:
    events.append(FollowupAction(this.name()))
else:
    events.append(FollowupAction("next_action_name"))

events.append(SlotSet("utterance_number", (utterance_number + 1) % 4))

sleep(60 * len(text) / cpm)
dispatcher.utter_message(text)

return events

asyncio - Task exception was never retrieved future: <Task finished coro=<configure_app..run_cmdline_io() done

I just tried it out and got this error. It probably ends in a timeout, as seen in some other threads with a similar issue.

That’s weird… I use follow-up actions and never encountered a problem with them…

Maybe the timeout is caused by the sleep()? What if you don’t use it? (I know you still want sleep(), but just curious)

In order to create more immersion and human-like behavior, it would be great if we could delay the responses several seconds.

One comment about this: immersion for a game can be fine, but one should avoid creating confusion in a conversational setting. The user should always understand that they are talking to a virtual assistant as opposed to a human being.

On the topic of sleep, if your custom action is implemented asynchronously then your actions may not want to use the standard time.sleep. The best explainer for this effect might be this PyData Amsterdam video.

1 Like

Creating and queuing follow-up actions works perfectly fine without the sleep(). As soon as we put a delay between them it results in the error I mentioned above. We now tested the whole thing in Rasa X. There we don’t get this error but everything gets sent at the same time again. We can’t think of any further solution but combining the utterances in a single long utterance with a longer delay in front.

asyncio.sleep sadly couldn’t fix the problem.

One last idea would have been using reminders but there we have those unintended text messages in the chat that just doesn’t work in the game context. Is there maybe a way to hide those?

1 Like

What are these unintended text messages?

I thought the text messages “EXTERNAL: external_reminder” you can see in rasa x can’t be removed. After I checked with Telegram and Facebook connection I realized that those texts are only for development.

I now managed to create a version with very short reminders and follow-up actions that simulate the time the bot needs to write the messages.

Thanks again for the support! :smiley:

1 Like