From 40465643b9227e4ed10bf825672462517029007d Mon Sep 17 00:00:00 2001 From: Artem Lavrenov Date: Fri, 21 Jan 2022 12:32:23 +0300 Subject: [PATCH] Add timer_bot sync and async example --- .../asynchronous_telebot/timer_bot_async.py | 52 +++++++++++++++++++ examples/timer_bot.py | 43 +++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/asynchronous_telebot/timer_bot_async.py create mode 100644 examples/timer_bot.py diff --git a/examples/asynchronous_telebot/timer_bot_async.py b/examples/asynchronous_telebot/timer_bot_async.py new file mode 100644 index 0000000..1d1da65 --- /dev/null +++ b/examples/asynchronous_telebot/timer_bot_async.py @@ -0,0 +1,52 @@ +#!/usr/bin/python3 + +# This is a simple bot with schedule timer +# https://github.com/ibrb/python-aioschedule +# https://schedule.readthedocs.io + +import asyncio +import os, aioschedule +from telebot.async_telebot import AsyncTeleBot + +API_TOKEN = '' +bot = AsyncTeleBot(API_TOKEN) + + +async def beep(chat_id) -> None: + """Send the beep message.""" + await bot.send_message(chat_id, text='Beep!') + aioschedule.clear(chat_id) # return schedule.CancelJob not working in aioschedule use tag for delete + + +@bot.message_handler(commands=['help', 'start']) +async def send_welcome(message): + await bot.reply_to(message, "Hi! Use /set to set a timer") + + +@bot.message_handler(commands=['set']) +async def set_timer(message): + args = message.text.split() + if len(args) > 1 and args[1].isdigit(): + sec = int(args[1]) + aioschedule.every(sec).seconds.do(beep, message.chat.id).tag(message.chat.id) + else: + await bot.reply_to(message, 'Usage: /set ') + + +@bot.message_handler(commands=['unset']) +def unset_timer(message): + aioschedule.clean(message.chat.id) + + +async def scheduler(): + while True: + await aioschedule.run_pending() + await asyncio.sleep(1) + + +async def main(): + await asyncio.gather(bot.infinity_polling(), scheduler()) + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/examples/timer_bot.py b/examples/timer_bot.py new file mode 100644 index 0000000..85f6697 --- /dev/null +++ b/examples/timer_bot.py @@ -0,0 +1,43 @@ +#!/usr/bin/python + +# This is a simple bot with schedule timer +# https://schedule.readthedocs.io + +import os, time, threading, schedule +from telebot import TeleBot + +API_TOKEN = '' +bot = TeleBot(API_TOKEN) + + +@bot.message_handler(commands=['help', 'start']) +def send_welcome(message): + bot.reply_to(message, "Hi! Use /set to set a timer") + + +def beep(chat_id) -> None: + """Send the beep message.""" + bot.send_message(chat_id, text='Beep!') + return schedule.CancelJob # Run a job once + + +@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 ') + + +@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)