From b2c49912faa5ae611ecc7f8d804972f5e7c2a72d Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Tue, 20 Aug 2024 00:20:54 +0500 Subject: [PATCH] Rename ReplyMarkup to Keyboard --- .../telethon/_impl/client/client/client.py | 47 ++++++------ .../src/telethon/_impl/client/client/files.py | 26 +++---- .../telethon/_impl/client/client/messages.py | 10 +-- .../_impl/client/types/buttons/__init__.py | 6 +- .../_impl/client/types/buttons/keyboard.py | 53 ++++++++++++++ .../client/types/buttons/reply_markup.py | 71 ------------------- .../telethon/_impl/client/types/message.py | 19 ++--- client/src/telethon/types/__init__.py | 8 +-- 8 files changed, 112 insertions(+), 128 deletions(-) create mode 100644 client/src/telethon/_impl/client/types/buttons/keyboard.py delete mode 100644 client/src/telethon/_impl/client/types/buttons/reply_markup.py diff --git a/client/src/telethon/_impl/client/client/client.py b/client/src/telethon/_impl/client/client/client.py index b00b5d35..24b40e81 100644 --- a/client/src/telethon/_impl/client/client/client.py +++ b/client/src/telethon/_impl/client/client/client.py @@ -45,7 +45,7 @@ from ..types import ( RecentAction, User, ) -from ..types.buttons.reply_markup import ReplyMarkupType +from ..types.buttons.keyboard import KeyboardType from .auth import ( bot_sign_in, check_password, @@ -252,12 +252,13 @@ class Client: self._message_box = MessageBox(base_logger=base_logger) self._chat_hashes = ChatHashCache(None) self._last_update_limit_warn: Optional[float] = None - self._updates: asyncio.Queue[ - tuple[abcs.Update, dict[int, Peer]] - ] = asyncio.Queue(maxsize=self._config.update_queue_limit or 0) + self._updates: asyncio.Queue[tuple[abcs.Update, dict[int, Peer]]] = ( + asyncio.Queue(maxsize=self._config.update_queue_limit or 0) + ) self._dispatcher: Optional[asyncio.Task[None]] = None self._handlers: dict[ - Type[Event], list[tuple[Callable[[Any], Awaitable[Any]], Optional[Filter]]] + Type[Event], + list[tuple[Callable[[Any], Awaitable[Any]], Optional[Filter]]], ] = {} self._check_all_handlers = check_all_handlers @@ -572,7 +573,7 @@ class Client: markdown: Optional[str] = None, html: Optional[str] = None, link_preview: bool = False, - buttons: Optional[ReplyMarkupType] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: """ Edit a message. @@ -587,10 +588,10 @@ class Client: :param markdown: See :ref:`formatting`. :param html: See :ref:`formatting`. :param link_preview: See :ref:`formatting`. - :param buttons: - The buttons to use for the message. + :param keyboard: + The keyboard to use for the message. - Only bot accounts can send buttons. + Only bot accounts can send keyboard. :return: The edited message. @@ -616,7 +617,7 @@ class Client: markdown=markdown, html=html, link_preview=link_preview, - buttons=buttons, + keyboard=keyboard, ) async def forward_messages( @@ -1394,7 +1395,7 @@ class Client: caption_markdown: Optional[str] = None, caption_html: Optional[str] = None, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: """ Send an audio file. @@ -1438,7 +1439,7 @@ class Client: caption_markdown=caption_markdown, caption_html=caption_html, reply_to=reply_to, - buttons=buttons, + keyboard=keyboard, ) async def send_file( @@ -1467,7 +1468,7 @@ class Client: caption_markdown: Optional[str] = None, caption_html: Optional[str] = None, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType], + keyboard: Optional[KeyboardType], ) -> Message: """ Send any type of file with any amount of attributes. @@ -1621,7 +1622,7 @@ class Client: caption_markdown=caption_markdown, caption_html=caption_html, reply_to=reply_to, - buttons=buttons, + keyboard=keyboard, ) async def send_message( @@ -1634,7 +1635,7 @@ class Client: html: Optional[str] = None, link_preview: bool = False, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: """ Send a message. @@ -1650,10 +1651,10 @@ class Client: :param reply_to: The message identifier of the message to reply to. - :param buttons: - The buttons to use for the message. + :param keyboard: + The keyboard to use for the message. - Only bot accounts can send buttons. + Only bot accounts can send keyboard. .. rubric:: Example @@ -1669,7 +1670,7 @@ class Client: html=html, link_preview=link_preview, reply_to=reply_to, - buttons=buttons, + keyboard=keyboard, ) async def send_photo( @@ -1688,7 +1689,7 @@ class Client: caption_markdown: Optional[str] = None, caption_html: Optional[str] = None, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: """ Send a photo file. @@ -1734,7 +1735,7 @@ class Client: caption_markdown=caption_markdown, caption_html=caption_html, reply_to=reply_to, - buttons=buttons, + keyboard=keyboard, ) async def send_video( @@ -1756,7 +1757,7 @@ class Client: caption_markdown: Optional[str] = None, caption_html: Optional[str] = None, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType], + keyboard: Optional[KeyboardType], ) -> Message: """ Send a video file. @@ -1803,7 +1804,7 @@ class Client: caption_markdown=caption_markdown, caption_html=caption_html, reply_to=reply_to, - buttons=buttons, + keyboard=keyboard, ) async def set_chat_default_restrictions( diff --git a/client/src/telethon/_impl/client/client/files.py b/client/src/telethon/_impl/client/client/files.py index 065cd994..ba12a65d 100644 --- a/client/src/telethon/_impl/client/client/files.py +++ b/client/src/telethon/_impl/client/client/files.py @@ -22,7 +22,7 @@ from ..types import ( parse_message, try_get_url_path, ) -from ..types.buttons.reply_markup import ReplyMarkupType +from ..types.buttons.keyboard import KeyboardType if TYPE_CHECKING: from .client import Client @@ -57,7 +57,7 @@ async def send_photo( caption_markdown: Optional[str] = None, caption_html: Optional[str] = None, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: return await send_file( self, @@ -77,7 +77,7 @@ async def send_photo( caption_markdown=caption_markdown, caption_html=caption_html, reply_to=reply_to, - buttons=buttons, + keyboard=keyboard, ) @@ -98,7 +98,7 @@ async def send_audio( caption_markdown: Optional[str] = None, caption_html: Optional[str] = None, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: return await send_file( self, @@ -115,7 +115,7 @@ async def send_audio( caption_markdown=caption_markdown, caption_html=caption_html, reply_to=reply_to, - buttons=buttons, + keyboard=keyboard, ) @@ -138,7 +138,7 @@ async def send_video( caption_markdown: Optional[str] = None, caption_html: Optional[str] = None, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType], + keyboard: Optional[KeyboardType], ) -> Message: return await send_file( self, @@ -157,7 +157,7 @@ async def send_video( caption_markdown=caption_markdown, caption_html=caption_html, reply_to=reply_to, - buttons=buttons, + keyboard=keyboard, ) @@ -187,7 +187,7 @@ async def send_file( caption_markdown: Optional[str] = None, caption_html: Optional[str] = None, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType], + keyboard: Optional[KeyboardType], ) -> Message: message, entities = parse_message( text=caption, markdown=caption_markdown, html=caption_html, allow_empty=True @@ -196,7 +196,7 @@ async def send_file( # Re-send existing file. if isinstance(file, File): return await do_send_file( - self, chat, file._input_media, message, entities, reply_to, buttons + self, chat, file._input_media, message, entities, reply_to, keyboard ) # URLs are handled early as they can't use any other attributes either. @@ -220,7 +220,7 @@ async def send_file( spoiler=False, url=file, ttl_seconds=None ) return await do_send_file( - self, chat, input_media, message, entities, reply_to, buttons + self, chat, input_media, message, entities, reply_to, keyboard ) input_file, name = await upload(self, file, size, name) @@ -286,7 +286,7 @@ async def send_file( ) return await do_send_file( - self, chat, input_media, message, entities, reply_to, buttons + self, chat, input_media, message, entities, reply_to, keyboard ) @@ -297,7 +297,7 @@ async def do_send_file( message: str, entities: Optional[list[abcs.MessageEntity]], reply_to: Optional[int], - buttons: Optional[ReplyMarkupType], + keyboard: Optional[KeyboardType], ) -> Message: random_id = generate_random_id() return client._build_message_map( @@ -317,7 +317,7 @@ async def do_send_file( media=input_media, message=message, random_id=random_id, - reply_markup=buttons._build() if buttons is not None else None, + reply_markup=keyboard._raw if keyboard else None, entities=entities, schedule_date=None, send_as=None, diff --git a/client/src/telethon/_impl/client/client/messages.py b/client/src/telethon/_impl/client/client/messages.py index 6ccbac7a..a1438167 100644 --- a/client/src/telethon/_impl/client/client/messages.py +++ b/client/src/telethon/_impl/client/client/messages.py @@ -15,7 +15,7 @@ from ..types import ( parse_message, peer_id, ) -from ..types.buttons.reply_markup import ReplyMarkupType +from ..types.buttons.keyboard import KeyboardType if TYPE_CHECKING: from .client import Client @@ -31,7 +31,7 @@ async def send_message( html: Optional[str] = None, link_preview: bool = False, reply_to: Optional[int] = None, - buttons: Optional[ReplyMarkupType] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: random_id = generate_random_id() @@ -78,7 +78,7 @@ async def send_message( ), message=message, random_id=random_id, - reply_markup=buttons._build() if buttons is not None else None, + reply_markup=keyboard._raw if keyboard else None, entities=entities, schedule_date=None, send_as=None, @@ -128,7 +128,7 @@ async def edit_message( markdown: Optional[str] = None, html: Optional[str] = None, link_preview: bool = False, - buttons: Optional[ReplyMarkupType] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: message, entities = parse_message( text=text, markdown=markdown, html=html, allow_empty=False @@ -141,7 +141,7 @@ async def edit_message( id=message_id, message=message, media=None, - reply_markup=buttons._build() if buttons is not None else None, + reply_markup=keyboard._raw if keyboard else None, entities=entities, schedule_date=None, ) diff --git a/client/src/telethon/_impl/client/types/buttons/__init__.py b/client/src/telethon/_impl/client/types/buttons/__init__.py index 429af7f6..47eb3930 100644 --- a/client/src/telethon/_impl/client/types/buttons/__init__.py +++ b/client/src/telethon/_impl/client/types/buttons/__init__.py @@ -7,7 +7,7 @@ from ....tl import abcs, types from .button import Button from .callback import Callback from .inline_button import InlineButton -from .reply_markup import ReplyInlineMarkup, ReplyKeyboardMarkup +from .keyboard import InlineKeyboard, Keyboard from .request_geo_location import RequestGeoLocation from .request_phone import RequestPhone from .request_poll import RequestPoll @@ -80,8 +80,8 @@ __all__ = [ "Button", "Callback", "InlineButton", - "ReplyInlineMarkup", - "ReplyKeyboardMarkup", + "InlineKeyboard", + "Keyboard", "RequestGeoLocation", "RequestPhone", "RequestPoll", diff --git a/client/src/telethon/_impl/client/types/buttons/keyboard.py b/client/src/telethon/_impl/client/types/buttons/keyboard.py new file mode 100644 index 00000000..5e10befb --- /dev/null +++ b/client/src/telethon/_impl/client/types/buttons/keyboard.py @@ -0,0 +1,53 @@ +from typing import Optional, TypeAlias + +from ....tl import abcs, types +from .button import Button + + +def _build_keyboard_rows( + btns: list[Button] | list[list[Button]], +) -> list[abcs.KeyboardButtonRow]: + # list[button] -> list[list[button]] + # This does allow for "invalid" inputs (mixing lists and non-lists), but that's acceptable. + buttons_lists_iter = [ + button if isinstance(button, list) else [button] for button in (btns or []) + ] + # Remove empty rows (also making it easy to check if all-empty). + buttons_lists = [bs for bs in buttons_lists_iter if bs] + + return [ + types.KeyboardButtonRow(buttons=[btn._raw for btn in btns]) + for btns in buttons_lists + ] + + +class Keyboard: + __slots__ = ("_raw",) + + def __init__( + self, + buttons: list[Button] | list[list[Button]], + resize: bool, + single_use: bool, + selective: bool, + persistent: bool, + placeholder: Optional[str], + ) -> None: + self._raw = types.ReplyKeyboardMarkup( + resize=resize, + single_use=single_use, + selective=selective, + persistent=persistent, + rows=_build_keyboard_rows(buttons), + placeholder=placeholder, + ) + + +class InlineKeyboard: + __slots__ = ("_raw",) + + def __init__(self, buttons: list[Button] | list[list[Button]]) -> None: + self._raw = types.ReplyInlineMarkup(rows=_build_keyboard_rows(buttons)) + + +KeyboardType: TypeAlias = Keyboard | InlineKeyboard diff --git a/client/src/telethon/_impl/client/types/buttons/reply_markup.py b/client/src/telethon/_impl/client/types/buttons/reply_markup.py deleted file mode 100644 index db3cabe0..00000000 --- a/client/src/telethon/_impl/client/types/buttons/reply_markup.py +++ /dev/null @@ -1,71 +0,0 @@ -from typing import Optional, TypeAlias - -from ....tl import abcs, types -from .button import Button - - -def build_keyboard_rows( - btns: list[Button] | list[list[Button]], -) -> list[abcs.KeyboardButtonRow]: - # list[button] -> list[list[button]] - # This does allow for "invalid" inputs (mixing lists and non-lists), but that's acceptable. - buttons_lists_iter = [ - button if isinstance(button, list) else [button] for button in (btns or []) - ] - # Remove empty rows (also making it easy to check if all-empty). - buttons_lists = [bs for bs in buttons_lists_iter if bs] - - return [ - types.KeyboardButtonRow(buttons=[btn._raw for btn in btns]) - for btns in buttons_lists - ] - - -class ReplyKeyboardMarkup: - __slots__ = ( - "_btns", - "resize", - "single_use", - "selective", - "persistent", - "placeholder", - ) - - def __init__( - self, - btns: list[Button] | list[list[Button]], - resize: bool, - single_use: bool, - selective: bool, - persistent: bool, - placeholder: Optional[str], - ) -> None: - self._btns = build_keyboard_rows(btns) - self.resize = resize - self.single_use = single_use - self.selective = selective - self.persistent = persistent - self.placeholder = placeholder - - def _build(self) -> abcs.ReplyMarkup: - return types.ReplyKeyboardMarkup( - resize=self.resize, - single_use=self.single_use, - selective=self.selective, - persistent=self.persistent, - rows=self._btns, - placeholder=self.placeholder, - ) - - -class ReplyInlineMarkup: - __slots__ = ("_btns",) - - def __init__(self, btns: list[Button] | list[list[Button]]) -> None: - self._btns = build_keyboard_rows(btns) - - def _build(self) -> abcs.ReplyMarkup: - return types.ReplyInlineMarkup(rows=self._btns) - - -ReplyMarkupType: TypeAlias = ReplyKeyboardMarkup | ReplyInlineMarkup diff --git a/client/src/telethon/_impl/client/types/message.py b/client/src/telethon/_impl/client/types/message.py index 074d5dca..bf2ef850 100644 --- a/client/src/telethon/_impl/client/types/message.py +++ b/client/src/telethon/_impl/client/types/message.py @@ -13,6 +13,7 @@ from ..parsers import ( parse_markdown_message, ) from .buttons import Button, as_concrete_row, create_button +from .buttons.keyboard import KeyboardType from .file import File from .meta import NoPublicConstructor from .peer import Peer, expand_peer, peer_id @@ -333,7 +334,7 @@ class Message(metaclass=NoPublicConstructor): markdown: Optional[str] = None, html: Optional[str] = None, link_preview: bool = False, - buttons: Optional[list[Button] | list[list[Button]]] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: """ Alias for :meth:`telethon.Client.send_message`. @@ -342,7 +343,7 @@ class Message(metaclass=NoPublicConstructor): :param markdown: See :ref:`formatting`. :param html: See :ref:`formatting`. :param link_preview: See :meth:`~telethon.Client.send_message`. - :param buttons: See :meth:`~telethon.Client.send_message`. + :param keyboard: See :meth:`~telethon.Client.send_message`. """ return await self._client.send_message( self.chat, @@ -350,7 +351,7 @@ class Message(metaclass=NoPublicConstructor): markdown=markdown, html=html, link_preview=link_preview, - buttons=buttons, + keyboard=keyboard, ) async def reply( @@ -360,7 +361,7 @@ class Message(metaclass=NoPublicConstructor): markdown: Optional[str] = None, html: Optional[str] = None, link_preview: bool = False, - buttons: Optional[list[Button] | list[list[Button]]] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: """ Alias for :meth:`telethon.Client.send_message` with the ``reply_to`` parameter set to this message. @@ -369,7 +370,7 @@ class Message(metaclass=NoPublicConstructor): :param markdown: See :ref:`formatting`. :param html: See :ref:`formatting`. :param link_preview: See :meth:`~telethon.Client.send_message`. - :param buttons: See :meth:`~telethon.Client.send_message`. + :param keyboard: See :meth:`~telethon.Client.send_message`. """ return await self._client.send_message( self.chat, @@ -378,7 +379,7 @@ class Message(metaclass=NoPublicConstructor): html=html, link_preview=link_preview, reply_to=self.id, - buttons=buttons, + keyboard=keyboard, ) async def delete(self, *, revoke: bool = True) -> None: @@ -395,7 +396,7 @@ class Message(metaclass=NoPublicConstructor): markdown: Optional[str] = None, html: Optional[str] = None, link_preview: bool = False, - buttons: Optional[list[Button] | list[list[Button]]] = None, + keyboard: Optional[KeyboardType] = None, ) -> Message: """ Alias for :meth:`telethon.Client.edit_message`. @@ -404,7 +405,7 @@ class Message(metaclass=NoPublicConstructor): :param markdown: See :ref:`formatting`. :param html: See :ref:`formatting`. :param link_preview: See :meth:`~telethon.Client.send_message`. - :param buttons: See :meth:`~telethon.Client.send_message`. + :param keyboard: See :meth:`~telethon.Client.send_message`. """ return await self._client.edit_message( self.chat, @@ -413,7 +414,7 @@ class Message(metaclass=NoPublicConstructor): markdown=markdown, html=html, link_preview=link_preview, - buttons=buttons, + keyboard=keyboard, ) async def forward(self, target: Peer | PeerRef) -> Message: diff --git a/client/src/telethon/types/__init__.py b/client/src/telethon/types/__init__.py index 8333901a..7b03637b 100644 --- a/client/src/telethon/types/__init__.py +++ b/client/src/telethon/types/__init__.py @@ -25,8 +25,8 @@ from .._impl.client.types import ( from .._impl.client.types.buttons import ( Button, InlineButton, - ReplyInlineMarkup, - ReplyKeyboardMarkup, + InlineKeyboard, + Keyboard, ) from .._impl.session import ChannelRef, GroupRef, PeerRef, UserRef @@ -42,13 +42,13 @@ __all__ = [ "Draft", "File", "Group", + "InlineKeyboard", "InlineResult", + "Keyboard", "LoginToken", "Message", "Participant", "PasswordToken", - "ReplyInlineMarkup", - "ReplyKeyboardMarkup", "RecentAction", "User", "Button",