Skip to content

Commit

Permalink
refactor: add a dialog for prompting strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Makcal committed May 24, 2024
1 parent b2f2f7d commit 9a4afe7
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 52 deletions.
6 changes: 3 additions & 3 deletions src/bot/dialogs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src.bot.dialogs.roomless import roomless_dialog
from src.bot.dialogs.create_room import create_room_dialog
from src.bot.dialogs.prompt import prompt_dialog
from src.bot.dialogs.room import room_dialog
from src.bot.dialogs.confirmation import confirmation_dialog
from src.bot.dialogs.incoming_invitations import incoming_invitations_dialog
Expand All @@ -9,7 +9,7 @@
dialogs = [
roomless_dialog,
room_dialog,
create_room_dialog,
prompt_dialog,
incoming_invitations_dialog,
confirmation_dialog,
outgoing_invitations_dialog,
Expand All @@ -20,7 +20,7 @@
"dialogs",
"roomless_dialog",
"room_dialog",
"create_room_dialog",
"prompt_dialog",
"confirmation_dialog",
"outgoing_invitations_dialog",
"incoming_invitations_dialog",
Expand Down
2 changes: 1 addition & 1 deletion src/bot/dialogs/confirmation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aiogram_dialog.widgets.kbd import Row, Select
from aiogram_dialog.widgets.text import Format

from src.bot.dialogs.communication import ConfirmationDialogStartData
from src.bot.dialogs.dialog_communications import ConfirmationDialogStartData
from src.bot.dialogs.states import ConfirmationSG


Expand Down
25 changes: 0 additions & 25 deletions src/bot/dialogs/create_room.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import re
from dataclasses import dataclass


@dataclass
class CreateRoomDialogResult:
name: str | None = None
created: bool = True
from typing import Callable


@dataclass
Expand Down Expand Up @@ -37,10 +33,32 @@ class TaskViewDialogStartData:
task_id: int


@dataclass
class PromptDialogStartData:
content: str
cancel_message: str | None = "Canceled"
cancel_button: str = "Cancel"
format: str = "Enter {}"
filter: re.Pattern | Callable[[str], bool] | None = None

@property
def prompt(self) -> str:
return self.format.format(self.content)

def validate(self, text: str) -> bool:
if self.filter is None:
return True
if isinstance(self.filter, re.Pattern):
return self.filter.fullmatch(text) is not None
if callable(self.filter):
return self.filter(text)
raise TypeError("Filter's type is incorrect")


__all__ = [
"CreateRoomDialogResult",
"RoomDialogStartData",
"ConfirmationDialogStartData",
"IncomingInvitationDialogStartData",
"TaskViewDialogStartData",
"PromptDialogStartData",
]
2 changes: 1 addition & 1 deletion src/bot/dialogs/incoming_invitations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from src.api import client
from src.api.schemas.method_output_schemas import IncomingInvitationInfo
from src.bot.dialogs.communication import (
from src.bot.dialogs.dialog_communications import (
ConfirmationDialogStartData,
RoomDialogStartData,
IncomingInvitationDialogStartData,
Expand Down
2 changes: 1 addition & 1 deletion src/bot/dialogs/outgoing_invitations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from src.api import client
from src.api.schemas.method_output_schemas import SentInvitationInfo
from src.bot.dialogs.communication import ConfirmationDialogStartData
from src.bot.dialogs.dialog_communications import ConfirmationDialogStartData
from src.bot.dialogs.states import OutgoingInvitationsSG, ConfirmationSG
from src.bot.utils import list_group_finder

Expand Down
47 changes: 47 additions & 0 deletions src/bot/dialogs/prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from aiogram.types import Message, CallbackQuery
from aiogram_dialog import Dialog, Window, DialogManager, ShowMode
from aiogram_dialog.widgets.input import TextInput
from aiogram_dialog.widgets.kbd import Button
from aiogram_dialog.widgets.text import Format

from src.bot.dialogs.dialog_communications import PromptDialogStartData
from src.bot.dialogs.states import PromptSG


class Events:
@staticmethod
async def on_start(start_data: dict, manager: DialogManager):
args = PromptDialogStartData(**start_data["input"])
manager.dialog_data["args"] = args

@staticmethod
async def on_cancel(callback: CallbackQuery, widget, manager: DialogManager):
await callback.bot.send_message(callback.message.chat.id, manager.dialog_data["args"].cancel_message)
await manager.done(None, show_mode=ShowMode.NO_UPDATE)

@staticmethod
async def on_input(message: Message, widget, manager: DialogManager, text: str):
if not manager.dialog_data["args"].validate(text):
await manager.show(ShowMode.SEND)
return
await manager.done(text, show_mode=ShowMode.NO_UPDATE)


async def getter(dialog_manager: DialogManager, **kwargs):
dialog_args: PromptDialogStartData = dialog_manager.dialog_data["args"]
return {
"message": dialog_args.prompt,
"cancel_button": dialog_args.cancel_button,
}


prompt_dialog = Dialog(
Window(
Format("{message}"),
Button(Format("{cancel_button}"), id="cancel_button", on_click=Events.on_cancel),
TextInput("input_name", on_success=Events.on_input),
state=PromptSG.main,
getter=getter,
),
on_start=Events.on_start,
)
2 changes: 1 addition & 1 deletion src/bot/dialogs/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from src.api import client
from src.api.schemas.method_output_schemas import DailyInfoResponse, UserInfo, Task
from src.bot.dialogs.communication import (
from src.bot.dialogs.dialog_communications import (
RoomDialogStartData,
ConfirmationDialogStartData,
IncomingInvitationDialogStartData,
Expand Down
27 changes: 19 additions & 8 deletions src/bot/dialogs/roomless.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
from aiogram_dialog.widgets.text import Const

from src.api import client
from src.bot.dialogs.communication import CreateRoomDialogResult, RoomDialogStartData, IncomingInvitationDialogStartData
from src.bot.dialogs.states import RoomlessSG, CreateRoomSG, RoomSG, IncomingInvitationsSG
from src.bot.dialogs.dialog_communications import (
RoomDialogStartData,
IncomingInvitationDialogStartData,
PromptDialogStartData,
)
from src.bot.dialogs.states import RoomlessSG, PromptSG, RoomSG, IncomingInvitationsSG


class WelcomeWindowConsts:
Expand All @@ -22,22 +26,29 @@ class WelcomeWindowConsts:

class Events:
@staticmethod
async def process_create_room_result(start_data: Data, result: CreateRoomDialogResult, manager: DialogManager):
async def on_process_result(start_data: Data, result: str | None, manager: DialogManager):
if not isinstance(start_data, dict):
return

if start_data["intent"] == "create_room":
if result.created:
room_id = await client.create_room(result.name, manager.event.from_user.id)
if result is not None:
room_id = await client.create_room(result, manager.event.from_user.id)
await manager.start(
RoomSG.main,
data={"input": dataclasses.asdict(RoomDialogStartData(room_id, result.name))},
data={"input": dataclasses.asdict(RoomDialogStartData(room_id, result))},
mode=StartMode.RESET_STACK,
)
else:
await manager.show(ShowMode.SEND)
return

@staticmethod
async def on_click_create_room(event: CallbackQuery, button: Button, dialog_manager: DialogManager):
await dialog_manager.start(CreateRoomSG.enter_name, data={"intent": "create_room"}, show_mode=ShowMode.SEND)
await dialog_manager.start(
PromptSG.main,
data={"intent": "create_room", "input": dataclasses.asdict(PromptDialogStartData("a room's name"))},
show_mode=ShowMode.SEND,
)


roomless_dialog = Dialog(
Expand All @@ -62,5 +73,5 @@ async def on_click_create_room(event: CallbackQuery, button: Button, dialog_mana
),
state=RoomlessSG.welcome,
),
on_process_result=Events.process_create_room_result,
on_process_result=Events.on_process_result,
)
6 changes: 3 additions & 3 deletions src/bot/dialogs/states.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from aiogram.fsm.state import StatesGroup, State


class CreateRoomSG(StatesGroup):
enter_name = State()
class PromptSG(StatesGroup):
main = State()


class RoomlessSG(StatesGroup):
Expand Down Expand Up @@ -34,7 +34,7 @@ class TaskViewSG(StatesGroup):


__all__ = [
"CreateRoomSG",
"PromptSG",
"RoomlessSG",
"RoomSG",
"IncomingInvitationsSG",
Expand Down
2 changes: 1 addition & 1 deletion src/bot/dialogs/task_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from src.api import client
from src.api.schemas.method_output_schemas import UserInfo, TaskInfoResponse
from src.bot.dialogs.communication import TaskViewDialogStartData
from src.bot.dialogs.dialog_communications import TaskViewDialogStartData
from src.bot.dialogs.states import TaskViewSG


Expand Down
2 changes: 1 addition & 1 deletion src/bot/start_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from aiogram_dialog import DialogManager, StartMode

from src.api import client
from src.bot.dialogs.communication import RoomDialogStartData
from src.bot.dialogs.dialog_communications import RoomDialogStartData
from src.bot.dialogs.states import RoomlessSG, RoomSG


Expand Down

0 comments on commit 9a4afe7

Please sign in to comment.