add profile dialog

This commit is contained in:
2025-09-25 20:48:39 +03:00
parent 8c801eb66e
commit cbe1919343
12 changed files with 117 additions and 14 deletions

View File

@@ -1,13 +1,16 @@
import asyncio
from aiogram import Bot, Dispatcher
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram_dialog import setup_dialogs
from app.bot.dialogs.flows import dialogs_router
from app.bot.handlers.commands import commands_router
from app.bot.middlewares import GetUserMiddleware
from config.config import settings
bot = Bot(token=settings.bot_token)
bot = Bot(token=settings.bot_token, default=DefaultBotProperties(parse_mode=ParseMode.MARKDOWN_V2))
dp = Dispatcher()
@@ -15,6 +18,8 @@ async def main():
setup_dialogs(dp)
dp.include_router(commands_router)
dp.include_router(dialogs_router)
dp.update.outer_middleware(GetUserMiddleware())
await dp.start_polling(bot)

View File

@@ -0,0 +1,14 @@
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.text import Format
from app.bot.dialogs.widgets.getters import username_getter
from .states import AdminStartSG
admin_start_dialog = Dialog(
Window(
Format("Hello, {username}"),
getter=username_getter,
state=AdminStartSG.start,
)
)

View File

@@ -0,0 +1,5 @@
from aiogram.fsm.state import State, StatesGroup
class AdminStartSG(StatesGroup):
start = State()

View File

@@ -1,6 +1,8 @@
from aiogram import Router
from .start.dialogs import start_dialog
from .profile.dialogs import user_profile_dialog
from .start.dialogs import user_start_dialog
user_router = Router(name="user")
user_router.include_router(start_dialog)
user_router = Router(name="user dialogs")
user_router.include_router(user_start_dialog)
user_router.include_router(user_profile_dialog)

View File

@@ -0,0 +1,38 @@
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.input import TextInput
from aiogram_dialog.widgets.kbd import Back, Cancel, SwitchTo
from aiogram_dialog.widgets.text import Const, Format
from .states import UserProfileSG
user_profile_dialog = Dialog(
Window(
Const("*Профиль*"),
Format("Имя:"),
Format("Телефон:"),
SwitchTo(
Const("изменить имя"),
id="change_name",
state=UserProfileSG.change_name,
),
SwitchTo(
Const("изменить телефон"),
id="change_phone",
state=UserProfileSG.change_phone,
),
Cancel(Const("назад")),
state=UserProfileSG.profile,
),
Window(
Const("Введите имя"),
SwitchTo(Const("отмена"), id="go_profile", state=UserProfileSG.profile),
TextInput(id="name_input"),
state=UserProfileSG.change_name,
),
Window(
Const("Введите телефон"),
SwitchTo(Const("отмена"), id="go_profile", state=UserProfileSG.profile),
TextInput(id="phone_input"),
state=UserProfileSG.change_phone,
),
)

View File

@@ -0,0 +1,9 @@
from aiogram.fsm.state import State, StatesGroup
class UserProfileSG(StatesGroup):
profile = State()
change_name = State()
input_name = State()
change_phone = State()
change_phone = State()

View File

@@ -1,14 +1,18 @@
from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.text import Format
from aiogram_dialog.widgets.kbd import Button, Start
from aiogram_dialog.widgets.text import Const, Format
from app.bot.dialogs.flows.user.profile.dialogs import UserProfileSG
from app.bot.dialogs.widgets.getters import username_getter
from .states import StartSG
from .states import UserStartSG
start_dialog = Dialog(
user_start_dialog = Dialog(
Window(
Format("Hello, {username}"),
Button(Const("События"), id="events"),
Start(Const("Профиль"), id="profile", state=UserProfileSG.profile),
getter=username_getter,
state=StartSG.start,
state=UserStartSG.start,
)
)

View File

@@ -1,5 +1,5 @@
from aiogram.fsm.state import State, StatesGroup
class StartSG(StatesGroup):
class UserStartSG(StatesGroup):
start = State()

View File

@@ -1,12 +1,19 @@
from aiogram import Router
from aiogram.filters import CommandStart
from aiogram.types import Message
from aiogram_dialog import DialogManager
from aiogram_dialog import DialogManager, StartMode
from app.bot.dialogs.flows.user.start.states import StartSG
from app.bot.dialogs.flows.admin.start.states import AdminStartSG
from app.bot.dialogs.flows.user.start.states import UserStartSG
commands_router = Router(name="commands_router")
@commands_router.message(CommandStart())
async def command_start_process(message: Message, dialog_manager: DialogManager):
await dialog_manager.start(state=StartSG.start)
async def command_start_process(
message: Message, dialog_manager: DialogManager, user: dict
):
if user["is_admin"]:
await dialog_manager.start(state=AdminStartSG.start, mode=StartMode.RESET_STACK)
else:
await dialog_manager.start(state=UserStartSG.start, mode=StartMode.RESET_STACK)

View File

@@ -0,0 +1,3 @@
from .get_user import GetUserMiddleware
__all__ = ["GetUserMiddleware"]

View File

@@ -0,0 +1,15 @@
from typing import Any, Awaitable, Callable
from aiogram import BaseMiddleware
from aiogram.types import Update
class GetUserMiddleware(BaseMiddleware):
async def __call__(
self,
handler: Callable[[Update, dict[str, Any]], Awaitable[Any]],
event: Update,
data: dict[str, Any],
):
data.update(user={"is_admin": False})
return await handler(event, data)

View File

@@ -1,7 +1,8 @@
from config import DATABASE_URL
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from config.config import DATABASE_URL
engine = create_async_engine(DATABASE_URL, echo=True)
Base = declarative_base()
async_session = sessionmaker(bind=engine, expire_on_commit=False, class_=AsyncSession)