From 8f603ad51ce1fa0e678961803eb62be6dba51751 Mon Sep 17 00:00:00 2001 From: machka-pasla Date: Thu, 4 Sep 2025 20:20:27 +0300 Subject: [PATCH] Enhance subscription command handler with dynamic inline keyboard options - Added support for a mini-app connect button in the subscription command handler, allowing users to access additional features if the URL is configured. - Implemented an auto-renew toggle button and payment methods management button, improving user interaction based on subscription status and settings. - Refactored inline keyboard construction to prepend new buttons above the existing markup, enhancing the user experience in subscription management. --- bot/handlers/user/subscription/core.py | 38 +++++++++++++++++++------- bot/keyboards/inline/user_keyboards.py | 18 ++++-------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/bot/handlers/user/subscription/core.py b/bot/handlers/user/subscription/core.py index 32e6b64..cb84d1b 100644 --- a/bot/handlers/user/subscription/core.py +++ b/bot/handlers/user/subscription/core.py @@ -1,7 +1,7 @@ import logging from aiogram import Router, F, types, Bot from aiogram.filters import Command -from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup +from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup, WebAppInfo from typing import Optional, Union from datetime import datetime from sqlalchemy.ext.asyncio import AsyncSession @@ -151,20 +151,38 @@ async def my_subscription_command_handler( kb = base_markup.inline_keyboard try: local_sub = await subscription_dal.get_active_subscription_by_user_id(session, event.from_user.id) + # Build rows to prepend above the base "back" markup + prepend_rows = [] + + # 1) Mini-app connect button on top if enabled + if settings.SUBSCRIPTION_MINI_APP_URL: + prepend_rows.append([ + InlineKeyboardButton( + text=get_text("connect_button"), + web_app=WebAppInfo(url=settings.SUBSCRIPTION_MINI_APP_URL), + ) + ]) + + # 2) Auto-renew toggle (if supported and not tribute) if local_sub and local_sub.provider != "tribute" and getattr(settings, 'YOOKASSA_AUTOPAYMENTS_ENABLED', False): toggle_text = ( get_text("autorenew_disable_button") if local_sub.auto_renew_enabled else get_text("autorenew_enable_button") ) - kb = [ - [ - InlineKeyboardButton( - text=toggle_text, - callback_data=f"toggle_autorenew:{local_sub.subscription_id}:{1 if not local_sub.auto_renew_enabled else 0}", - ) - ] - ] + kb + prepend_rows.append([ + InlineKeyboardButton( + text=toggle_text, + callback_data=f"toggle_autorenew:{local_sub.subscription_id}:{1 if not local_sub.auto_renew_enabled else 0}", + ) + ]) + + # 3) Payment methods management (when autopayments enabled) if getattr(settings, 'YOOKASSA_AUTOPAYMENTS_ENABLED', False): - kb = [[InlineKeyboardButton(text=get_text("payment_methods_manage_button"), callback_data="pm:manage")]] + kb + prepend_rows.append([ + InlineKeyboardButton(text=get_text("payment_methods_manage_button"), callback_data="pm:manage") + ]) + + if prepend_rows: + kb = prepend_rows + kb except Exception: pass markup = InlineKeyboardMarkup(inline_keyboard=kb) diff --git a/bot/keyboards/inline/user_keyboards.py b/bot/keyboards/inline/user_keyboards.py index 4ed92b4..6fd2dae 100644 --- a/bot/keyboards/inline/user_keyboards.py +++ b/bot/keyboards/inline/user_keyboards.py @@ -21,20 +21,12 @@ def get_main_menu_inline_keyboard( builder.row( InlineKeyboardButton(text=_(key="menu_subscribe_inline"), callback_data="main_action:subscribe")) - if settings.SUBSCRIPTION_MINI_APP_URL: - builder.row( - InlineKeyboardButton( - text=_(key="menu_my_subscription_inline"), - web_app=WebAppInfo(url=settings.SUBSCRIPTION_MINI_APP_URL), - ) - ) - else: - builder.row( - InlineKeyboardButton( - text=_(key="menu_my_subscription_inline"), - callback_data="main_action:my_subscription", - ) + builder.row( + InlineKeyboardButton( + text=_(key="menu_my_subscription_inline"), + callback_data="main_action:my_subscription", ) + ) referral_button = InlineKeyboardButton( text=_(key="menu_referral_inline"),