From 99e6005cf10c4850502f389d07ad5ec50fbdf474 Mon Sep 17 00:00:00 2001 From: wulan17 Date: Thu, 15 May 2025 23:01:20 +0700 Subject: [PATCH] pyrofork: fix MESSAGE_IDS_EMPTY error on get_scheduled_messages method Signed-off-by: wulan17 --- .../messages/get_scheduled_messages.py | 2 +- pyrogram/utils.py | 119 ++++++++++-------- 2 files changed, 67 insertions(+), 54 deletions(-) diff --git a/pyrogram/methods/messages/get_scheduled_messages.py b/pyrogram/methods/messages/get_scheduled_messages.py index 532627d1..0623944a 100644 --- a/pyrogram/methods/messages/get_scheduled_messages.py +++ b/pyrogram/methods/messages/get_scheduled_messages.py @@ -78,6 +78,6 @@ class GetScheduledMessages: r = await self.invoke(rpc, sleep_threshold=-1) - messages = await utils.parse_messages(self, r) + messages = await utils.parse_messages(self, r, is_scheduled=True) return messages if is_iterable else messages[0] if messages else None diff --git a/pyrogram/utils.py b/pyrogram/utils.py index 09e3a7ed..4ad6c5c2 100644 --- a/pyrogram/utils.py +++ b/pyrogram/utils.py @@ -103,7 +103,8 @@ async def parse_messages( client, messages: "raw.types.messages.Messages", replies: int = 1, - business_connection_id: str = None + business_connection_id: str = None, + is_scheduled: bool = False ) -> List["types.Message"]: users = {i.id: i for i in messages.users} chats = {i.id: i for i in messages.chats} @@ -120,64 +121,76 @@ async def parse_messages( parsed_messages.append(await types.Message._parse(client, message, users, chats, topics, replies=0, business_connection_id=business_connection_id)) if replies: - messages_with_replies = { - i.id: i.reply_to.reply_to_msg_id - for i in messages.messages - if ( - not isinstance(i, raw.types.MessageEmpty) - and i.reply_to - and isinstance(i.reply_to, raw.types.MessageReplyHeader) - and i.reply_to.reply_to_msg_id is not None - ) - } + if not is_scheduled: + messages_with_replies = { + i.id: i.reply_to.reply_to_msg_id + for i in messages.messages + if ( + not isinstance(i, raw.types.MessageEmpty) + and i.reply_to + and isinstance(i.reply_to, raw.types.MessageReplyHeader) + and i.reply_to.reply_to_msg_id is not None + ) + } - message_reply_to_story = { - i.id: {'user_id': i.reply_to.user_id, 'story_id': i.reply_to.story_id} - for i in messages.messages - if not isinstance(i, raw.types.MessageEmpty) and i.reply_to and isinstance(i.reply_to, raw.types.MessageReplyStoryHeader) - } + message_reply_to_story = { + i.id: {'user_id': i.reply_to.user_id, 'story_id': i.reply_to.story_id} + for i in messages.messages + if not isinstance(i, raw.types.MessageEmpty) and i.reply_to and isinstance(i.reply_to, raw.types.MessageReplyStoryHeader) + } - if messages_with_replies: - # We need a chat id, but some messages might be empty (no chat attribute available) - # Scan until we find a message with a chat available (there must be one, because we are fetching replies) - for m in parsed_messages: - if m.chat: - chat_id = m.chat.id - break - else: - chat_id = 0 + if messages_with_replies: + # We need a chat id, but some messages might be empty (no chat attribute available) + # Scan until we find a message with a chat available (there must be one, because we are fetching replies) + for m in parsed_messages: + if m.chat: + chat_id = m.chat.id + break + else: + chat_id = 0 - reply_messages = await client.get_messages( - chat_id, - reply_to_message_ids=messages_with_replies.keys(), - replies=replies - 1 - ) - - for message in parsed_messages: - reply_id = messages_with_replies.get(message.id, None) - - for reply in reply_messages: - if reply.id == reply_id: - if not reply.forum_topic_created: - message.reply_to_message = reply - if message_reply_to_story: - for m in parsed_messages: - if m.chat: - chat_id = m.chat.id - break - else: - chat_id = 0 - - reply_messages = {} - for msg_id in message_reply_to_story.keys(): - reply_messages[msg_id] = await client.get_stories( - message_reply_to_story[msg_id]['user_id'], - message_reply_to_story[msg_id]['story_id'] + reply_messages = await client.get_messages( + chat_id, + reply_to_message_ids=messages_with_replies.keys(), + replies=replies - 1 ) + for message in parsed_messages: + reply_id = messages_with_replies.get(message.id, None) + + for reply in reply_messages: + if reply.id == reply_id: + if not reply.forum_topic_created: + message.reply_to_message = reply + if message_reply_to_story: + for m in parsed_messages: + if m.chat: + chat_id = m.chat.id + break + else: + chat_id = 0 + + reply_messages = {} + for msg_id in message_reply_to_story.keys(): + reply_messages[msg_id] = await client.get_stories( + message_reply_to_story[msg_id]['user_id'], + message_reply_to_story[msg_id]['story_id'] + ) + + for message in parsed_messages: + if message.id in reply_messages: + message.reply_to_story = reply_messages[message.id] + else: for message in parsed_messages: - if message.id in reply_messages: - message.reply_to_story = reply_messages[message.id] + if ( + message.reply_to_message_id + and not message.external_reply + ): + message.reply_to_message = await client.get_messages( + message.chat.id, + message_ids=message.reply_to_message_id, + replies=replies - 1 + ) return types.List(parsed_messages)