summaryrefslogtreecommitdiff
path: root/tgbot.py
diff options
context:
space:
mode:
authorDmitrii Morozov <snoopdesigns@gmail.com>2024-04-09 00:39:38 +0200
committerDmitrii Morozov <snoopdesigns@gmail.com>2024-04-09 00:39:38 +0200
commit997fe0d4aaaeb5be81583b8ad4d82935d6118969 (patch)
tree593e25e05801855b2735e2b267c3dedf15a19edc /tgbot.py
parent7b992afe70925f244ca6e9746071ae71e497b54e (diff)
Notify changed status
Diffstat (limited to 'tgbot.py')
-rwxr-xr-xtgbot.py22
1 files changed, 20 insertions, 2 deletions
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'
)