Refactor sender button parsing / Add tariff hooks

This commit is contained in:
Capybara-z
2025-12-03 02:26:17 +03:00
parent 48ebdeca0f
commit 26da97187c
4 changed files with 39 additions and 9 deletions
+6 -9
View File
@@ -93,16 +93,12 @@ def strip_html_tags(text: str) -> str:
def parse_message_buttons(text: str) -> tuple[str, InlineKeyboardMarkup | None]:
buttons_match = re.search(r"(<[^>]+>)?\s*BUTTONS\s*:\s*(</[^>]+>)?", text, re.IGNORECASE)
if not buttons_match:
if "BUTTONS:" not in text:
return text, None
clean_text = text[: buttons_match.start()].strip()
buttons_section = text[buttons_match.start() :].strip()
buttons_text = strip_html_tags(buttons_section)
buttons_text = re.sub(r"^.*?BUTTONS\s*:\s*", "", buttons_text, flags=re.IGNORECASE).strip()
parts = text.split("BUTTONS:", 1)
clean_text = parts[0].strip()
buttons_text = parts[1].strip()
if not buttons_text:
return clean_text, None
@@ -112,7 +108,8 @@ def parse_message_buttons(text: str) -> tuple[str, InlineKeyboardMarkup | None]:
for line in button_lines:
try:
button_data = json.loads(line)
cleaned_line = re.sub(r'<tg-emoji emoji-id="[^"]*">([^<]*)</tg-emoji>', r"\1", line)
button_data = json.loads(cleaned_line)
if not isinstance(button_data, dict) or "text" not in button_data:
logger.warning(f"[Sender] Неверный формат кнопки: {line}")
@@ -31,6 +31,8 @@ from handlers.texts import (
UNLIMITED_TRAFFIC_LABEL,
)
from handlers.utils import edit_or_send_message
from hooks.hook_buttons import insert_hook_buttons
from hooks.processors import process_addons_menu
from logger import logger
from ..buy.key_tariffs import calculate_config_price
@@ -294,6 +296,9 @@ async def render_addons_screen(callback: CallbackQuery, state: FSMContext, sessi
builder.row(InlineKeyboardButton(text=BACK, callback_data=f"view_key|{email}"))
module_buttons = await process_addons_menu(email=email, session=session)
builder = insert_hook_buttons(builder, module_buttons)
await edit_or_send_message(
target_message=callback.message,
text=text,
@@ -28,6 +28,8 @@ from handlers.texts import (
UNLIMITED_TRAFFIC_LABEL,
)
from handlers.utils import edit_or_send_message
from hooks.hook_buttons import insert_hook_buttons
from hooks.processors import process_addons_menu
from logger import logger
from ..buy.key_tariffs import calculate_config_price
@@ -302,6 +304,9 @@ async def render_addons_screen(callback: CallbackQuery, state: FSMContext, sessi
)
builder.row(InlineKeyboardButton(text=BACK, callback_data=f"view_key|{email}"))
module_buttons = await process_addons_menu(email=email, session=session)
builder = insert_hook_buttons(builder, module_buttons)
await edit_or_send_message(
target_message=callback.message,
text=text,
+23
View File
@@ -570,6 +570,29 @@ async def process_check_discount_validity(
return None
async def process_addons_menu(
email: str,
session: Any,
**kwargs,
) -> list:
"""
Обрабатывает хук addons_menu.
Возвращает список операций для модификации кнопок в меню конфигуратора тарифов.
"""
try:
results = await run_hooks(
"addons_menu",
email=email,
session=session,
**kwargs,
)
return results if results else []
except Exception as e:
logger.warning(f"[ADDONS_MENU] Ошибка при обработке хука: {e}")
return []
async def process_connect_device_menu(
chat_id: int,
session: Any,