Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new Request widgets: RequestChat, RequestUser, RequestUsers #452

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/mega/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from bot_dialogs.link_preview import link_preview_dialog
from bot_dialogs.main import main_dialog
from bot_dialogs.mutltiwidget import multiwidget_dialog
from bot_dialogs.reply_buttons import reply_kbd_dialog
from bot_dialogs.reply_buttons import reply_kbd_dialog, reply_kbd_router
from bot_dialogs.scrolls import scroll_dialog
from bot_dialogs.select import selects_dialog
from bot_dialogs.switch import switch_dialog
Expand Down Expand Up @@ -60,6 +60,7 @@ async def on_unknown_intent(event: ErrorEvent, dialog_manager: DialogManager):

dialog_router = Router()
dialog_router.include_routers(
reply_kbd_router,
layouts_dialog,
scroll_dialog,
main_dialog,
Expand Down
40 changes: 40 additions & 0 deletions example/mega/bot_dialogs/reply_buttons.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from aiogram import F, Router
from aiogram.types import Message

from aiogram_dialog import Dialog, Window
from aiogram_dialog.widgets.kbd import (
Checkbox,
Radio,
RequestChat,
RequestContact,
RequestLocation,
RequestUser,
Row,
)
from aiogram_dialog.widgets.markup.reply_keyboard import ReplyKeyboardFactory
Expand All @@ -12,13 +17,48 @@
from . import states
from .common import MAIN_MENU_BUTTON

reply_kbd_router = Router()


@reply_kbd_router.message(F.chat_shared | F.user_shared)
async def on_user_shared(message: Message) -> None:
if message.chat_shared:
shifted_id = str(message.chat_shared.chat_id)[4:]
await message.answer(
f"ID: <b><code>{message.chat_shared.chat_id}</code></b>\n\n"
f"Shifted ID: <b><code>{shifted_id}</code></b>",
parse_mode="HTML",
)
elif message.user_shared:
await message.answer(
f"User ID: <b><code>{message.user_shared.user_id}</code></b>",
parse_mode="HTML",
)


reply_kbd_dialog = Dialog(
Window(
Const("Reply keyboard with multiple widgets.\n"),
Row(
RequestContact(Const("👤 Send contact")),
RequestLocation(Const("📍 Send location")),
),
Row(
RequestChat(
Const("Request chat"),
request_id=1,
chat_is_channel=True,
),
RequestUser(
Const("Request user"),
request_id=2,
),
RequestUser(
Const("Request premium user"),
request_id=3,
user_is_premium=True,
),
),
Checkbox(
checked_text=Const("✓ Checkbox"),
unchecked_text=Const(" Checkbox"),
Expand Down
11 changes: 10 additions & 1 deletion src/aiogram_dialog/widgets/kbd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"ManagedListGroup",
"StubScroll",
"CopyText",
"RequestChat",
"RequestUser",
"RequestUsers",
]

from .base import Keyboard
Expand All @@ -67,7 +70,13 @@
PrevPage,
SwitchPage,
)
from .request import RequestContact, RequestLocation
from .request import (
RequestChat,
RequestContact,
RequestLocation,
RequestUser,
RequestUsers,
)
from .scrolling_group import ScrollingGroup
from .select import (
ManagedMultiselect,
Expand Down
172 changes: 158 additions & 14 deletions src/aiogram_dialog/widgets/kbd/request.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from collections.abc import Callable
from typing import Union
from typing import Optional, Union

from aiogram.types import KeyboardButton
from aiogram.types import (
ChatAdministratorRights,
KeyboardButton,
KeyboardButtonRequestChat,
KeyboardButtonRequestUser,
KeyboardButtonRequestUsers,
)

from aiogram_dialog.api.internal import RawKeyboard
from aiogram_dialog.api.protocols import DialogManager
Expand All @@ -12,17 +18,17 @@

class RequestContact(Keyboard):
def __init__(
self,
text: Text,
when: Union[str, Callable, None] = None,
self,
text: Text,
when: Union[str, Callable, None] = None,
):
super().__init__(when=when)
self.text = text

async def _render_keyboard(
self,
data: dict,
manager: DialogManager,
self,
data: dict,
manager: DialogManager,
) -> RawKeyboard:
return [
[
Expand All @@ -36,17 +42,17 @@ async def _render_keyboard(

class RequestLocation(Keyboard):
def __init__(
self,
text: Text,
when: Union[str, Callable, None] = None,
self,
text: Text,
when: Union[str, Callable, None] = None,
):
super().__init__(when=when)
self.text = text

async def _render_keyboard(
self,
data: dict,
manager: DialogManager,
self,
data: dict,
manager: DialogManager,
) -> RawKeyboard:
return [
[
Expand All @@ -56,3 +62,141 @@ async def _render_keyboard(
),
],
]


class RequestChat(Keyboard):
def __init__(
self,
text: Text,
request_id: int,
chat_is_channel: bool,
chat_is_forum: Optional[bool] = None,
chat_has_username: Optional[bool] = None,
chat_is_created: Optional[bool] = None,
user_administrator_rights: Optional[ChatAdministratorRights] = None,
bot_administrator_rights: Optional[ChatAdministratorRights] = None,
bot_is_member: Optional[bool] = None,
request_title: Optional[bool] = None,
request_username: Optional[bool] = None,
request_photo: Optional[bool] = None,
when: Union[str, Callable, None] = None,
):
super().__init__(when=when)
self.text = text
self.request_id = request_id
self.chat_is_channel = chat_is_channel
self.chat_is_forum = chat_is_forum
self.chat_has_username = chat_has_username
self.chat_is_created = chat_is_created
self.user_administrator_rights = user_administrator_rights
self.bot_administrator_rights = bot_administrator_rights
self.bot_is_member = bot_is_member
self.request_title = request_title
self.request_username = request_username
self.request_photo = request_photo

async def _render_keyboard(
self,
data: dict,
manager: DialogManager,
) -> RawKeyboard:
return [
[
KeyboardButton(
text=await self.text.render_text(data, manager),
request_chat=KeyboardButtonRequestChat(
request_id=self.request_id,
chat_is_channel=self.chat_is_channel,
chat_is_forum=self.chat_is_forum,
chat_has_username=self.chat_has_username,
chat_is_created=self.chat_is_created,
user_administrator_rights=self.user_administrator_rights,
bot_administrator_rights=self.bot_administrator_rights,
bot_is_member=self.bot_is_member,
request_title=self.request_title,
request_username=self.request_username,
request_photo=self.request_photo,
),
),
],
]


class RequestUser(Keyboard):
def __init__(
self,
text: Text,
request_id: int,
user_is_bot: Optional[bool] = None,
user_is_premium: Optional[bool] = None,
when: Union[str, Callable, None] = None,
):
super().__init__(when=when)
self.text = text
self.request_id = request_id
self.user_is_bot = user_is_bot
self.user_is_premium = user_is_premium

async def _render_keyboard(
self,
data: dict,
manager: DialogManager,
) -> RawKeyboard:
return [
[
KeyboardButton(
text=await self.text.render_text(data, manager),
request_user=KeyboardButtonRequestUser(
request_id=self.request_id,
user_is_bot=self.user_is_bot,
user_is_premium=self.user_is_premium,
),
),
],
]


class RequestUsers(Keyboard):
def __init__(
self,
text: Text,
request_id: int,
user_is_bot: Optional[bool] = None,
user_is_premium: Optional[bool] = None,
max_quantity: Optional[int] = None,
request_name: Optional[bool] = None,
request_username: Optional[bool] = None,
request_photo: Optional[bool] = None,
when: Union[str, Callable, None] = None,
):
super().__init__(when=when)
self.text = text
self.request_id = request_id
self.user_is_bot = user_is_bot
self.user_is_premium = user_is_premium
self.max_quantity = max_quantity
self.request_name = request_name
self.request_username = request_username
self.request_photo = request_photo

async def _render_keyboard(
self,
data: dict,
manager: DialogManager,
) -> RawKeyboard:
return [
[
KeyboardButton(
text=await self.text.render_text(data, manager),
request_users=KeyboardButtonRequestUsers(
request_id=self.request_id,
user_is_bot=self.user_is_bot,
user_is_premium=self.user_is_premium,
max_quantity=self.max_quantity,
request_name=self.request_name,
request_username=self.request_username,
request_photo=self.request_photo,
),
),
],
]
Loading