Add webhook support for modules / Implement hooks for payment keyboard

This commit is contained in:
Capybara-z
2025-08-04 20:31:28 +03:00
parent 35ef17660c
commit a833b11b8e
3 changed files with 42 additions and 0 deletions
+5
View File
@@ -53,6 +53,8 @@ from handlers.payments.wata import process_callback_pay_wata
from handlers.payments.yookassa_pay import process_callback_pay_yookassa
from handlers.payments.yoomoney_pay import process_callback_pay_yoomoney
from handlers.texts import BALANCE_MANAGEMENT_TEXT, PAYMENT_METHODS_MSG
from hooks.hook_buttons import insert_hook_buttons
from hooks.hooks import run_hooks
from .utils import edit_or_send_message
@@ -115,6 +117,9 @@ async def handle_pay(callback_query: CallbackQuery, state: FSMContext, session:
if DONATIONS_ENABLE:
builder.row(InlineKeyboardButton(text="💰 Поддержать проект", callback_data="donate"))
module_buttons = await run_hooks("pay_menu_buttons", chat_id=callback_query.from_user.id, admin=False, session=session)
builder = insert_hook_buttons(builder, module_buttons)
builder.row(InlineKeyboardButton(text=MAIN_MENU, callback_data="profile"))
await edit_or_send_message(
+24
View File
@@ -28,3 +28,27 @@ def load_modules_from_folder(folder: str = "modules") -> list[Router]:
except Exception as e:
logger.error(f"[Modules] Ошибка при загрузке {module_path}: {e}")
return routers
def load_module_webhooks(folder: str = "modules") -> list[dict]:
webhooks = []
base_path = Path(folder)
if not base_path.exists():
logger.warning(f"[Modules] Папка {folder} не найдена, пропускаем загрузку вебхуков.")
return []
for _finder, name, _ispkg in pkgutil.iter_modules([str(base_path)]):
module_path = f"{folder}.{name}"
try:
router_module = importlib.import_module(f"{module_path}.router")
if hasattr(router_module, "get_webhook_data"):
webhook_data = router_module.get_webhook_data()
if isinstance(webhook_data, dict) and "path" in webhook_data and "handler" in webhook_data:
webhooks.append(webhook_data)
logger.info(f"[Modules] Найден вебхук в модуле {name}: {webhook_data['path']}")
except Exception as e:
logger.error(f"[Modules] Ошибка при загрузке вебхуков из {module_path}: {e}")
return webhooks
+13
View File
@@ -6,6 +6,7 @@ from .heleket_payment import heleket_payment_webhook
from .kassai_payment import kassai_payment_webhook
from .tblocker import tblocker_webhook
from .wata_payment import wata_payment_webhook
from utils.modules_loader import load_module_webhooks
WATA_WEBHOOK_PATH = "/wata/webhook"
@@ -18,3 +19,15 @@ async def register_web_routes(router: UrlDispatcher) -> None:
router.add_post(WATA_WEBHOOK_PATH, wata_payment_webhook)
router.add_post(KASSAI_WEBHOOK_PATH, kassai_payment_webhook)
router.add_post(HELEKET_WEBHOOK_PATH, heleket_payment_webhook)
try:
module_webhooks = load_module_webhooks()
for webhook_data in module_webhooks:
path = webhook_data.get("path")
handler = webhook_data.get("handler")
if path and handler:
router.add_post(path, handler)
print(f"[Web] Зарегистрирован вебхук модуля: {path}")
except Exception as e:
print(f"[Web] Ошибка при загрузке вебхуков модулей: {e}")