#!/usr/bin/python3 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 # Check token in environment variables if "TELEBOT_BOT_TOKEN" not in os.environ: raise AssertionError("Please configure TELEBOT_BOT_TOKEN as environment variables") 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, party_size: int) -> None: if playing: if not display_name in self.statuses: await self.__notifyFriendPlaying(display_name, party_size) self.statuses[display_name] = time.time() else: diff = time.time() - self.statuses[display_name] if diff > 60 * 60: # 60 minutes self.__notifyFriendPlaying(display_name, party_size) else: self.statuses[display_name] = time.time() async def __notifyFriendPlaying(self, display_name: str, party_size: int): await send_message_to_all(formatFriendOnline(display_name, party_size)) bot = telebot.async_telebot.AsyncTeleBot(os.environ["TELEBOT_BOT_TOKEN"]) userRepository = UserRepository('db.sqlite') fortniteStatusWrapper = FortniteStatusNotifier(FortniteStatusObserver()) fortniteClient = FortniteClient(FortnitePresenceObserver()) @bot.message_handler(commands = ['start']) async def startCommand(message: telebot.types.Message): if message.chat.type == 'private': alias = message.chat.username else: alias = message.chat.title userRepository.putUser(message.chat.id, alias) await reply(message, 'This chat successfully registered to receive Fortnite updates') @bot.message_handler(commands = ['status']) async def getStatus(message): await reply(message, formatFortniteStatus(fortniteStatus.getStatus())) @bot.message_handler(commands = ['friends']) async def getFriends(message): await reply(message, await formatFriends(fortniteClient.get_friends())) @bot.message_handler(commands = ['find']) async def findUser(message): arg = message.text.split() if len(arg) > 1: 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) 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') @bot.message_handler(commands = ['add']) async def addUser(message): arg = message.text.split() if len(arg) > 1: user_id = arg[1] print('Adding user with ID as friend {}'.format(user_id)) await fortniteClient.add_friend(user_id) await reply(message, 'Send friend request successfully') else: await reply(message, 'Usage: /add username') async def send_message_to_all(message_text: str): for user in userRepository.getAllUsers(): try: await bot.send_message( user[0], message_text, parse_mode='MarkdownV2' ) except Exception as error: if 'bot was kicked from the group chat' in str(error): userRepository.removeChat(user[0]) async def reply(message, message_text): await bot.reply_to( message, message_text, parse_mode='MarkdownV2') async def run_tgbot(): await bot.polling() async def run_fortniteStatusWrapper(): await fortniteStatusWrapper.run() async def run_fortniteClient(): fortniteClient.run() 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()