From 9518ef9f076bd10dff48ee8eb7fd3a12c130a1e3 Mon Sep 17 00:00:00 2001 From: Zakhar Izmaylov Date: Sat, 9 Nov 2024 15:35:22 +0300 Subject: [PATCH] Refactoring profile.py --- handlers/profile.py | 103 +++++++++++++++++++++----------------------- handlers/start.py | 18 ++++---- 2 files changed, 56 insertions(+), 65 deletions(-) diff --git a/handlers/profile.py b/handlers/profile.py index b6cfb722..4646d5ec 100644 --- a/handlers/profile.py +++ b/handlers/profile.py @@ -4,6 +4,7 @@ from aiogram import F, Router, types from aiogram.fsm.context import FSMContext from aiogram.fsm.state import State, StatesGroup from aiogram.types import BufferedInputFile, InlineKeyboardButton, InlineKeyboardMarkup +from aiogram.utils.keyboard import InlineKeyboardBuilder from loguru import logger from bot import bot @@ -16,34 +17,24 @@ from handlers.texts import ( profile_message_send, ) - -class ReplenishBalanceState(StatesGroup): - choosing_transfer_method = State() - waiting_for_admin_confirmation = State() - - router = Router() async def process_callback_view_profile( callback_query: types.CallbackQuery, state: FSMContext ): - tg_id = callback_query.from_user.id + chat_id = callback_query.from_user.id username = callback_query.from_user.full_name image_path = os.path.join(os.path.dirname(__file__), "pic.jpg") - if not os.path.isfile(image_path): - await bot.send_message(tg_id, "Файл изображения не найден.") - return - try: - key_count = await get_key_count(tg_id) - balance = await get_balance(tg_id) + key_count = await get_key_count(chat_id) + balance = await get_balance(chat_id) if balance is None: balance = 0 - profile_message = profile_message_send(username, tg_id, balance, key_count) + profile_message = profile_message_send(username, chat_id, balance, key_count) profile_message += f"Обязательно подпишитесь на канал здесь\n" @@ -52,35 +43,26 @@ async def process_callback_view_profile( "\nНажмите ➕Устройство снизу, чтобы добавить устройство в VPN" ) - inline_keyboard = InlineKeyboardMarkup( - inline_keyboard=[ - [ - InlineKeyboardButton( - text="➕ Устройство", callback_data="create_key" - ), - InlineKeyboardButton( - text="📱 Мои устр-ва", callback_data="view_keys" - ), - ], - [ - InlineKeyboardButton( - text="💳 Пополнить баланс", - callback_data=( - "pay_freekassa" - if PAYMENT_METHOD == "freekassa" - else "replenish_balance" - ), - ) - ], - [ - InlineKeyboardButton(text="👥 Пригласить", callback_data="invite"), - InlineKeyboardButton( - text="📘 Инструкции", callback_data="instructions" - ), - ], - [InlineKeyboardButton(text="⬅️ Назад", callback_data="back_to_menu")], - ] + builder = InlineKeyboardBuilder() + builder.add( + InlineKeyboardButton(text="➕ Устройство", callback_data="create_key"), + InlineKeyboardButton(text="📱 Мои устр-ва", callback_data="view_keys"), ) + builder.add( + InlineKeyboardButton( + text="💳 Пополнить баланс", + callback_data=( + "pay_freekassa" + if PAYMENT_METHOD == "freekassa" + else "replenish_balance" + ), + ) + ) + builder.add( + InlineKeyboardButton(text="👥 Пригласить", callback_data="invite"), + InlineKeyboardButton(text="📘 Инструкции", callback_data="instructions"), + ) + builder.add(InlineKeyboardButton(text="⬅️ Назад", callback_data="back_to_menu")) # Попробуем удалить предыдущее сообщение try: @@ -90,37 +72,48 @@ async def process_callback_view_profile( f"Ошибка при удалении сообщения: {e}" ) # Логируем ошибку, если удаление не удалось - with open(image_path, "rb") as image_file: - await bot.send_photo( - chat_id=tg_id, - photo=BufferedInputFile(image_file.read(), filename="pic.jpg"), - caption=profile_message, + if not os.path.isfile(image_path): + with open(image_path, "rb") as image_file: + await bot.send_photo( + chat_id=chat_id, + photo=BufferedInputFile(image_file.read(), filename="pic.jpg"), + caption=profile_message, + parse_mode="HTML", + reply_markup=builder.as_markup(), + ) + else: + await bot.send_message( + chat_id=chat_id, + text=profile_message, parse_mode="HTML", - reply_markup=inline_keyboard, + reply_markup=builder.as_markup(), ) except Exception as e: - await bot.send_message(tg_id, f"❗️ Ошибка при получении данных профиля: {e}") + await bot.send_message(chat_id, f"❗️ Ошибка при получении данных профиля: {e}") await callback_query.answer() @router.callback_query(F.data == "invite") async def invite_handler(callback_query: types.CallbackQuery): - tg_id = callback_query.from_user.id - referral_link = get_referral_link(tg_id) + chat_id = callback_query.from_user.id + referral_link = get_referral_link(chat_id) - referral_stats = await get_referral_stats(tg_id) + referral_stats = await get_referral_stats(chat_id) invite_message = invite_message_send(referral_link, referral_stats) - button_back = InlineKeyboardButton(text="⬅️ Назад", callback_data="view_profile") - keyboard = InlineKeyboardMarkup(inline_keyboard=[[button_back]]) + builder = InlineKeyboardBuilder() + builder.add(InlineKeyboardButton(text="⬅️ Назад", callback_data="view_profile")) await callback_query.message.delete() await bot.send_message( - chat_id=tg_id, text=invite_message, parse_mode="HTML", reply_markup=keyboard + chat_id=chat_id, + text=invite_message, + parse_mode="HTML", + reply_markup=builder.as_markup(), ) await callback_query.answer() diff --git a/handlers/start.py b/handlers/start.py index 9ece311d..569ea216 100644 --- a/handlers/start.py +++ b/handlers/start.py @@ -35,8 +35,10 @@ async def send_welcome_message(chat_id: int, trial_status: int): InlineKeyboardButton(text="👤 Мой профиль", callback_data="view_profile") ) builder.add(InlineKeyboardButton(text="🔒 О VPN", callback_data="about_vpn")) - builder.add(InlineKeyboardButton(text="📞 Поддержка", url=SUPPORT_CHAT_URL)) - builder.add(InlineKeyboardButton(text="📢 Наш канал", url=CHANNEL_URL)) + builder.add( + InlineKeyboardButton(text="📞 Поддержка", url=SUPPORT_CHAT_URL), + InlineKeyboardButton(text="📢 Наш канал", url=CHANNEL_URL), + ) if not os.path.isfile(image_path): with open(image_path, "rb") as image_from_buffer: @@ -113,26 +115,22 @@ async def handle_connect_vpn(callback_query: CallbackQuery): InlineKeyboardButton( text="🍏 Подключить", url=f'{APP_URL}/?url=v2raytun://import/{trial_key_info["key"]}', - ) - ) - builder.add( + ), InlineKeyboardButton( text="🤖 Подключить", url=f'{APP_URL}/?url=v2raytun://import-sub?url={trial_key_info["key"]}', - ) + ), ) builder.add( InlineKeyboardButton( text="🍏 Скачать", url="https://apps.apple.com/ru/app/v2raytun/id6476628951", - ) - ) - builder.add( + ), InlineKeyboardButton( text="🤖 Скачать", url="https://play.google.com/store/apps/details?id=com.v2raytun.android&hl=ru", - ) + ), ) await callback_query.message.answer(