1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
pyTelegramBotAPI/examples/timer_bot.py

43 lines
1.1 KiB
Python
Raw Normal View History

2022-01-21 12:32:23 +03:00
#!/usr/bin/python
# This is a simple bot with schedule timer
# https://schedule.readthedocs.io
2022-01-21 22:25:06 +03:00
import time, threading, schedule
2022-01-21 12:32:23 +03:00
from telebot import TeleBot
API_TOKEN = '<api_token>'
bot = TeleBot(API_TOKEN)
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
bot.reply_to(message, "Hi! Use /set <seconds> to set a timer")
def beep(chat_id) -> None:
"""Send the beep message."""
bot.send_message(chat_id, text='Beep!')
@bot.message_handler(commands=['set'])
def set_timer(message):
args = message.text.split()
if len(args) > 1 and args[1].isdigit():
sec = int(args[1])
schedule.every(sec).seconds.do(beep, message.chat.id).tag(message.chat.id)
else:
bot.reply_to(message, 'Usage: /set <seconds>')
@bot.message_handler(commands=['unset'])
def unset_timer(message):
schedule.clean(message.chat.id)
if __name__ == '__main__':
threading.Thread(target=bot.infinity_polling, name='bot_infinity_polling', daemon=True).start()
while True:
schedule.run_pending()
time.sleep(1)