from typing import Any from aiogram.types import Message 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 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 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, **kwargs, ) async def _on_success_callback( self, message: Message, widget: ManagedTextInput, dialog_manager: DialogManager, data: str, ): 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__( 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, ), Button( Const("🔍 Проверить оплату"), id="check_pay_button", on_click=check_pay_function, when=lambda data, widget, manager: not data.get("error") and data.get("paylink") is not None, ), Button( Const("🔙 Отмена"), id="cancel_button", on_click=lambda callback, button, manager: manager.switch_to( ProfileSG.profile ), when=lambda data, widget, manager: data.get("error") is not None, ), state=state, getter=getter, **kwargs, ) 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, ), ) up_balance_cryptobot_dialog = Dialog( UpBalanceWindow( text="💸 Введите сумму пополнения", state=UpBalanceCryptoSG.up_balance_crypto, on_error=error_number_handler, ), PaymentWindow( state=UpBalanceCryptoSG.pay_link, check_pay_function=check_pay_crypto_bot, getter=getter_amount_cryptobot, ), )