Fixed unexpected behavior of the device selection button

This commit is contained in:
Vladless
2024-11-18 14:56:41 +03:00
parent 12b9b9c77e
commit e04ccd893f
2 changed files with 64 additions and 29 deletions
+56 -18
View File
@@ -4,9 +4,10 @@ from aiogram import F, Router, types
from aiogram.types import BufferedInputFile, InlineKeyboardButton, InlineKeyboardMarkup from aiogram.types import BufferedInputFile, InlineKeyboardButton, InlineKeyboardMarkup
from bot import bot 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 handlers.texts import INSTRUCTION_PC, INSTRUCTIONS, KEY_MESSAGE
from logger import logger from logger import logger
import asyncpg
router = Router() router = Router()
@@ -42,7 +43,7 @@ async def send_instructions(callback_query: types.CallbackQuery):
@router.callback_query(F.data.startswith("connect_pc|")) @router.callback_query(F.data.startswith("connect_pc|"))
async def process_connect_pc(callback_query: types.CallbackQuery): async def process_connect_pc(callback_query: types.CallbackQuery):
tg_id = callback_query.from_user.id tg_id = callback_query.from_user.id
key = callback_query.data.split("|")[1] key_name = callback_query.data.split("|")[1]
try: try:
await bot.delete_message( await bot.delete_message(
@@ -51,27 +52,64 @@ async def process_connect_pc(callback_query: types.CallbackQuery):
except Exception as e: except Exception as e:
logger.error(f"Ошибка при удалении сообщения: {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="<b>Ключ не найден. Проверьте имя ключа.</b>",
parse_mode="HTML",
)
return
connect_windows_button = types.InlineKeyboardButton( key = record["key"]
text="💻 Подключить Windows", url=f"{CONNECT_WINDOWS}{key}" key_message = KEY_MESSAGE.format(key)
) instruction_message = f"{key_message}{INSTRUCTION_PC}"
support_button = types.InlineKeyboardButton( connect_windows_button = types.InlineKeyboardButton(
text="🆘 Поддержка", url=f"{SUPPORT_CHAT_URL}" text="💻 Подключить Windows", url=f"{CONNECT_WINDOWS}{key}"
) )
back_button = types.InlineKeyboardButton( support_button = types.InlineKeyboardButton(
text="🔙 Назад в профиль", callback_data="view_profile" text="🆘 Поддержка", url=f"{SUPPORT_CHAT_URL}"
) )
inline_keyboard = [[connect_windows_button], [support_button], [back_button]] back_button = types.InlineKeyboardButton(
keyboard = types.InlineKeyboardMarkup(inline_keyboard=inline_keyboard) text="🔙 Назад в профиль", callback_data="view_profile"
)
await bot.send_message( inline_keyboard = [
tg_id, instruction_message, reply_markup=keyboard, parse_mode="HTML" [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="<b>Произошла ошибка. Пожалуйста, повторите попытку позже.</b>",
parse_mode="HTML",
)
await callback_query.answer() await callback_query.answer()
+8 -11
View File
@@ -38,11 +38,11 @@ async def process_callback_view_keys(callback_query: types.CallbackQuery):
buttons = [] buttons = []
for record in records: for record in records:
key_name = record["email"] key_name = record["email"]
client_id = record["client_id"]
button = types.InlineKeyboardButton( button = types.InlineKeyboardButton(
text=f"🔑 {key_name}", text=f"🔑 {key_name}",
callback_data=f"view_key|{key_name}|{client_id}", callback_data=f"view_key|{key_name}",
) )
buttons.append([button]) buttons.append([button])
back_button = types.InlineKeyboardButton( 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|")) @router.callback_query(F.data.startswith("view_key|"))
async def process_callback_view_key(callback_query: types.CallbackQuery): async def process_callback_view_key(callback_query: types.CallbackQuery):
tg_id = callback_query.from_user.id tg_id = callback_query.from_user.id
key_name, client_id = ( key_name = callback_query.data.split("|")[1]
callback_query.data.split("|")[1],
callback_query.data.split("|")[2],
)
try: try:
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 SELECT k.expiry_time, k.server_id, k.key
FROM keys k FROM keys k
WHERE k.tg_id = $1 AND k.email = $2 WHERE k.tg_id = $1 AND k.email = $2
""", """,
tg_id, tg_id,
key_name, key_name,
) )
@@ -200,14 +197,14 @@ async def process_callback_view_key(callback_query: types.CallbackQuery):
) )
connect_pc_button = types.InlineKeyboardButton( 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( renew_button = types.InlineKeyboardButton(
text="⏳ Продлить", callback_data=f"renew_key|{client_id}" text="⏳ Продлить", callback_data=f"renew_key|{key_name}"
) )
delete_button = types.InlineKeyboardButton( delete_button = types.InlineKeyboardButton(
text="❌ Удалить", callback_data=f"delete_key|{client_id}" text="❌ Удалить", callback_data=f"delete_key|{key_name}"
) )
back_button = types.InlineKeyboardButton( back_button = types.InlineKeyboardButton(
text="🔙 Назад в профиль", callback_data="view_profile" 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): if not key.startswith(PUBLIC_LINK):
update_subscription_button = types.InlineKeyboardButton( update_subscription_button = types.InlineKeyboardButton(
text="🔄 Обновить подписку", text="🔄 Обновить подписку",
callback_data=f"update_subscription|{client_id}", callback_data=f"update_subscription|{key_name}",
) )
inline_keyboard.append([update_subscription_button]) inline_keyboard.append([update_subscription_button])