skip to Main Content

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


  1. Chosen as BEST ANSWER

    Maybe i should think it easier, which is combined two functions. And it worked

    import time
    import concurrent.futures
    import requests
    
    
    def countdown(telegram_message, timex):
        elapsed_time = 0
        while elapsed_time < timex:
            start = time.perf_counter()
            time.sleep(1)  # checks every second
            elapsed_time = elapsed_time + time.perf_counter() - start
        def telegram_bot_sendtext(bot_message):   
            bot_token = 'XXX
            bot_chatID = 'XXX
            send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
            response = requests.get(send_text)
            return response.json()
        test = telegram_bot_sendtext("Intruder detected!!")
    
    msg= "Intruder detected!!"
    delay = 5
    
    concurrent.futures.ThreadPoolExecutor().submit(lambda: countdown(msg, delay)).result()
    
    
    
    

  2. why not just this:

    import time
    import telegram_bot
    def countdown(time_sec):
        time.sleep(time_sec)
        telegram_bot.telegram_bot_sendtext("Intruder detected!!")
    countdown(5)
    

    but if you want to use this function this would prevent initial message:

    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
        if time_sec == 0:
            telegram_bot.telegram_bot_sendtext("Intruder detected!!")
    countdown(5)
    
    Login or Signup to reply.
  3. I believe this is a more elegant and simple solution:

    import time
    
    
    def countdown(message, timex):
        elapsed_time = 0
        while elapsed_time < timex:
            start = time.perf_counter()
            time.sleep(1)  # checks every second
            elapsed_time = elapsed_time + time.perf_counter() - start
    
        telegram_bot.telegram_bot_sendtext("Intruder detected!!")
    
    
    countdown("Intruder detected!!", 5)
    

    If you wanted to run this function in the background while the rest of the code keeps running, you can do this:

    import time
    import concurrent.futures
    
    
    def countdown(telegram_message, timex):
        elapsed_time = 0
        while elapsed_time < timex:
            start = time.perf_counter()
            time.sleep(1)  # checks every second
            elapsed_time = elapsed_time + time.perf_counter() - start
    
        telegram_bot.telegram_bot_sendtext(telegram_message)
    
    
    msg= "Intruder detected!!"
    delay = 5
    
    concurrent.futures.ThreadPoolExecutor().submit(lambda: countdown(msg, delay)).result()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search