From a53ea1d94a25f010dbba8a471bc832874bbb4ae7 Mon Sep 17 00:00:00 2001 From: ue86388 Date: Wed, 10 Apr 2024 15:09:51 +0200 Subject: Make it work again --- tgbot.py | 101 +++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 49 insertions(+), 52 deletions(-) (limited to 'tgbot.py') diff --git a/tgbot.py b/tgbot.py index 38b1789..5422cb6 100755 --- a/tgbot.py +++ b/tgbot.py @@ -1,25 +1,44 @@ #!/usr/bin/python3 -import os -import time, threading, schedule -import telebot -import asyncio -import telebot.async_telebot -from FortniteStatusWrapper import * -from FortniteStatusFormatter import * +import os, time, telebot, asyncio, telebot.async_telebot, nest_asyncio +from FortniteStatusNotifier import * +from Formatter import * from FortniteClient import * from FortniteEvents import * from persistence import UserRepository -from datetime import datetime +# Check token in environment variables 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"]) +class FortniteStatusObserver(Observer): + async def update(self, fortniteStatus) -> None: + await send_message_to_all(formatFortniteStatus(fortniteStatus)) + +class FortnitePresenceObserver(PresenceObserver): + # Map name -> last seen not playing timestamp seconds + statuses = {} + + async 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: + await 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() + + async def __notifyFriendPlaying(self, display_name: str): + await send_message_to_all(formatFriendOnline(display_name)) + bot = telebot.async_telebot.AsyncTeleBot(os.environ["TELEBOT_BOT_TOKEN"]) userRepository = UserRepository('db.sqlite') -fortniteStatusWrapper = FortniteStatusWrapper() -fortniteClient = FortniteClient() +fortniteStatusWrapper = FortniteStatusNotifier(FortniteStatusObserver()) +fortniteClient = FortniteClient(FortnitePresenceObserver()) @bot.message_handler(commands = ['start']) async def startCommand(message): @@ -32,7 +51,7 @@ async def getStatus(message): @bot.message_handler(commands = ['friends']) async def getFriends(message): - await reply(message, formatFriends(fortniteClient.get_friends())) + await reply(message, await formatFriends(fortniteClient.get_friends())) @bot.message_handler(commands = ['find']) async def findUser(message): @@ -41,9 +60,11 @@ async def findUser(message): search_user_display_name = arg[1] print('Searching users by name {}'.format(search_user_display_name)) users: typing.List[fortnitepy.User] = await fortniteClient.fetch_users_by_display_name(search_user_display_name) - for user in users: - stats = await user.fetch_br_stats() - await reply(message, formatUser(user, stats)) + if (len(users) > 0): + for user in users: + await reply(message, await formatUser(user)) + else: + await reply(message, 'User {} not found'.format(search_user_display_name)) else: await reply(message, 'Usage: /find username') @@ -58,31 +79,6 @@ async def addUser(message): else: await reply(message, 'Usage: /add username') -class FortniteStatusObserver(Observer): - async def update(self, fortniteStatus) -> None: - await send_message_to_all(formatFortniteStatus(fortniteStatus)) - -class FortnitePresenceObserver(PresenceObserver): - - # Map name -> last seen not playing timestamp seconds - statuses = {} - - async 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: - await 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() - - async def __notifyFriendPlaying(self, display_name: str): - await send_message_to_all('{} is online'.format(display_name)) - async def send_message_to_all(message_text: str): for user in userRepository.getAllUsers(): await bot.send_message( @@ -97,19 +93,20 @@ async def reply(message, message_text): message_text, parse_mode='MarkdownV2') -async def run_fortnite_client(): - await fortniteClient.run() +async def run_tgbot(): + await bot.polling() -if __name__ == '__main__': - fortniteStatusWrapper.attach(FortniteStatusObserver()) - fortniteClient.attach(FortnitePresenceObserver()) +async def run_fortniteStatusWrapper(): + await fortniteStatusWrapper.run() - loop = asyncio.get_event_loop() +async def run_fortniteClient(): + fortniteClient.run() - tasks = [ - loop.create_task(bot.polling()), - loop.create_task(fortniteStatusWrapper.run()), - loop.create_task(run_fortnite_client()) - ] - loop.run_until_complete(asyncio.wait(tasks)) +async def run_all(): + await asyncio.gather(run_tgbot(), run_fortniteStatusWrapper(), run_fortniteClient()) + +if __name__ == '__main__': + nest_asyncio.apply() + loop = asyncio.get_event_loop() + loop.run_until_complete(run_all()) loop.close() \ No newline at end of file -- cgit v1.2.3