From b524fb60493d66d05ff6c385a3b0211ddaabc45f Mon Sep 17 00:00:00 2001 From: wulan17 Date: Sat, 30 Dec 2023 16:53:57 +0700 Subject: [PATCH] Pyrofork: fix {chat,user}_shared Signed-off-by: wulan17 --- .../bots_and_keyboards/keyboard_button.py | 94 +++++++++---------- .../request_peer_type_channel.py | 4 +- .../request_peer_type_chat.py | 4 +- .../request_peer_type_user.py | 4 +- pyrogram/types/messages_and_media/message.py | 37 ++++---- 5 files changed, 76 insertions(+), 67 deletions(-) diff --git a/pyrogram/types/bots_and_keyboards/keyboard_button.py b/pyrogram/types/bots_and_keyboards/keyboard_button.py index f0969c05..5c144e21 100644 --- a/pyrogram/types/bots_and_keyboards/keyboard_button.py +++ b/pyrogram/types/bots_and_keyboards/keyboard_button.py @@ -18,7 +18,7 @@ from pyrogram import raw, types from ..object import Object - +from typing import Union class KeyboardButton(Object): """One button of the reply keyboard. @@ -38,15 +38,11 @@ class KeyboardButton(Object): If True, the user's current location will be sent when the button is pressed. Available in private chats only. - request_channel ("obj:`~pyrogram.types.RequestPeerTypeChannel`, *optional*): - If specified, defines the criteria used to request a suitable channels. - The identifier of the selected channels will be shared with the bot when the corresponding button is pressed. - - request_chat ("obj:`~pyrogram.types.RequestPeerTypeChat`, *optional*): - If specified, defines the criteria used to request a suitable chats. + request_chat (:obj:`~pyrogram.types.RequestPeerTypeChannel` | :obj:`~pyrogram.types.RequestPeerTypeChat`, *optional*): + If specified, defines the criteria used to request a suitable chats/channels. The identifier of the selected chats will be shared with the bot when the corresponding button is pressed. - request_user ("obj:`~pyrogram.types.RequestPeerTypeUser`, *optional*): + request_user (:obj:`~pyrogram.types.RequestPeerTypeUser`, *optional*): If specified, defines the criteria used to request a suitable users. The identifier of the selected users will be shared with the bot when the corresponding button is pressed. @@ -62,8 +58,7 @@ class KeyboardButton(Object): text: str, request_contact: bool = None, request_location: bool = None, - request_channel: "types.RequestPeerTypeChannel" = None, - request_chat: "types.RequestPeerTypeChat" = None, + request_chat: Union["types.RequestPeerTypeChat","types.RequestPeerTypeChannel"] = None, request_user: "types.RequestPeerTypeUser" = None, web_app: "types.WebAppInfo" = None ): @@ -72,7 +67,6 @@ class KeyboardButton(Object): self.text = str(text) self.request_contact = request_contact self.request_location = request_location - self.request_channel = request_channel self.request_chat = request_chat self.request_user = request_user self.web_app = web_app @@ -102,33 +96,36 @@ class KeyboardButton(Object): ) ) - if isinstance(b, raw.types.RequestPeerTypeBroadcast): - return KeyboardButton( - text=b.text, - request_chat=types.RequestPeerTypeChannel( - is_creator=b.creator, - is_username=b.has_username + if isinstance(b, raw.types.KeyboardButtonRequestPeer): + if isinstance(b.peer_type, raw.types.RequestPeerTypeBroadcast): + return KeyboardButton( + text=b.text, + request_chat=types.RequestPeerTypeChannel( + is_creator=b.peer_type.creator, + is_username=b.peer_type.has_username, + max=b.max_quantity + ) + ) + if isinstance(b.peer_type, raw.types.RequestPeerTypeChat): + return KeyboardButton( + text=b.text, + request_chat=types.RequestPeerTypeChat( + is_creator=b.peer_type.creator, + is_bot_participant=b.peer_type.bot_participant, + is_username=b.peer_type.has_username, + is_forum=b.peer_type.forum, + max=b.max_quantity + ) ) - ) - if isinstance(b, raw.types.RequestPeerTypeChat): - return KeyboardButton( - text=b.text, - request_chat=types.RequestPeerTypeChat( - is_creator=b.creator, - is_bot_participant=b.bot_participant, - is_username=b.has_username, - is_forum=b.forum - ) - ) - - if isinstance(b, raw.types.RequestPeerTypeUser): - return KeyboardButton( - text=b.text, - request_user=types.RequestPeerTypeUser( - is_bot=b.bot, - is_premium=b.premium - ) + if isinstance(b.peer_type, raw.types.RequestPeerTypeUser): + return KeyboardButton( + text=b.text, + request_user=types.RequestPeerTypeUser( + is_bot=b.peer_type.bot, + is_premium=b.peer_type.premium, + max=b.max_quantity + ) ) def write(self): @@ -136,16 +133,17 @@ class KeyboardButton(Object): return raw.types.KeyboardButtonRequestPhone(text=self.text) elif self.request_location: return raw.types.KeyboardButtonRequestGeoLocation(text=self.text) - elif self.request_channel: - return raw.types.KeyboardButtonRequestPeer( - text=self.text, - button_id=0, - peer_type=raw.types.RequestPeerTypeBroadcast( - creator=self.request_broadcast.is_creator, - has_username=self.request_broadcast.is_username - ) - ) elif self.request_chat: + if isinstance(self.request_chat, types.RequestPeerTypeChannel): + return raw.types.KeyboardButtonRequestPeer( + text=self.text, + button_id=0, + peer_type=raw.types.RequestPeerTypeBroadcast( + creator=self.request_chat.is_creator, + has_username=self.request_chat.is_username + ), + max_quantity=self.request_chat.max + ) return raw.types.KeyboardButtonRequestPeer( text=self.text, button_id=0, @@ -154,7 +152,8 @@ class KeyboardButton(Object): bot_participant=self.request_chat.is_bot_participant, has_username=self.request_chat.is_username, forum=self.request_chat.is_forum - ) + ), + max_quantity=self.request_chat.max ) elif self.request_user: return raw.types.KeyboardButtonRequestPeer( @@ -163,7 +162,8 @@ class KeyboardButton(Object): peer_type=raw.types.RequestPeerTypeUser( bot=self.request_user.is_bot, premium=self.request_user.is_premium - ) + ), + max_quantity=self.request_user.max ) elif self.web_app: return raw.types.KeyboardButtonSimpleWebView(text=self.text, url=self.web_app.url) diff --git a/pyrogram/types/bots_and_keyboards/request_peer_type_channel.py b/pyrogram/types/bots_and_keyboards/request_peer_type_channel.py index a67af2cc..87a4dd45 100644 --- a/pyrogram/types/bots_and_keyboards/request_peer_type_channel.py +++ b/pyrogram/types/bots_and_keyboards/request_peer_type_channel.py @@ -33,9 +33,11 @@ class RequestPeerTypeChannel(Object): def __init__( self, is_creator: bool=None, - is_username: bool=None + is_username: bool=None, + max: int=1 ): super().__init__() self.is_creator = is_creator self.is_username = is_username + self.max = max diff --git a/pyrogram/types/bots_and_keyboards/request_peer_type_chat.py b/pyrogram/types/bots_and_keyboards/request_peer_type_chat.py index 905c50d4..7249a9dc 100644 --- a/pyrogram/types/bots_and_keyboards/request_peer_type_chat.py +++ b/pyrogram/types/bots_and_keyboards/request_peer_type_chat.py @@ -41,7 +41,8 @@ class RequestPeerTypeChat(Object): is_creator: bool=None, is_bot_participant: bool=None, is_username: bool=None, - is_forum: bool=None + is_forum: bool=None, + max: int=1 ): super().__init__() @@ -49,3 +50,4 @@ class RequestPeerTypeChat(Object): self.is_bot_participant = is_bot_participant self.is_username = is_username self.is_forum = is_forum + self.max = max diff --git a/pyrogram/types/bots_and_keyboards/request_peer_type_user.py b/pyrogram/types/bots_and_keyboards/request_peer_type_user.py index 3be5a602..f11d3500 100644 --- a/pyrogram/types/bots_and_keyboards/request_peer_type_user.py +++ b/pyrogram/types/bots_and_keyboards/request_peer_type_user.py @@ -33,9 +33,11 @@ class RequestPeerTypeUser(Object): def __init__( self, is_bot: bool=None, - is_premium: bool=None + is_premium: bool=None, + max: int=1 ): super().__init__() self.is_bot = is_bot self.is_premium = is_premium + self.max = max diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index f88d9918..09b99b30 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -322,10 +322,10 @@ class Message(Object, Update): E.g.: "/start 1 2 3" would produce ["start", "1", "2", "3"]. Only applicable when using :obj:`~pyrogram.filters.command`. - channel_shared (``int``, *optional*): + chat_shared (List of ``int``, *optional*): Service message: chat/channel shared - user_shared (``int``, *optional*): + user_shared (List of ``int``, *optional*): Service message: user shared forum_topic_created (:obj:`~pyrogram.types.ForumTopicCreated`, *optional*): @@ -456,8 +456,8 @@ class Message(Object, Update): outgoing: bool = None, matches: List[Match] = None, command: List[str] = None, - channel_shared: int = None, - user_shared: int = None, + chat_shared: List[int] = None, + user_shared: List[int] = None, forum_topic_created: "types.ForumTopicCreated" = None, forum_topic_closed: "types.ForumTopicClosed" = None, forum_topic_reopened: "types.ForumTopicReopened" = None, @@ -556,7 +556,7 @@ class Message(Object, Update): self.matches = matches self.command = command self.reply_markup = reply_markup - self.channel_shared = channel_shared + self.chat_shared = chat_shared self.user_shared = user_shared self.forum_topic_created = forum_topic_created self.forum_topic_closed = forum_topic_closed @@ -656,7 +656,7 @@ class Message(Object, Update): group_chat_created = None channel_chat_created = None new_chat_photo = None - channel_shared = None + chat_shared = None user_shared = None is_topic_message = None forum_topic_created = None @@ -709,15 +709,18 @@ class Message(Object, Update): new_chat_photo = types.Photo._parse(client, action.photo) service_type = enums.MessageServiceType.NEW_CHAT_PHOTO elif isinstance(action, raw.types.MessageActionRequestedPeer): - if isinstance(action.peer, raw.types.PeerChannel): - channel_shared = utils.get_channel_id(utils.get_raw_peer_id(action.peer)) - service_type = enums.MessageServiceType.ChannelShared - elif isinstance(action.peer, raw.types.PeerChat): - channel_shared = utils.get_channel_id(utils.get_raw_peer_id(action.peer)) - service_type = enums.MessageServiceType.ChannelShared - elif isinstance(action.peer, raw.types.PeerUser): - user_shared = action.peer.user_id - service_type = enums.MessageServiceType.UserShared + chat_shared = [] + user_shared = [] + for peer in action.peers: + if isinstance(peer, raw.types.PeerChannel): + chat_shared.append(utils.get_channel_id(utils.get_raw_peer_id(peer))) + service_type = enums.MessageServiceType.ChannelShared + elif isinstance(peer, raw.types.PeerChat): + chat_shared.append(utils.get_channel_id(utils.get_raw_peer_id(peer))) + service_type = enums.MessageServiceType.ChannelShared + elif isinstance(peer, raw.types.PeerUser): + user_shared.append(peer.user_id) + service_type = enums.MessageServiceType.UserShared elif isinstance(action, raw.types.MessageActionTopicCreate): forum_topic_created = types.ForumTopicCreated._parse(message) service_type = enums.MessageServiceType.FORUM_TOPIC_CREATED @@ -782,8 +785,8 @@ class Message(Object, Update): migrate_from_chat_id=-migrate_from_chat_id if migrate_from_chat_id else None, group_chat_created=group_chat_created, channel_chat_created=channel_chat_created, - channel_shared=channel_shared, - user_shared=user_shared, + chat_shared=chat_shared if chat_shared is not None and len(chat_shared) > 0 else None, + user_shared=user_shared if user_shared is not None and len(user_shared) > 0 else None, is_topic_message=is_topic_message, forum_topic_created=forum_topic_created, forum_topic_closed=forum_topic_closed,