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.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from aiogram import Router, F, types, Bot
|
from aiogram import Router, F, types, Bot
|
||||||
from aiogram.filters import Command
|
from aiogram.filters import Command
|
||||||
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
|
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup, WebAppInfo
|
||||||
from typing import Optional, Union
|
from typing import Optional, Union
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
@@ -151,20 +151,38 @@ async def my_subscription_command_handler(
|
|||||||
kb = base_markup.inline_keyboard
|
kb = base_markup.inline_keyboard
|
||||||
try:
|
try:
|
||||||
local_sub = await subscription_dal.get_active_subscription_by_user_id(session, event.from_user.id)
|
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):
|
if local_sub and local_sub.provider != "tribute" and getattr(settings, 'YOOKASSA_AUTOPAYMENTS_ENABLED', False):
|
||||||
toggle_text = (
|
toggle_text = (
|
||||||
get_text("autorenew_disable_button") if local_sub.auto_renew_enabled else get_text("autorenew_enable_button")
|
get_text("autorenew_disable_button") if local_sub.auto_renew_enabled else get_text("autorenew_enable_button")
|
||||||
)
|
)
|
||||||
kb = [
|
prepend_rows.append([
|
||||||
[
|
InlineKeyboardButton(
|
||||||
InlineKeyboardButton(
|
text=toggle_text,
|
||||||
text=toggle_text,
|
callback_data=f"toggle_autorenew:{local_sub.subscription_id}:{1 if not local_sub.auto_renew_enabled else 0}",
|
||||||
callback_data=f"toggle_autorenew:{local_sub.subscription_id}:{1 if not local_sub.auto_renew_enabled else 0}",
|
)
|
||||||
)
|
])
|
||||||
]
|
|
||||||
] + kb
|
# 3) Payment methods management (when autopayments enabled)
|
||||||
if getattr(settings, 'YOOKASSA_AUTOPAYMENTS_ENABLED', False):
|
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:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
markup = InlineKeyboardMarkup(inline_keyboard=kb)
|
markup = InlineKeyboardMarkup(inline_keyboard=kb)
|
||||||
|
|||||||
@@ -21,20 +21,12 @@ def get_main_menu_inline_keyboard(
|
|||||||
builder.row(
|
builder.row(
|
||||||
InlineKeyboardButton(text=_(key="menu_subscribe_inline"),
|
InlineKeyboardButton(text=_(key="menu_subscribe_inline"),
|
||||||
callback_data="main_action:subscribe"))
|
callback_data="main_action:subscribe"))
|
||||||
if settings.SUBSCRIPTION_MINI_APP_URL:
|
builder.row(
|
||||||
builder.row(
|
InlineKeyboardButton(
|
||||||
InlineKeyboardButton(
|
text=_(key="menu_my_subscription_inline"),
|
||||||
text=_(key="menu_my_subscription_inline"),
|
callback_data="main_action:my_subscription",
|
||||||
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",
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
referral_button = InlineKeyboardButton(
|
referral_button = InlineKeyboardButton(
|
||||||
text=_(key="menu_referral_inline"),
|
text=_(key="menu_referral_inline"),
|
||||||
|
|||||||
Reference in New Issue
Block a user