Files
FascioSchoolBot/app/bot/dialogs/flows/profile/handlers.py
2025-10-03 14:09:43 +03:00

53 lines
1.4 KiB
Python

import re
from aiogram.types import Message
from aiogram_dialog import DialogManager
from aiogram_dialog.widgets.input import ManagedTextInput
from app.infrastructure.database.crud import update_user
from .states import ProfileSG
def name_type_factory(text: str):
if not 2 < len(text) < 20:
raise ValueError("Имя должно быть от 2 до 20 символов")
return text
def phone_type_factory(text: str):
if len(re.findall("\d", text)) < 11:
raise ValueError("Телефон должен быть в формате +79991112233")
return text
async def on_error(
message: Message,
widget: ManagedTextInput,
dialog_manager: DialogManager,
error: ValueError,
):
await message.answer(error.args[0])
async def update_user_name(
message: Message,
widget: ManagedTextInput,
dialog_manager: DialogManager,
text: str,
):
session = dialog_manager.middleware_data["session"]
await update_user(session, message.from_user.id, fullname=text.strip())
await dialog_manager.switch_to(ProfileSG.profile)
async def update_user_phone(
message: Message,
widget: ManagedTextInput,
dialog_manager: DialogManager,
text: str,
):
session = dialog_manager.middleware_data["session"]
print(text)
text = ''.join(re.findall(r"\d", text))
await update_user(session, message.from_user.id, phone=text)
await dialog_manager.switch_to(ProfileSG.profile)