From 86e678de08d724604ef59cd8472cb7e74fb3c742 Mon Sep 17 00:00:00 2001 From: Capybara-z Date: Wed, 2 Apr 2025 23:07:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE?= =?UTF-8?q?=D1=81=D1=82=D1=8C=20=D0=B4=D0=B5=D0=BB=D0=B8=D1=82=D1=8C=D1=81?= =?UTF-8?q?=D1=8F=20=D0=BA=D1=83=D0=BF=D0=BE=D0=BD=D0=BE=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Добалена возможность делиться купоном при инлайн режиме. --- handlers/admin/coupons/coupons_handler.py | 84 ++++++++++++++++++----- 1 file changed, 68 insertions(+), 16 deletions(-) diff --git a/handlers/admin/coupons/coupons_handler.py b/handlers/admin/coupons/coupons_handler.py index 2b5e276e..f48b1148 100644 --- a/handlers/admin/coupons/coupons_handler.py +++ b/handlers/admin/coupons/coupons_handler.py @@ -3,9 +3,11 @@ from typing import Any from aiogram import F, Router from aiogram.fsm.context import FSMContext from aiogram.fsm.state import State, StatesGroup -from aiogram.types import CallbackQuery, Message +from aiogram.types import CallbackQuery, Message, InlineQuery, InlineQueryResultArticle, InputTextMessageContent +from aiogram.enums import ParseMode +from aiogram.utils.keyboard import InlineKeyboardBuilder -from config import USERNAME_BOT +from config import USERNAME_BOT, INLINE_MODE from database import create_coupon, delete_coupon, get_all_coupons from filters.admin import IsAdminFilter from logger import logger @@ -54,7 +56,8 @@ async def handle_coupon_data_input(message: Message, state: FSMContext, session: text = message.text.strip() parts = text.split() - kb = build_admin_back_kb("coupons") + kb = InlineKeyboardBuilder() + kb.button(text="Назад", callback_data=AdminPanelCallback(action="coupons").pack()) if len(parts) != 3: text = ( @@ -62,11 +65,7 @@ async def handle_coupon_data_input(message: Message, state: FSMContext, session: "🏷️ код 💰 сумма 🔢 лимит\n" "Пример: 'COUPON1 50 5' 👈" ) - - await message.answer( - text=text, - reply_markup=kb, - ) + await message.answer(text=text, reply_markup=kb.as_markup()) return try: @@ -75,28 +74,32 @@ async def handle_coupon_data_input(message: Message, state: FSMContext, session: usage_limit = int(parts[2]) except ValueError: text = "⚠️ Проверьте правильность введенных данных!\n💱 Сумма должна быть числом, а лимит — целым числом." - - await message.answer( - text=text, - reply_markup=kb, - ) + await message.answer(text=text, reply_markup=kb.as_markup()) return try: await create_coupon(coupon_code, coupon_amount, usage_limit, session) + coupon_link = f"https://t.me/{USERNAME_BOT}?start=coupons_{coupon_code}" text = ( f"✅ Купон с кодом {coupon_code} успешно создан!\n" - f"💰 Сумма: {coupon_amount} рублей \n" + f"💰 Сумма: {coupon_amount} рублей\n" f"🔢 Лимит использования: {usage_limit} раз\n" - f"🔗 Ссылка: https://t.me/{USERNAME_BOT}?start=coupons_{coupon_code}\n" + f"🔗 Ссылка: {coupon_link}\n" ) - await message.answer(text=text, reply_markup=kb) + kb = InlineKeyboardBuilder() + if INLINE_MODE: + kb.button(text="📤 Поделиться", switch_inline_query=f"coupon_{coupon_code}") + kb.button(text="Назад", callback_data=AdminPanelCallback(action="coupons").pack()) + kb.adjust(1) + + await message.answer(text=text, reply_markup=kb.as_markup()) await state.clear() except Exception as e: logger.error(f"Ошибка при создании купона: {e}") + await message.answer("❌ Произошла ошибка при создании купона.", reply_markup=kb.as_markup()) @router.callback_query( @@ -172,3 +175,52 @@ async def update_coupons_list(message, session: Any, page: int = 1): f"🔗 Ссылка: https://t.me/{USERNAME_BOT}?start=coupons_{coupon['code']}\n" ) await message.edit_text(text=coupon_list, reply_markup=kb) + + +@router.inline_query(F.query.startswith("coupon_")) +async def inline_coupon_handler(inline_query: InlineQuery, session: Any): + if not INLINE_MODE: + return + + coupon_code = inline_query.query.split("coupon_")[1] + coupon_link = f"https://t.me/{USERNAME_BOT}?start=coupons_{coupon_code}" + + coupons = await get_all_coupons(session, page=1, per_page=10) + coupon = next((c for c in coupons["coupons"] if c["code"] == coupon_code), None) + + if not coupon: + await inline_query.answer( + results=[], + switch_pm_text="Купон не найден", + switch_pm_parameter="coupons", + cache_time=1, + ) + return + + title = f"Купон {coupon['code']}" + description = f"Получи {coupon['amount']} рублей!" + message_text = ( + f"🎫 Купон: {coupon['code']}\n" + f"💰 Бонус: {coupon['amount']} рублей\n" + f"👇 Нажми, чтобы активировать!" + ) + + builder = InlineKeyboardBuilder() + builder.button(text="Активировать купон", url=coupon_link) + + result = InlineQueryResultArticle( + id=coupon_code, + title=title, + description=description, + input_message_content=InputTextMessageContent( + message_text=message_text, + parse_mode=ParseMode.HTML + ), + reply_markup=builder.as_markup(), + ) + + await inline_query.answer( + results=[result], + cache_time=86400, + is_personal=True + )