From af129ba5a7a8cc351c649a242f08950aface9882 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Mon, 27 Jan 2025 13:49:02 +0100 Subject: [PATCH 1/8] Try to make get_messages more complicated :( --- pyrogram/methods/messages/get_messages.py | 22 ++++++++++++-- pyrogram/types/messages_and_media/message.py | 2 +- pyrogram/types/user_and_chats/chat.py | 30 ++++++-------------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/pyrogram/methods/messages/get_messages.py b/pyrogram/methods/messages/get_messages.py index 81899c5150..955c527bec 100644 --- a/pyrogram/methods/messages/get_messages.py +++ b/pyrogram/methods/messages/get_messages.py @@ -33,6 +33,7 @@ async def get_messages( chat_id: Union[int, str] = None, message_ids: Union[int, Iterable[int]] = None, reply_to_message_ids: Union[int, Iterable[int]] = None, + pinned: bool = False, replies: int = 1, is_scheduled: bool = False, link: str = None, @@ -45,7 +46,7 @@ async def get_messages( .. include:: /_includes/usable-by/users-bots.rst - You must use exactly one of ``message_ids`` OR (``chat_id``, ``message_ids``) OR (``chat_id``, ``reply_to_message_ids``) OR ``link``. + You must use exactly one of ``message_ids`` OR ``reply_to_message_ids`` OR (``chat_id``, ``message_ids``) OR (``chat_id``, ``reply_to_message_ids``) OR (``chat_id``, ``pinned``) OR ``link``. Parameters: chat_id (``int`` | ``str``, *optional*): @@ -62,6 +63,10 @@ async def get_messages( the previous message you replied to using this message. If *message_ids* is set, this argument will be ignored. + pinned (``bool``, *optional*): + Returns information about the newest pinned message in the specified ``chat_id``. + Use :meth:`~pyrogram.Client.search_messages` to return all the pinned messages. + replies (``int``, *optional*): The number of subsequent replies to get for each message. Pass 0 for no reply at all or -1 for unlimited replies. @@ -69,6 +74,7 @@ async def get_messages( is_scheduled (``bool``, *optional*): Whether to get scheduled messages. Defaults to False. + Only supported if both ``chat_id`` and ``message_ids`` are passed. Other parameters are ignored when this is set. link (``str``): A link of the message, usually can be copied using ``Copy Link`` functionality OR obtained using :obj:`~pyrogram.raw.types.Message.link` OR :obj:`~pyrogram.raw.functions.channels.ExportMessageLink` @@ -113,7 +119,7 @@ async def get_messages( ) return messages if is_iterable else messages[0] if messages else None - if chat_id: + if chat_id and (message_ids or reply_to_message_ids): ids, ids_type = ( (message_ids, raw.types.InputMessageID) if message_ids else (reply_to_message_ids, raw.types.InputMessageReplyTo) if reply_to_message_ids @@ -154,6 +160,18 @@ async def get_messages( return messages if is_iterable else messages[0] if messages else None + if chat_id and pinned: + peer = await self.resolve_peer(chat_id) + rpc = raw.functions.channels.GetMessages(channel=peer, id=[raw.types.InputMessagePinned()]) + r = await self.invoke(rpc, sleep_threshold=-1) + messages = await utils.parse_messages( + self, + r, + is_scheduled=False, + replies=replies + ) + return messages[0] if messages else None + if link: linkps = link.split("/") raw_chat_id, message_thread_id, message_id = None, None, None diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index f23fb8b0a9..864afdb968 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -1069,7 +1069,7 @@ async def _parse( try: parsed_message.pinned_message = await client.get_messages( chat_id=parsed_message.chat.id, - reply_to_message_ids=message.id, + pinned=True, replies=0 ) parsed_message.service = enums.MessageServiceType.PINNED_MESSAGE diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index bc47523eda..6cc88ed4b3 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -588,17 +588,10 @@ async def _parse_full(client, chat_full: Union[raw.types.messages.ChatFull, raw. ) if full_user.pinned_msg_id: - try: - parsed_chat.pinned_message = await client.get_messages( - chat_id=parsed_chat.id, - message_ids=full_user.pinned_msg_id - ) - except MessageIdsEmpty: - parsed_chat.pinned_message = types.Message( - id=full_user.pinned_msg_id, - empty=True, - client=client - ) + parsed_chat.pinned_message = await client.get_messages( + chat_id=parsed_chat.id, + pinned=True + ) if getattr(full_user, "birthday", None): parsed_chat.birthdate = types.Birthdate._parse( @@ -679,17 +672,10 @@ async def _parse_full(client, chat_full: Union[raw.types.messages.ChatFull, raw. parsed_chat.message_auto_delete_time = getattr(full_chat, "ttl_period") if full_chat.pinned_msg_id: - try: - parsed_chat.pinned_message = await client.get_messages( - chat_id=parsed_chat.id, - message_ids=full_chat.pinned_msg_id - ) - except MessageIdsEmpty: - parsed_chat.pinned_message = types.Message( - id=full_chat.pinned_msg_id, - empty=True, - client=client - ) + parsed_chat.pinned_message = await client.get_messages( + chat_id=parsed_chat.id, + pinned=True + ) if isinstance(full_chat.exported_invite, raw.types.ChatInviteExported): parsed_chat.invite_link = full_chat.exported_invite.link From 0474728d540c62866917bfe2743ce75bfbdcca2d Mon Sep 17 00:00:00 2001 From: Shrimadhav U K Date: Fri, 31 Jan 2025 15:22:53 +0530 Subject: [PATCH 2/8] try changing conditions --- pyrogram/methods/messages/get_messages.py | 27 +++++------------------ 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/pyrogram/methods/messages/get_messages.py b/pyrogram/methods/messages/get_messages.py index 955c527bec..b125d011c4 100644 --- a/pyrogram/methods/messages/get_messages.py +++ b/pyrogram/methods/messages/get_messages.py @@ -105,46 +105,29 @@ async def get_messages( ValueError: In case of invalid arguments. """ - if not chat_id and message_ids: - is_iterable = not isinstance(message_ids, int) - ids = list(message_ids) if is_iterable else [message_ids] - ids = [raw.types.InputMessageID(id=i) for i in ids] - rpc = raw.functions.messages.GetMessages(id=ids) - r = await self.invoke(rpc, sleep_threshold=-1) - messages = await utils.parse_messages( - self, - r, - is_scheduled=is_scheduled, - replies=replies - ) - return messages if is_iterable else messages[0] if messages else None - - if chat_id and (message_ids or reply_to_message_ids): + if message_ids or reply_to_message_ids: ids, ids_type = ( (message_ids, raw.types.InputMessageID) if message_ids else (reply_to_message_ids, raw.types.InputMessageReplyTo) if reply_to_message_ids else (None, None) ) - if ids is None: - raise ValueError("No argument supplied. Either pass message_ids or reply_to_message_ids") - - peer = await self.resolve_peer(chat_id) - is_iterable = not isinstance(ids, int) ids = list(ids) if is_iterable else [ids] if replies < 0: replies = (1 << 31) - 1 - if is_scheduled: + peer = await self.resolve_peer(chat_id) if chat_id else None + + if chat_id and is_scheduled: rpc = raw.functions.messages.GetScheduledMessages( peer=peer, id=ids ) else: ids = [ids_type(id=i) for i in ids] - if isinstance(peer, raw.types.InputPeerChannel): + if chat_id and isinstance(peer, raw.types.InputPeerChannel): rpc = raw.functions.channels.GetMessages(channel=peer, id=ids) else: rpc = raw.functions.messages.GetMessages(id=ids) From f4429eef80abfb57ae221f09b625fd7c8bde7548 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 6 Feb 2025 16:32:58 +0100 Subject: [PATCH 3/8] fixes remove tryexcept --- pyrogram/types/messages_and_media/message.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 864afdb968..949472b9cb 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -1066,15 +1066,12 @@ async def _parse( ) if isinstance(action, raw.types.MessageActionPinMessage): - try: - parsed_message.pinned_message = await client.get_messages( - chat_id=parsed_message.chat.id, - pinned=True, - replies=0 - ) - parsed_message.service = enums.MessageServiceType.PINNED_MESSAGE - except MessageIdsEmpty: - pass + parsed_message.pinned_message = await client.get_messages( + chat_id=parsed_message.chat.id, + pinned=True, + replies=0 + ) + parsed_message.service = enums.MessageServiceType.PINNED_MESSAGE if isinstance(action, raw.types.MessageActionGameScore): parsed_message.game_high_score = types.GameHighScore._parse_action(client, message, users) From 07e873b52e10038844d9e1b1fb384140700e6796 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 6 Feb 2025 16:36:22 +0100 Subject: [PATCH 4/8] minor fixes --- pyrogram/session/session.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 5df270bdae..86d8e93d7e 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -396,6 +396,9 @@ async def invoke( timeout: float = WAIT_TIMEOUT, sleep_threshold: float = SLEEP_THRESHOLD ): + if sleep_threshold < 0: + # TODO: remove this later + sleep_threshold = (1 << 31) - 1 sleep_threshold = max(sleep_threshold, self.client.sleep_threshold) try: From 82e7f0b66ca78c5b023cd7f2a115f4d290bbb084 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 6 Feb 2025 16:43:34 +0100 Subject: [PATCH 5/8] documentation update --- pyrogram/methods/messages/get_messages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrogram/methods/messages/get_messages.py b/pyrogram/methods/messages/get_messages.py index b125d011c4..df59096882 100644 --- a/pyrogram/methods/messages/get_messages.py +++ b/pyrogram/methods/messages/get_messages.py @@ -61,16 +61,16 @@ async def get_messages( reply_to_message_ids (``int`` | Iterable of ``int``, *optional*): Pass a single message identifier or an iterable of message ids (as integers) to get the content of the previous message you replied to using this message. - If *message_ids* is set, this argument will be ignored. pinned (``bool``, *optional*): - Returns information about the newest pinned message in the specified ``chat_id``. + Returns information about the newest pinned message in the specified ``chat_id``. Other parameters are ignored when this is set. Use :meth:`~pyrogram.Client.search_messages` to return all the pinned messages. replies (``int``, *optional*): The number of subsequent replies to get for each message. Pass 0 for no reply at all or -1 for unlimited replies. Defaults to 1. + Is ignored if ``is_scheduled`` parameter is set. is_scheduled (``bool``, *optional*): Whether to get scheduled messages. Defaults to False. From a4f7d3d2e21bef00431caf7b5de1df65b830bd31 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 6 Feb 2025 16:49:57 +0100 Subject: [PATCH 6/8] (fix): SuccessfulPayment documentation --- pyrogram/types/business/successful_payment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyrogram/types/business/successful_payment.py b/pyrogram/types/business/successful_payment.py index 470548702d..a619b5cd92 100644 --- a/pyrogram/types/business/successful_payment.py +++ b/pyrogram/types/business/successful_payment.py @@ -26,7 +26,7 @@ class SuccessfulPayment(Object): - """This object contains basic information about a successful payment. + """This object contains basic information about a successful payment. Note that if the buyer initiates a chargeback with the relevant payment provider following this transaction, the funds may be debited from your balance. This is outside of Telegram's control. Parameters: currency (``str``): From 9973e4ad84db5a417ce54be688b18152dbd7adf6 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 6 Feb 2025 16:51:20 +0100 Subject: [PATCH 7/8] mbno --- pyrogram/session/session.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py index 86d8e93d7e..5df270bdae 100644 --- a/pyrogram/session/session.py +++ b/pyrogram/session/session.py @@ -396,9 +396,6 @@ async def invoke( timeout: float = WAIT_TIMEOUT, sleep_threshold: float = SLEEP_THRESHOLD ): - if sleep_threshold < 0: - # TODO: remove this later - sleep_threshold = (1 << 31) - 1 sleep_threshold = max(sleep_threshold, self.client.sleep_threshold) try: From 29dd30a2736df8bd827a304750a060b03b53d933 Mon Sep 17 00:00:00 2001 From: shriMADhav U k Date: Thu, 6 Feb 2025 16:54:12 +0100 Subject: [PATCH 8/8] docs --- docs/source/releases/changes-in-this-fork.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/source/releases/changes-in-this-fork.rst b/docs/source/releases/changes-in-this-fork.rst index d1449d103a..cbe6ded8b8 100644 --- a/docs/source/releases/changes-in-this-fork.rst +++ b/docs/source/releases/changes-in-this-fork.rst @@ -26,6 +26,7 @@ Changes in this Fork | Scheme layer used: 198 | +------------------------+ +- Added the parameter ``pinned`` and made the parameter ``chat_id`` optional in :meth:`~pyrogram.Client.get_messages`. **NOTE**: Please be aware about using the correct :doc:`Message Identifiers <../../topics/message-identifiers>`, when using this method. - Added the ``cover`` and ``start_timestamp`` parameters in :meth:`~pyrogram.Client.send_video` and :obj:`~pyrogram.types.InputPaidMediaVideo`. - Added the ``new_video_start_timestamp`` and renamed the ``send_copy`` and ``remove_caption`` parameters in :meth:`~pyrogram.Client.forward_messages` and :meth:`~pyrogram.types.Message.forward`. - Added the ``gift_count`` to the :obj:`~pyrogram.types.Chat`.