From e04ccd893f87f2ea91043b0748beae9221dc8d3e Mon Sep 17 00:00:00 2001 From: Vladless Date: Mon, 18 Nov 2024 14:56:41 +0300 Subject: [PATCH] Fixed unexpected behavior of the device selection button --- handlers/instructions/instructions.py | 74 ++++++++++++++++++++------- handlers/keys/keys.py | 19 +++---- 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/handlers/instructions/instructions.py b/handlers/instructions/instructions.py index 3cf8acfa..76d6865b 100644 --- a/handlers/instructions/instructions.py +++ b/handlers/instructions/instructions.py @@ -4,9 +4,10 @@ from aiogram import F, Router, types from aiogram.types import BufferedInputFile, InlineKeyboardButton, InlineKeyboardMarkup from bot import bot -from config import CONNECT_WINDOWS, SUPPORT_CHAT_URL +from config import CONNECT_WINDOWS, SUPPORT_CHAT_URL, DATABASE_URL from handlers.texts import INSTRUCTION_PC, INSTRUCTIONS, KEY_MESSAGE from logger import logger +import asyncpg router = Router() @@ -42,7 +43,7 @@ async def send_instructions(callback_query: types.CallbackQuery): @router.callback_query(F.data.startswith("connect_pc|")) async def process_connect_pc(callback_query: types.CallbackQuery): tg_id = callback_query.from_user.id - key = callback_query.data.split("|")[1] + key_name = callback_query.data.split("|")[1] try: await bot.delete_message( @@ -51,27 +52,64 @@ async def process_connect_pc(callback_query: types.CallbackQuery): except Exception as e: logger.error(f"Ошибка при удалении сообщения: {e}") - key_message = KEY_MESSAGE.format(key) + try: + conn = await asyncpg.connect(DATABASE_URL) + try: + # Поиск ключа по имени ключа + record = await conn.fetchrow( + """ + SELECT k.key + FROM keys k + WHERE k.tg_id = $1 AND k.email = $2 + """, + tg_id, + key_name, + ) - instruction_message = f"{key_message}{INSTRUCTION_PC}" + if not record: + await bot.send_message( + chat_id=tg_id, + text="Ключ не найден. Проверьте имя ключа.", + parse_mode="HTML", + ) + return - connect_windows_button = types.InlineKeyboardButton( - text="💻 Подключить Windows", url=f"{CONNECT_WINDOWS}{key}" - ) + key = record["key"] + key_message = KEY_MESSAGE.format(key) + instruction_message = f"{key_message}{INSTRUCTION_PC}" - support_button = types.InlineKeyboardButton( - text="🆘 Поддержка", url=f"{SUPPORT_CHAT_URL}" - ) + connect_windows_button = types.InlineKeyboardButton( + text="💻 Подключить Windows", url=f"{CONNECT_WINDOWS}{key}" + ) - back_button = types.InlineKeyboardButton( - text="🔙 Назад в профиль", callback_data="view_profile" - ) + support_button = types.InlineKeyboardButton( + text="🆘 Поддержка", url=f"{SUPPORT_CHAT_URL}" + ) - inline_keyboard = [[connect_windows_button], [support_button], [back_button]] - keyboard = types.InlineKeyboardMarkup(inline_keyboard=inline_keyboard) + back_button = types.InlineKeyboardButton( + text="🔙 Назад в профиль", callback_data="view_profile" + ) - await bot.send_message( - tg_id, instruction_message, reply_markup=keyboard, parse_mode="HTML" - ) + inline_keyboard = [ + [connect_windows_button], + [support_button], + [back_button], + ] + keyboard = types.InlineKeyboardMarkup(inline_keyboard=inline_keyboard) + + await bot.send_message( + tg_id, instruction_message, reply_markup=keyboard, parse_mode="HTML" + ) + + finally: + await conn.close() + + except Exception as e: + logger.error(f"Ошибка при получении ключа: {e}") + await bot.send_message( + chat_id=tg_id, + text="Произошла ошибка. Пожалуйста, повторите попытку позже.", + parse_mode="HTML", + ) await callback_query.answer() diff --git a/handlers/keys/keys.py b/handlers/keys/keys.py index c2dff394..97b01244 100644 --- a/handlers/keys/keys.py +++ b/handlers/keys/keys.py @@ -38,11 +38,11 @@ async def process_callback_view_keys(callback_query: types.CallbackQuery): buttons = [] for record in records: key_name = record["email"] - client_id = record["client_id"] button = types.InlineKeyboardButton( text=f"🔑 {key_name}", - callback_data=f"view_key|{key_name}|{client_id}", + callback_data=f"view_key|{key_name}", ) + buttons.append([button]) back_button = types.InlineKeyboardButton( @@ -137,10 +137,7 @@ async def process_callback_view_keys(callback_query: types.CallbackQuery): @router.callback_query(F.data.startswith("view_key|")) async def process_callback_view_key(callback_query: types.CallbackQuery): tg_id = callback_query.from_user.id - key_name, client_id = ( - callback_query.data.split("|")[1], - callback_query.data.split("|")[2], - ) + key_name = callback_query.data.split("|")[1] try: try: @@ -157,7 +154,7 @@ async def process_callback_view_key(callback_query: types.CallbackQuery): SELECT k.expiry_time, k.server_id, k.key FROM keys k WHERE k.tg_id = $1 AND k.email = $2 - """, + """, tg_id, key_name, ) @@ -200,14 +197,14 @@ async def process_callback_view_key(callback_query: types.CallbackQuery): ) connect_pc_button = types.InlineKeyboardButton( - text="💻 Windows/Linux", callback_data=f"connect_pc|{key}" + text="💻 Windows/Linux", callback_data=f"connect_pc|{key_name}" ) renew_button = types.InlineKeyboardButton( - text="⏳ Продлить", callback_data=f"renew_key|{client_id}" + text="⏳ Продлить", callback_data=f"renew_key|{key_name}" ) delete_button = types.InlineKeyboardButton( - text="❌ Удалить", callback_data=f"delete_key|{client_id}" + text="❌ Удалить", callback_data=f"delete_key|{key_name}" ) back_button = types.InlineKeyboardButton( text="🔙 Назад в профиль", callback_data="view_profile" @@ -223,7 +220,7 @@ async def process_callback_view_key(callback_query: types.CallbackQuery): if not key.startswith(PUBLIC_LINK): update_subscription_button = types.InlineKeyboardButton( text="🔄 Обновить подписку", - callback_data=f"update_subscription|{client_id}", + callback_data=f"update_subscription|{key_name}", ) inline_keyboard.append([update_subscription_button])