From 6d4133a04d79b3313c352e536788333af491d4f8 Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Fri, 21 Jun 2024 20:17:43 +0200 Subject: Reworked statistics --- telegram_bot/__init__.py | 12 ++++++++- telegram_bot/commands.py | 68 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 13 deletions(-) (limited to 'telegram_bot') diff --git a/telegram_bot/__init__.py b/telegram_bot/__init__.py index 5e3d455..0818d67 100644 --- a/telegram_bot/__init__.py +++ b/telegram_bot/__init__.py @@ -10,6 +10,10 @@ class CommandHandler: async def handle(self, message: telebot.types.Message): pass +class CallbackQueryHandler: + async def handle(self, call: telebot.types.CallbackQuery): + pass + class TelegramBot: __bot: AsyncTeleBot __user_repository: UserRepository @@ -35,6 +39,11 @@ class TelegramBot: command_handler.handle, commands=[ command ]) + def register_callback_query(self, callback_query_handler: CallbackQueryHandler, filter: typing.Callable): + self.__bot.register_callback_query_handler( + callback=callback_query_handler.handle, + func=filter) + async def send_message_to_all(self, message_text: str): for user in self.__user_repository.get_all_users(): try: @@ -47,8 +56,9 @@ class TelegramBot: if 'bot was kicked from the group chat' in str(error): self.__user_repository.remove_chat(user[0]) - async def reply(self, message, message_text): + async def reply(self, message, message_text, reply_markup = None): await self.__bot.reply_to( message, message_text, + reply_markup=reply_markup, parse_mode='MarkdownV2') \ No newline at end of file diff --git a/telegram_bot/commands.py b/telegram_bot/commands.py index 1e3295b..a1a2888 100644 --- a/telegram_bot/commands.py +++ b/telegram_bot/commands.py @@ -1,10 +1,16 @@ import telebot +from datetime import date, datetime from telegram_bot import * from formatter import * from persistence import * from fortnite_client import * from fortnite_status import * +__stats_now__ = 'stats_now' +__stats_day__ = 'stats_day' +__stats_week__ = 'stats_week' +__stats_month__ = 'stats_month' + class StartCommand(CommandHandler): __telegram_bot: TelegramBot @@ -48,39 +54,77 @@ class GetFriendsCommand(CommandHandler): friends = await self.__fortnite_client.get_friends() await self.__telegram_bot.reply(message, format_users(friends)) - -class GetStatsCommand(CommandHandler): +class GetStatsCallbackQueryHandler(CallbackQueryHandler): __telegram_bot: TelegramBot __fortnite_client: FortniteClient + __stats_repository: StatsRepository - def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient): + def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient, stats_repository: StatsRepository): self.__telegram_bot = telegram_bot self.__fortnite_client = fortnite_client + self.__stats_repository = stats_repository - async def handle(self, message: telebot.types.Message): + async def handle(self, call: telebot.types.CallbackQuery): if self.__fortnite_client.is_initialized(): + if call.data == __stats_now__: + await self.reply_with_today_stats(call.message) + elif call.data == __stats_day__: + await self.reply_with_stats_days_difference(call.message, 1, 'day') + elif call.data == __stats_week__: + await self.reply_with_stats_days_difference(call.message, 7, 'week') + elif call.data == __stats_month__: + await self.reply_with_stats_days_difference(call.message, 30, 'month') + + async def reply_with_today_stats(self, message): + friends = await self.__fortnite_client.get_friends() + stats = [await friend.fetch_stats() for friend in friends] + await self.__telegram_bot.reply(message, format_user_stats_list(stats)) + + async def reply_with_stats_days_difference(self, message, days: int, timeframe_alias: str): + stats_date = date.today() - datetime.timedelta(days) + persisted_stats = self.__stats_repository.get_stats(stats_date) + if len(persisted_stats) > 0: friends = await self.__fortnite_client.get_friends() - stats = [await friend.fetch_stats() for friend in friends] - await self.__telegram_bot.reply(message, format_user_stats_list(stats)) + current_stats = [await friend.fetch_stats() for friend in friends] + await self.__telegram_bot.reply(message, format_user_stats_difference(persisted_stats, current_stats, timeframe_alias)) + else: + await self.__telegram_bot.reply(message, 'No stats available yet for selected time period') -class GetTodayStatsCommand(CommandHandler): +class GetStatsCommand(CommandHandler): __telegram_bot: TelegramBot __fortnite_client: FortniteClient __stats_repository: StatsRepository + __reply_markup: any def __init__(self, telegram_bot: TelegramBot, fortnite_client: FortniteClient, stats_repository: StatsRepository): self.__telegram_bot = telegram_bot self.__fortnite_client = fortnite_client self.__stats_repository = stats_repository + self.__reply_markup = telebot.types.InlineKeyboardMarkup() + self.__reply_markup.add(telebot.types.InlineKeyboardButton('Now', callback_data=__stats_now__)) + self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 day', callback_data=__stats_day__)) + self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 week', callback_data=__stats_week__)) + self.__reply_markup.add(telebot.types.InlineKeyboardButton('1 month', callback_data=__stats_month__)) + + self.__telegram_bot.register_callback_query( + GetStatsCallbackQueryHandler(self.__telegram_bot, self.__fortnite_client, self.__stats_repository), + lambda call: True) + async def handle(self, message: telebot.types.Message): - if self.__fortnite_client.is_initialized(): - persisted_stats = self.__stats_repository.get_stats() - friends = await self.__fortnite_client.get_friends() - current_stats = [await friend.fetch_stats() for friend in friends] - await self.__telegram_bot.reply(message, format_user_stats_difference(persisted_stats, current_stats)) + await self.__telegram_bot.reply(message, 'Which statistics would you like to see?', reply_markup=self.__reply_markup) + +class GetTodayStatsCommand(CommandHandler): + + __telegram_bot: TelegramBot + + def __init__(self, telegram_bot: TelegramBot): + self.__telegram_bot = telegram_bot + + async def handle(self, message: telebot.types.Message): + await self.__telegram_bot.reply(message, 'Use new command /stats, you must, young Padawan') class RecordStatsCommand(CommandHandler): -- cgit v1.2.3