73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
import logging
|
|
|
|
from aiogram import Router, F
|
|
from aiogram.types import (
|
|
ChosenInlineResult,
|
|
InlineQuery,
|
|
InlineQueryResultArticle,
|
|
InputTextMessageContent,
|
|
)
|
|
from aiogram.utils.deep_linking import create_start_link
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from misc.filters import IsVerified
|
|
from misc.kb.admins import payment_link
|
|
from misc.kb.common import placeholder_kb
|
|
from misc.utils import b64_to_dict, dict_to_b64
|
|
from repositories.invoices import InvoiceRepository
|
|
|
|
router = Router()
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@router.inline_query(IsVerified(), F.query.cast(int).as_("amount"))
|
|
async def send_invoice(iq: InlineQuery, amount: int):
|
|
results = [
|
|
InlineQueryResultArticle(
|
|
id=dict_to_b64({"a": amount}),
|
|
title=f"💸 Счёт на {amount}₽",
|
|
input_message_content=InputTextMessageContent(
|
|
message_text="⏳ Создаю счёт..."
|
|
),
|
|
reply_markup=placeholder_kb,
|
|
description="Нажмите, чтобы создать счёт.",
|
|
),
|
|
]
|
|
await iq.answer(results, is_personal=True)
|
|
|
|
|
|
@router.chosen_inline_result()
|
|
async def chosen_inline(
|
|
ev: ChosenInlineResult, session: AsyncSession, invoice_repo: InvoiceRepository
|
|
):
|
|
data = b64_to_dict(ev.result_id)
|
|
amount = data.get("a")
|
|
if not amount:
|
|
await ev.bot.edit_message_text(
|
|
inline_message_id=ev.inline_message_id, text="🍃 Что-то пошло не так..."
|
|
)
|
|
logger.critical("amount is null during the invoice creation.")
|
|
return
|
|
|
|
try:
|
|
amount = int(amount)
|
|
except Exception as e:
|
|
await ev.bot.edit_message_text(
|
|
inline_message_id=ev.inline_message_id, text="🍃 Что-то пошло не так..."
|
|
)
|
|
logger.exception(e)
|
|
return
|
|
|
|
invoice = await invoice_repo.create_invoice(
|
|
session,
|
|
amount=amount,
|
|
creator_id=ev.from_user.id,
|
|
inline_message_id=ev.inline_message_id,
|
|
)
|
|
invoice_link = await create_start_link(ev.bot, f"invoice:{invoice.id}", encode=True)
|
|
await ev.bot.edit_message_text(
|
|
inline_message_id=ev.inline_message_id,
|
|
text=f"<b>💸 Счёт на {amount}₽.</b>",
|
|
reply_markup=payment_link(invoice_link),
|
|
)
|