From 997fe0d4aaaeb5be81583b8ad4d82935d6118969 Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Tue, 9 Apr 2024 00:39:38 +0200 Subject: Notify changed status --- DeviceAuth.py | 4 ---- tgbot.py | 22 ++++++++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/DeviceAuth.py b/DeviceAuth.py index d62731c..3374956 100644 --- a/DeviceAuth.py +++ b/DeviceAuth.py @@ -5,10 +5,6 @@ class DeviceAuth: __filename__ = 'device-auth.json' - def __init__(self): - print('DeviceAuth init') - - def device_auth_file_exists(self): return os.path.isfile(self.__filename__) diff --git a/tgbot.py b/tgbot.py index 72e7aa9..2df88ff 100755 --- a/tgbot.py +++ b/tgbot.py @@ -8,6 +8,7 @@ from FortniteStatusFormatter import * from FortniteClient import * from FortniteEvents import * from persistence import UserRepository +from datetime import datetime if "TELEBOT_BOT_TOKEN" not in os.environ: raise AssertionError("Please configure TELEBOT_BOT_TOKEN as environment variables") @@ -19,8 +20,8 @@ fortniteClient = FortniteClient() @bot.message_handler(commands = ['start']) def startCommand(message): - bot.reply_to(message, "This bot is doing nothing so far..") userRepository.putUser(message.chat.id) + bot.reply_to(message, "This chat successfully registered to receive Fortnite updates!") class FortniteStatusObserver(Observer): def update(self, fortniteStatus) -> None: @@ -32,11 +33,28 @@ class FortniteStatusObserver(Observer): ) class FortnitePresenceObserver(PresenceObserver): + + # Map name -> last seen not playing timestamp seconds + statuses = {} + def update(self, display_name: str, playing: bool) -> None: + print('FortnitePresenceObserver: {} playing = {}'.format(display_name, playing)) + if playing: + if not display_name in self.statuses: + self.__notifyFriendPlaying(display_name) + self.statuses[display_name] = time.time() + else: + diff = time.time() - self.statuses[display_name] + if diff > 60 * 60: # 60 minutes + self.__notifyFriendPlaying(display_name) + else: + self.statuses[display_name] = time.time() + + def __notifyFriendPlaying(self, display_name: str): for user in userRepository.getAllUsers(): bot.send_message( user[0], - 'Fortnite observer {}, playing {}'.format(display_name, playing), + '{} is online'.format(display_name), parse_mode='MarkdownV2' ) -- cgit v1.2.3