-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dialog for creating a room
- Loading branch information
Showing
9 changed files
with
179 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from src.bot.dialogs.roomless import roomless_dialog | ||
|
||
__all__ = ["roomless_dialog"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class CreateRoomDialogResult: | ||
name: str | None | ||
ok: bool = True | ||
|
||
|
||
@dataclass | ||
class RoomDialogStartData: | ||
id: int | ||
name: str | ||
|
||
|
||
__all__ = ["CreateRoomDialogResult", "RoomDialogStartData"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from aiogram.types import Message | ||
from aiogram_dialog import Dialog, Window, DialogManager, ShowMode | ||
from aiogram_dialog.widgets.input import TextInput | ||
from aiogram_dialog.widgets.kbd import Cancel | ||
from aiogram_dialog.widgets.text import Const | ||
|
||
from src.bot.dialogs.communication import CreateRoomDialogResult | ||
from src.bot.dialogs.states import CreateRoomSG | ||
|
||
|
||
NAME_INPUT_ID = "name_input" | ||
|
||
|
||
async def text_handler(message: Message, widget, dialog_manager: DialogManager, _): | ||
await dialog_manager.done(CreateRoomDialogResult(name=message.text), show_mode=ShowMode.SEND) | ||
|
||
|
||
create_room_dialog = Dialog( | ||
Window( | ||
Const("Enter a room's name"), | ||
Cancel(result=CreateRoomDialogResult(name=None, ok=False)), | ||
TextInput(NAME_INPUT_ID, on_success=text_handler), | ||
state=CreateRoomSG.enter_name, | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from aiogram_dialog import Dialog, Window, DialogManager | ||
from aiogram_dialog.widgets.text import Format | ||
|
||
from src.bot.dialogs.communication import RoomDialogStartData | ||
from src.bot.dialogs.states import RoomSG | ||
|
||
|
||
async def getter(dialog_manager: DialogManager): | ||
room_info: RoomDialogStartData = dialog_manager.dialog_data["room_info"] | ||
return { | ||
"room_id": room_info.id, | ||
"room_name": room_info.name, | ||
} | ||
|
||
|
||
async def on_start(start_data: RoomDialogStartData, dialog_manager: DialogManager): | ||
dialog_manager.dialog_data["room_info"] = start_data | ||
|
||
|
||
room_dialog = Dialog( | ||
Window( | ||
Format("Your room: {room_name}\nID: {room_id}"), | ||
getter=getter, | ||
state=RoomSG.main, | ||
), | ||
on_start=on_start, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from aiogram.types import CallbackQuery | ||
from aiogram_dialog import Window, Dialog, DialogManager, Data, ShowMode, StartMode | ||
from aiogram_dialog.widgets.text import Const | ||
from aiogram_dialog.widgets.kbd import Row, Button | ||
|
||
from src.api import client | ||
from src.bot.dialogs.communication import CreateRoomDialogResult, RoomDialogStartData | ||
from src.bot.dialogs.states import RoomlessSG, CreateRoomSG, RoomSG | ||
|
||
WELCOME_MESSAGE = """This is a welcome message | ||
You can: | ||
- Accept an invitation to a room | ||
- Create a new room""" | ||
|
||
INVITATIONS_BUTTON_ID = "invitations_button" | ||
CREATE_ROOM_BUTTON_ID = "create_room_button" | ||
|
||
|
||
async def process_result(start_data: Data, result: CreateRoomDialogResult, dialog_manager: DialogManager): | ||
if result.ok: | ||
room_id = await client.create_room(result.name, dialog_manager.event.from_user.id) | ||
# noinspection PyTypeChecker | ||
await dialog_manager.start( | ||
RoomSG.main, data=RoomDialogStartData(room_id, result.name), mode=StartMode.RESET_STACK | ||
) | ||
|
||
|
||
async def start_creating_room(event: CallbackQuery, button: Button, dialog_manager: DialogManager): | ||
await dialog_manager.start(CreateRoomSG.enter_name, show_mode=ShowMode.SEND) | ||
|
||
|
||
roomless_dialog = Dialog( | ||
Window( | ||
Const(WELCOME_MESSAGE), | ||
Row( | ||
Button(Const("Invitations"), INVITATIONS_BUTTON_ID), | ||
Button( | ||
Const("Create"), | ||
CREATE_ROOM_BUTTON_ID, | ||
on_click=start_creating_room, | ||
), | ||
), | ||
state=RoomlessSG.welcome, | ||
), | ||
on_process_result=process_result, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from aiogram.fsm.state import StatesGroup, State | ||
|
||
|
||
class CreateRoomSG(StatesGroup): | ||
enter_name = State() | ||
|
||
|
||
class RoomlessSG(StatesGroup): | ||
welcome = State() | ||
invitations = State() | ||
|
||
|
||
class RoomSG(StatesGroup): | ||
main = State() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from aiogram.types import Message | ||
from aiogram_dialog import DialogManager, StartMode | ||
|
||
from src.api import client | ||
from src.bot.dialogs.communication import RoomDialogStartData | ||
from src.bot.dialogs.states import RoomlessSG, RoomSG | ||
|
||
|
||
async def start_message_handler(message: Message, dialog_manager: DialogManager): | ||
user_id = message.from_user.id | ||
try: | ||
await client.create_user(user_id) | ||
except RuntimeError: | ||
pass | ||
|
||
try: | ||
room_info = await client.get_room_info(user_id) | ||
# noinspection PyTypeChecker | ||
await dialog_manager.start( | ||
RoomSG.main, | ||
data=RoomDialogStartData(room_info.id, room_info.name), | ||
mode=StartMode.RESET_STACK, | ||
) | ||
except RuntimeError: | ||
await dialog_manager.start(RoomlessSG.welcome, mode=StartMode.RESET_STACK) |