add profile dialog
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from aiogram import Bot, Dispatcher
|
from aiogram import Bot, Dispatcher
|
||||||
|
from aiogram.client.default import DefaultBotProperties
|
||||||
|
from aiogram.enums import ParseMode
|
||||||
from aiogram_dialog import setup_dialogs
|
from aiogram_dialog import setup_dialogs
|
||||||
|
|
||||||
from app.bot.dialogs.flows import dialogs_router
|
from app.bot.dialogs.flows import dialogs_router
|
||||||
from app.bot.handlers.commands import commands_router
|
from app.bot.handlers.commands import commands_router
|
||||||
|
from app.bot.middlewares import GetUserMiddleware
|
||||||
from config.config import settings
|
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()
|
dp = Dispatcher()
|
||||||
|
|
||||||
|
|
||||||
@@ -15,6 +18,8 @@ async def main():
|
|||||||
setup_dialogs(dp)
|
setup_dialogs(dp)
|
||||||
dp.include_router(commands_router)
|
dp.include_router(commands_router)
|
||||||
dp.include_router(dialogs_router)
|
dp.include_router(dialogs_router)
|
||||||
|
|
||||||
|
dp.update.outer_middleware(GetUserMiddleware())
|
||||||
await dp.start_polling(bot)
|
await dp.start_polling(bot)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
14
app/bot/dialogs/flows/admin/start/dialogs.py
Normal file
14
app/bot/dialogs/flows/admin/start/dialogs.py
Normal 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,
|
||||||
|
)
|
||||||
|
)
|
||||||
5
app/bot/dialogs/flows/admin/start/states.py
Normal file
5
app/bot/dialogs/flows/admin/start/states.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from aiogram.fsm.state import State, StatesGroup
|
||||||
|
|
||||||
|
|
||||||
|
class AdminStartSG(StatesGroup):
|
||||||
|
start = State()
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
from aiogram import Router
|
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 = Router(name="user dialogs")
|
||||||
user_router.include_router(start_dialog)
|
user_router.include_router(user_start_dialog)
|
||||||
|
user_router.include_router(user_profile_dialog)
|
||||||
|
|||||||
38
app/bot/dialogs/flows/user/profile/dialogs.py
Normal file
38
app/bot/dialogs/flows/user/profile/dialogs.py
Normal 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,
|
||||||
|
),
|
||||||
|
)
|
||||||
9
app/bot/dialogs/flows/user/profile/states.py
Normal file
9
app/bot/dialogs/flows/user/profile/states.py
Normal 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()
|
||||||
@@ -1,14 +1,18 @@
|
|||||||
from aiogram_dialog import Dialog, Window
|
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 app.bot.dialogs.widgets.getters import username_getter
|
||||||
|
|
||||||
from .states import StartSG
|
from .states import UserStartSG
|
||||||
|
|
||||||
start_dialog = Dialog(
|
user_start_dialog = Dialog(
|
||||||
Window(
|
Window(
|
||||||
Format("Hello, {username}"),
|
Format("Hello, {username}"),
|
||||||
|
Button(Const("События"), id="events"),
|
||||||
|
Start(Const("Профиль"), id="profile", state=UserProfileSG.profile),
|
||||||
getter=username_getter,
|
getter=username_getter,
|
||||||
state=StartSG.start,
|
state=UserStartSG.start,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from aiogram.fsm.state import State, StatesGroup
|
from aiogram.fsm.state import State, StatesGroup
|
||||||
|
|
||||||
|
|
||||||
class StartSG(StatesGroup):
|
class UserStartSG(StatesGroup):
|
||||||
start = State()
|
start = State()
|
||||||
@@ -1,12 +1,19 @@
|
|||||||
from aiogram import Router
|
from aiogram import Router
|
||||||
from aiogram.filters import CommandStart
|
from aiogram.filters import CommandStart
|
||||||
from aiogram.types import Message
|
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 = Router(name="commands_router")
|
||||||
|
|
||||||
|
|
||||||
@commands_router.message(CommandStart())
|
@commands_router.message(CommandStart())
|
||||||
async def command_start_process(message: Message, dialog_manager: DialogManager):
|
async def command_start_process(
|
||||||
await dialog_manager.start(state=StartSG.start)
|
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)
|
||||||
|
|||||||
3
app/bot/middlewares/__init__.py
Normal file
3
app/bot/middlewares/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from .get_user import GetUserMiddleware
|
||||||
|
|
||||||
|
__all__ = ["GetUserMiddleware"]
|
||||||
15
app/bot/middlewares/get_user.py
Normal file
15
app/bot/middlewares/get_user.py
Normal 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)
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
from config import DATABASE_URL
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||||
from sqlalchemy.orm import declarative_base, sessionmaker
|
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||||
|
|
||||||
|
from config.config import DATABASE_URL
|
||||||
|
|
||||||
engine = create_async_engine(DATABASE_URL, echo=True)
|
engine = create_async_engine(DATABASE_URL, echo=True)
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
async_session = sessionmaker(bind=engine, expire_on_commit=False, class_=AsyncSession)
|
async_session = sessionmaker(bind=engine, expire_on_commit=False, class_=AsyncSession)
|
||||||
|
|||||||
Reference in New Issue
Block a user