Skip to content

Commit ae985b2

Browse files
committed
pyrofork: fix MESSAGE_IDS_EMPTY error on get_scheduled_messages method
Signed-off-by: wulan17 <wulan17@komodos.id>
1 parent 2992b6c commit ae985b2

File tree

2 files changed

+70
-57
lines changed

2 files changed

+70
-57
lines changed

pyrogram/methods/messages/get_scheduled_messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ async def get_scheduled_messages(
7878

7979
r = await self.invoke(rpc, sleep_threshold=-1)
8080

81-
messages = await utils.parse_messages(self, r)
81+
messages = await utils.parse_messages(self, r, is_scheduled=True)
8282

8383
return messages if is_iterable else messages[0] if messages else None

pyrogram/utils.py

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ async def parse_messages(
103103
client,
104104
messages: "raw.types.messages.Messages",
105105
replies: int = 1,
106-
business_connection_id: str = None
106+
business_connection_id: str = None,
107+
is_scheduled: bool = False
107108
) -> List["types.Message"]:
108109
users = {i.id: i for i in messages.users}
109110
chats = {i.id: i for i in messages.chats}
@@ -120,64 +121,76 @@ async def parse_messages(
120121
parsed_messages.append(await types.Message._parse(client, message, users, chats, topics, replies=0, business_connection_id=business_connection_id))
121122

122123
if replies:
123-
messages_with_replies = {
124-
i.id: i.reply_to.reply_to_msg_id
125-
for i in messages.messages
126-
if (
127-
not isinstance(i, raw.types.MessageEmpty)
128-
and i.reply_to
129-
and isinstance(i.reply_to, raw.types.MessageReplyHeader)
130-
and i.reply_to.reply_to_msg_id is not None
131-
)
132-
}
133-
134-
message_reply_to_story = {
135-
i.id: {'user_id': i.reply_to.user_id, 'story_id': i.reply_to.story_id}
136-
for i in messages.messages
137-
if not isinstance(i, raw.types.MessageEmpty) and i.reply_to and isinstance(i.reply_to, raw.types.MessageReplyStoryHeader)
138-
}
139-
140-
if messages_with_replies:
141-
# We need a chat id, but some messages might be empty (no chat attribute available)
142-
# Scan until we find a message with a chat available (there must be one, because we are fetching replies)
143-
for m in parsed_messages:
144-
if m.chat:
145-
chat_id = m.chat.id
146-
break
147-
else:
148-
chat_id = 0
149-
150-
reply_messages = await client.get_messages(
151-
chat_id,
152-
reply_to_message_ids=messages_with_replies.keys(),
153-
replies=replies - 1
154-
)
155-
156-
for message in parsed_messages:
157-
reply_id = messages_with_replies.get(message.id, None)
158-
159-
for reply in reply_messages:
160-
if reply.id == reply_id:
161-
if not reply.forum_topic_created:
162-
message.reply_to_message = reply
163-
if message_reply_to_story:
164-
for m in parsed_messages:
165-
if m.chat:
166-
chat_id = m.chat.id
167-
break
168-
else:
169-
chat_id = 0
170-
171-
reply_messages = {}
172-
for msg_id in message_reply_to_story.keys():
173-
reply_messages[msg_id] = await client.get_stories(
174-
message_reply_to_story[msg_id]['user_id'],
175-
message_reply_to_story[msg_id]['story_id']
124+
if not is_scheduled:
125+
messages_with_replies = {
126+
i.id: i.reply_to.reply_to_msg_id
127+
for i in messages.messages
128+
if (
129+
not isinstance(i, raw.types.MessageEmpty)
130+
and i.reply_to
131+
and isinstance(i.reply_to, raw.types.MessageReplyHeader)
132+
and i.reply_to.reply_to_msg_id is not None
133+
)
134+
}
135+
136+
message_reply_to_story = {
137+
i.id: {'user_id': i.reply_to.user_id, 'story_id': i.reply_to.story_id}
138+
for i in messages.messages
139+
if not isinstance(i, raw.types.MessageEmpty) and i.reply_to and isinstance(i.reply_to, raw.types.MessageReplyStoryHeader)
140+
}
141+
142+
if messages_with_replies:
143+
# We need a chat id, but some messages might be empty (no chat attribute available)
144+
# Scan until we find a message with a chat available (there must be one, because we are fetching replies)
145+
for m in parsed_messages:
146+
if m.chat:
147+
chat_id = m.chat.id
148+
break
149+
else:
150+
chat_id = 0
151+
152+
reply_messages = await client.get_messages(
153+
chat_id,
154+
reply_to_message_ids=messages_with_replies.keys(),
155+
replies=replies - 1
176156
)
177157

158+
for message in parsed_messages:
159+
reply_id = messages_with_replies.get(message.id, None)
160+
161+
for reply in reply_messages:
162+
if reply.id == reply_id:
163+
if not reply.forum_topic_created:
164+
message.reply_to_message = reply
165+
if message_reply_to_story:
166+
for m in parsed_messages:
167+
if m.chat:
168+
chat_id = m.chat.id
169+
break
170+
else:
171+
chat_id = 0
172+
173+
reply_messages = {}
174+
for msg_id in message_reply_to_story.keys():
175+
reply_messages[msg_id] = await client.get_stories(
176+
message_reply_to_story[msg_id]['user_id'],
177+
message_reply_to_story[msg_id]['story_id']
178+
)
179+
180+
for message in parsed_messages:
181+
if message.id in reply_messages:
182+
message.reply_to_story = reply_messages[message.id]
183+
else:
178184
for message in parsed_messages:
179-
if message.id in reply_messages:
180-
message.reply_to_story = reply_messages[message.id]
185+
if (
186+
message.reply_to_message_id
187+
and not message.external_reply
188+
):
189+
message.reply_to_message = await client.get_messages(
190+
message.chat.id,
191+
message_ids=message.reply_to_message_id,
192+
replies=replies - 1
193+
)
181194

182195
return types.List(parsed_messages)
183196

0 commit comments

Comments
 (0)