I found a countdown function on
https://www.programiz.com/python-programming/examples/countdown-timer
which i want to imply to my telegram bot function.
When the below code run, it will send the message to the bot immediately and after 5s.
But what i want is to send the message only after 5s.
Could someone tell me what’s wrong with the code? Greatly appreciate.
import time
import telegram_bot
def countdown(time_sec):
while time_sec:
mins, secs = divmod(time_sec, 60)
timeformat = '{:02d}:{:02d}'.format(mins, secs)
print(timeformat, end='r')
time.sleep(1)
time_sec -= 1
telegram_bot.telegram_bot_sendtext("Intruder detected!!")
countdown(5)
3
Answers
Maybe i should think it easier, which is combined two functions. And it worked
why not just this:
but if you want to use this function this would prevent initial message:
I believe this is a more elegant and simple solution:
If you wanted to run this function in the background while the rest of the code keeps running, you can do this: