'Run code in specific time with telebot
I want to send message from my bot in specific time, in 7:00 every day, WITHOUT cron and stuff like that. How I can do it???
import telebot
bot = telebot.TeleBot("TOKEN")
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
bot.reply_to(message, "Howdy, how are you doing?")
bot.polling()
I can't do if by myself because of bot.polling(). This line just throw my program into the infinity loop, where i can't insert
if time() == specific time:
send.message()
Please, help me
Solution 1:[1]
You can use the thread, datetime and time.
import threading
import telebot
import time
import datetime
def message_timer():
while 1:
if str (datetime.datetime.now().strftime("% H")) == "7":
bot.send_message(user_id, "Message")
# 60*65 because it doesn't send the message continuously but only does it once
time.sleep(60*65)
# Do some stuff ...
x = threading.Thread(target=message_timer)
x.start()
# Do some stuff ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
