From 9546faa2601c1b314bda0f6a0ee7bdd799a13061 Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Thu, 28 Mar 2024 19:31:10 +0100 Subject: Async notification for changes in status --- tgbot.py | 53 ++++++++++++++++------------------------------------- 1 file changed, 16 insertions(+), 37 deletions(-) (limited to 'tgbot.py') diff --git a/tgbot.py b/tgbot.py index 6d4fc52..99d6351 100755 --- a/tgbot.py +++ b/tgbot.py @@ -3,55 +3,34 @@ import os import time, threading, schedule import telebot -from fortniteStatusWrapper import formatFortniteStatus +from fortniteStatusWrapper import * +from fortniteStatusFormatter import * from persistence import UserRepository -from pythonFortniteStatus.FortniteStatus import * if "TELEBOT_BOT_TOKEN" not in os.environ: raise AssertionError("Please configure TELEBOT_BOT_TOKEN as environment variables") bot = telebot.TeleBot(os.environ["TELEBOT_BOT_TOKEN"]) -fortniteStatus = FortniteStatus() userRepository = UserRepository('db.sqlite') +fortniteStatusWrapper = FortniteStatusWrapper() @bot.message_handler(commands = ['start']) def startCommand(message): bot.reply_to(message, "This bot is doing nothing so far..") - print(message.chat.id) userRepository.putUser(message.chat.id) -@bot.message_handler(commands = ['status']) -def fortniteStatusCommand(message): - bot.send_message( - message.chat.id, - formatFortniteStatus(fortniteStatus.getStatus()), - parse_mode='MarkdownV2' - ) - -#bot.polling(none_stop=True, interval=0) - -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 ') - - -@bot.message_handler(commands=['unset']) -def unset_timer(message): - schedule.clear(message.chat.id) - +class FortniteStatusObserver(Observer): + def update(self, fortniteStatus) -> None: + for user in userRepository.getAllUsers(): + bot.send_message( + user[0], + formatFortniteStatus(fortniteStatus), + parse_mode='MarkdownV2' + ) if __name__ == '__main__': - threading.Thread(target=bot.infinity_polling, name='bot_infinity_polling', daemon=True).start() - while True: - schedule.run_pending() - time.sleep(1) \ No newline at end of file + fortniteStatusWrapper.attach(FortniteStatusObserver()) + + main_thread = threading.Thread(target=bot.infinity_polling, name='bot_infinity_polling', daemon=True) + main_thread.start() + main_thread.join() \ No newline at end of file -- cgit v1.2.3