From 1b0a414ad606e09c5bccbd90ac0ba853e102802d Mon Sep 17 00:00:00 2001 From: Zakhar Izmaylov Date: Fri, 14 Feb 2025 23:20:40 +0300 Subject: [PATCH] Add inline referral query handler with customizable offers --- handlers/profile.py | 62 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/handlers/profile.py b/handlers/profile.py index db25f06e..9ccda06c 100644 --- a/handlers/profile.py +++ b/handlers/profile.py @@ -4,10 +4,27 @@ from typing import Any import aiofiles import asyncpg from aiogram import F, Router +from aiogram.enums import ParseMode from aiogram.fsm.context import FSMContext -from aiogram.types import BufferedInputFile, CallbackQuery, InlineKeyboardButton, Message +from aiogram.types import ( + BufferedInputFile, + CallbackQuery, + InlineKeyboardButton, + InlineQuery, + InlineQueryResultArticle, + InputTextMessageContent, + Message, +) from aiogram.utils.keyboard import InlineKeyboardBuilder -from config import DATABASE_URL, INSTRUCTIONS_BUTTON, NEWS_MESSAGE, RENEWAL_PLANS +from config import ( + DATABASE_URL, + INSTRUCTIONS_BUTTON, + NEWS_MESSAGE, + REFERRAL_OFFERS, + RENEWAL_PLANS, + TRIAL_TIME, + USERNAME_BOT, +) from database import get_balance, get_key_count, get_last_payments, get_referral_stats, get_trial from handlers.buttons.profile import ( @@ -205,7 +222,8 @@ async def invite_handler(callback_query: CallbackQuery): image_path = os.path.join("img", "pic_invite.jpg") builder = InlineKeyboardBuilder() - builder.button(text="📢 Поделиться", switch_inline_query=invite_text) + # builder.button(text="📢 Поделиться", switch_inline_query=invite_text) + builder.button(text="👥 Пригласить друга", switch_inline_query="invite ") builder.button(text="👤 Личный кабинет", callback_data="profile") builder.adjust(1) @@ -222,3 +240,41 @@ async def invite_handler(callback_query: CallbackQuery): text=invite_message, reply_markup=builder.as_markup(), ) + + +@router.inline_query(F.query.in_(["referral", "ref", "invite"])) +async def inline_referral_handler(inline_query: InlineQuery): + try: + if not inline_query.from_user: + return await inline_query.answer([]) + + if not USERNAME_BOT: + return await inline_query.answer([]) + + referral_link = f"https://t.me/{USERNAME_BOT}?start=referral_{inline_query.from_user.id}" + + if not inline_query.query: + return await inline_query.answer([]) + + results: list[InlineQueryResultArticle] = [] + + for index, offer in enumerate(REFERRAL_OFFERS): + description = offer["description"][:64] + message_text = offer["message"].format(trial_time=TRIAL_TIME)[:4096] + + builder = InlineKeyboardBuilder() + builder.row(InlineKeyboardButton(text=offer["title"], url=referral_link)) + + results.append( + InlineQueryResultArticle( + id=str(index), + title=offer["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=results, cache_time=1) + except Exception: + await inline_query.answer([])