I am writing a bot for tg, it is necessary that the bot respond to the user two hours after the start. How can this be well organized?
- library or framework do you use? - Dima Kaukin
- Do you mean to write a bot? Yuzayu telebot - Scream Captain
- at start or after the user's first message, remember chat_id, and write a message through it to the user - gil9red
- import time # start the timer begin_time = time.time () # perform a long action time.sleep (1) # get the expiration time from the beginning of the timer start end_time = time.time () print end_time - begin_time # another long action time.sleep ( 2) # we get the end time from the start of the timer end_time = time.time () print end_time - begin_time - SoftQualityRC
- Something went wrong .. I wanted to say, and what is not an option in the Python itself to use the timer out of the box? - SoftQualityRC
|
1 answer
A simple example of implementation is in a separate thread (read about asynchrony):
import time from threading import Thread class my_users: def __init__(self): pass def post_msg(self): time.sleep(7200) # написать пользователю user1 = my_users() Thread(target = user1.post_msg).start() - one
target = user1.post_msg()problem here, because target is not a reference to the function, but the result of its execution. Those. your code is the same as:Thread(target=None).start(), and you need to do this:Thread(target=user1.post_msg).start()- gil9red - thanks, corrected - Eugene Dennis
- Why make such a crutch when there is
threading.Timer? - Pavel Durmanov
|