How to add delay between chatbot's responses?

Hello everyone!

I’ve developed a chatbot for Messenger platform that uses sender actions (typing and seen). I would like these actions to look more natural by adding a delay between the seen and typing indication.

For instance, user sends a message and bot shows “seen” action for 1 sec. Then, bot shows “typing” action for 2 sec until a response is sent. I’ve tried this with time.sleep() method in python, but I believe it isn’t a good practice.

Has anyone tried this? Any help or suggestion will be appreciated.

@stavr Have you looked into reminders?

Ideally this sounds more like something you should be doing on the frontend, rather than forcing the bot to delay its response.

If you’re developing your own frontend in, say, Javascript, its as easy as adding in a sleep() statement between receiving and displaying the message.

If instead you’re developing for something like Slack or Facebook, their respective bot APIs should ideally have bools for “is_typing” or something along those lines, which you can set and delay before responding.

3 Likes

Did anyone find a good solution for this? I have a similar issue where I’d like to delay the bot’s next utterance and give the user ~10 seconds to respond to the last message before moving forward.

Have a look at threading.Timer. It runs your function in a new thread without using sleep().

from threading import Timer

def hello(): print “hello, world”

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

The second method to delay would be using the implicit wait method:

driver.implicitly_wait(5)

The third method is more useful when you have to wait until a particular action is completed or until an element is found:

self.wait.until(EC.presence_of_element_located((By.ID, 'UserName'))

Python sleep() will pause for an hour, day or whatever if given the proper value. It does not allow other processes take place (in same script) however. A better way is to use an event which will create an event on timeout.