diff options
author | Dmitrii Morozov <snoopdesigns@gmail.com> | 2024-03-28 19:31:10 +0100 |
---|---|---|
committer | Dmitrii Morozov <snoopdesigns@gmail.com> | 2024-03-28 19:31:10 +0100 |
commit | 9546faa2601c1b314bda0f6a0ee7bdd799a13061 (patch) | |
tree | 875ea567953fe4b5e94594d3bd4f59b5ff734f2d /tgbot.py | |
parent | 27334b6e89cdec616ea6ac180ec1047a0bd4f54f (diff) |
Async notification for changes in status
Diffstat (limited to 'tgbot.py')
-rwxr-xr-x | tgbot.py | 53 |
1 files changed, 16 insertions, 37 deletions
@@ -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 <seconds>') - - -@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 |