shopbot/bot/dialogs/balance.py

124 lines
3.7 KiB
Python
Raw Permalink Normal View History

2024-12-25 01:30:33 +00:00
from typing import Any
2024-12-24 18:08:03 +00:00
from aiogram.types import Message
2024-12-25 01:30:33 +00:00
from aiogram_dialog import Dialog, DialogManager, Window
from aiogram_dialog.widgets.input import ManagedTextInput, TextInput
from aiogram_dialog.widgets.kbd import Button, Row, Start
from aiogram_dialog.widgets.text import Const, Format
2024-12-24 18:08:03 +00:00
2024-12-25 01:30:33 +00:00
from shopbot.bot.getters import getter_amount, getter_amount_cryptobot
from shopbot.bot.services import check_pay_crypto_bot, check_pay_lolz
from shopbot.bot.states import (
ProfileSG,
UpBalanceCryptoSG,
UpBalanceLolzSG,
UpBalanceSG,
)
from shopbot.bot.validations import error_number_handler
2024-12-24 18:08:03 +00:00
class UpBalanceWindow(Window):
def __init__(self, text: str, state: Any, on_error: Any, **kwargs):
super().__init__(
Const(text=text),
TextInput(
id="count",
type_factory=str,
on_success=self._on_success_callback,
on_error=on_error,
),
state=state,
2024-12-25 01:30:33 +00:00
**kwargs,
2024-12-24 18:08:03 +00:00
)
2024-12-25 01:30:33 +00:00
async def _on_success_callback(
self,
message: Message,
widget: ManagedTextInput,
dialog_manager: DialogManager,
data: str,
):
2024-12-24 18:08:03 +00:00
if not dialog_manager.current_context().dialog_data.get("error"):
await dialog_manager.next()
class PaymentWindow(Window):
def __init__(self, state: Any, check_pay_function: Any, getter: Any, **kwargs):
super().__init__(
2024-12-25 01:30:33 +00:00
Format(
text="{paylink}",
when=lambda data, widget, manager: not data.get("error"),
),
Format(
text="{error}",
when=lambda data, widget, manager: data.get("error") is not None,
),
2024-12-24 18:08:03 +00:00
Button(
Const("🔍 Проверить оплату"),
id="check_pay_button",
on_click=check_pay_function,
2024-12-25 01:30:33 +00:00
when=lambda data, widget, manager: not data.get("error")
and data.get("paylink") is not None,
2024-12-24 18:08:03 +00:00
),
Button(
Const("🔙 Отмена"),
id="cancel_button",
2024-12-25 01:30:33 +00:00
on_click=lambda callback, button, manager: manager.switch_to(
ProfileSG.profile
),
when=lambda data, widget, manager: data.get("error") is not None,
2024-12-24 18:08:03 +00:00
),
state=state,
getter=getter,
2024-12-25 01:30:33 +00:00
**kwargs,
2024-12-24 18:08:03 +00:00
)
up_balance_dialog = Dialog(
Window(
Const("Выберите через что хотите пополнить баланс:"),
Row(
Start(
Const("💸 Lolz"),
id="paymethod_lolz",
state=UpBalanceLolzSG.up_balance_lolz,
),
Start(
Const("💸 CryptoBot"),
id="paymethod_crypto",
state=UpBalanceCryptoSG.up_balance_crypto,
),
),
Start(Const("🔙 Назад"), id="back", state=ProfileSG.profile),
state=UpBalanceSG.up_balance,
)
)
up_balance_lolz_dialog = Dialog(
UpBalanceWindow(
text="💸 Введите сумму пополнения",
state=UpBalanceLolzSG.up_balance_lolz,
on_error=error_number_handler,
),
PaymentWindow(
state=UpBalanceLolzSG.pay_link,
check_pay_function=check_pay_lolz,
getter=getter_amount,
2024-12-25 01:30:33 +00:00
),
2024-12-24 18:08:03 +00:00
)
up_balance_cryptobot_dialog = Dialog(
UpBalanceWindow(
text="💸 Введите сумму пополнения",
state=UpBalanceCryptoSG.up_balance_crypto,
2024-12-25 01:30:33 +00:00
on_error=error_number_handler,
2024-12-24 18:08:03 +00:00
),
PaymentWindow(
state=UpBalanceCryptoSG.pay_link,
check_pay_function=check_pay_crypto_bot,
getter=getter_amount_cryptobot,
),
2024-12-25 01:30:33 +00:00
)