mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-29 05:18:10 +00:00
Make parse_messages accept a list of messages
This commit is contained in:
parent
65c67aaf57
commit
a1c3e40800
@ -138,14 +138,7 @@ class Dispatcher:
|
|||||||
self.dispatch(update, users=users, chats=chats, is_raw=True)
|
self.dispatch(update, users=users, chats=chats, is_raw=True)
|
||||||
|
|
||||||
if isinstance(update, Dispatcher.MESSAGE_UPDATES):
|
if isinstance(update, Dispatcher.MESSAGE_UPDATES):
|
||||||
if isinstance(update.message, types.Message):
|
message = utils.parse_messages(
|
||||||
parser = utils.parse_message
|
|
||||||
elif isinstance(update.message, types.MessageService):
|
|
||||||
parser = utils.parse_message_service
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
|
|
||||||
message = parser(
|
|
||||||
self.client,
|
self.client,
|
||||||
update.message,
|
update.message,
|
||||||
users,
|
users,
|
||||||
|
@ -218,13 +218,19 @@ def encode(s: bytes) -> str:
|
|||||||
|
|
||||||
|
|
||||||
# TODO: Reorganize code, maybe split parts as well
|
# TODO: Reorganize code, maybe split parts as well
|
||||||
def parse_message(
|
def parse_messages(
|
||||||
client,
|
client,
|
||||||
message: types.Message,
|
messages: list or types.Message or types.MessageService or types.MessageEmpty,
|
||||||
users: dict,
|
users: dict,
|
||||||
chats: dict,
|
chats: dict,
|
||||||
replies: int = 1
|
replies: int = 1
|
||||||
) -> pyrogram_types.Message:
|
) -> pyrogram_types.Message:
|
||||||
|
is_list = isinstance(messages, list)
|
||||||
|
messages = messages if is_list else [messages]
|
||||||
|
parsed_messages = []
|
||||||
|
|
||||||
|
for message in messages:
|
||||||
|
if isinstance(message, types.Message):
|
||||||
entities = parse_entities(message.entities, users)
|
entities = parse_entities(message.entities, users)
|
||||||
|
|
||||||
forward_from = None
|
forward_from = None
|
||||||
@ -540,16 +546,7 @@ def parse_message(
|
|||||||
|
|
||||||
if message.reply_to_msg_id and replies:
|
if message.reply_to_msg_id and replies:
|
||||||
m.reply_to_message = client.get_messages(m.chat.id, message.reply_to_msg_id, replies=replies - 1)
|
m.reply_to_message = client.get_messages(m.chat.id, message.reply_to_msg_id, replies=replies - 1)
|
||||||
|
elif isinstance(message, types.MessageService):
|
||||||
return m
|
|
||||||
|
|
||||||
|
|
||||||
def parse_message_service(
|
|
||||||
client,
|
|
||||||
message: types.MessageService,
|
|
||||||
users: dict,
|
|
||||||
chats: dict
|
|
||||||
) -> pyrogram_types.Message:
|
|
||||||
action = message.action
|
action = message.action
|
||||||
|
|
||||||
new_chat_members = None
|
new_chat_members = None
|
||||||
@ -640,15 +637,12 @@ def parse_message_service(
|
|||||||
|
|
||||||
if isinstance(action, types.MessageActionPinMessage):
|
if isinstance(action, types.MessageActionPinMessage):
|
||||||
m.pinned_message = client.get_messages(m.chat.id, message.reply_to_msg_id, replies=0)
|
m.pinned_message = client.get_messages(m.chat.id, message.reply_to_msg_id, replies=0)
|
||||||
|
else:
|
||||||
|
m = pyrogram_types.Message(message_id=message.id, client=proxy(client))
|
||||||
|
|
||||||
return m
|
parsed_messages.append(m)
|
||||||
|
|
||||||
|
return parsed_messages if is_list else parsed_messages[0]
|
||||||
def parse_message_empty(
|
|
||||||
client,
|
|
||||||
message: types.MessageEmpty
|
|
||||||
) -> pyrogram_types.Message:
|
|
||||||
return pyrogram_types.Message(message_id=message.id, client=proxy(client))
|
|
||||||
|
|
||||||
|
|
||||||
def get_peer_id(input_peer) -> int:
|
def get_peer_id(input_peer) -> int:
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
from pyrogram.api import functions, types
|
from pyrogram.api import functions, types
|
||||||
from ....ext import BaseClient, utils
|
from ...ext import BaseClient, utils
|
||||||
|
|
||||||
|
|
||||||
class ForwardMessages(BaseClient):
|
class ForwardMessages(BaseClient):
|
||||||
@ -79,7 +79,10 @@ class ForwardMessages(BaseClient):
|
|||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
messages.append(
|
messages.append(
|
||||||
utils.parse_message(self, i.message, users, chats)
|
utils.parse_messages(
|
||||||
|
self, i.message,
|
||||||
|
users, chats
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
return messages if is_iterable else messages[0]
|
return messages if is_iterable else messages[0]
|
||||||
|
@ -23,13 +23,35 @@ from ...ext import BaseClient, utils
|
|||||||
class GetHistory(BaseClient):
|
class GetHistory(BaseClient):
|
||||||
def get_history(self,
|
def get_history(self,
|
||||||
chat_id: int or str,
|
chat_id: int or str,
|
||||||
offset: int,
|
offset: int = 0,
|
||||||
limit: int,
|
limit: int = 100,
|
||||||
offset_id: int = 0,
|
offset_id: int = 0,
|
||||||
offset_date: int = 0,
|
offset_date: int = 0):
|
||||||
max_id: int = 0,
|
"""Use this method to retrieve the history of a chat.
|
||||||
min_id: int = 0):
|
|
||||||
# TODO: Documentation
|
You can get up to 100 messages at once.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
chat_id (``int`` | ``str``):
|
||||||
|
Unique identifier (int) or username (str) of the target chat.
|
||||||
|
For your personal cloud (Saved Messages) you can simply use "me" or "self".
|
||||||
|
For a contact that exists in your Telegram address book you can use his phone number (str).
|
||||||
|
For a private channel/supergroup you can use its *t.me/joinchat/* link.
|
||||||
|
|
||||||
|
offset (``int``, *optional*)
|
||||||
|
Sequential number of the first message to be returned.
|
||||||
|
Defaults to 0 (most recent message).
|
||||||
|
|
||||||
|
limit (``int``, *optional*):
|
||||||
|
Limits the number of messages to be retrieved.
|
||||||
|
By default, the first 100 messages are returned.
|
||||||
|
|
||||||
|
offset_id (``int``, *optional*):
|
||||||
|
Pass a message identifier as offset to retrieve only older messages starting from that message.
|
||||||
|
|
||||||
|
offset_date (``int``, *optional*):
|
||||||
|
Pass a date in Unix time as offset to retrieve only older messages starting from that date.
|
||||||
|
"""
|
||||||
|
|
||||||
r = self.send(
|
r = self.send(
|
||||||
functions.messages.GetHistory(
|
functions.messages.GetHistory(
|
||||||
@ -38,8 +60,8 @@ class GetHistory(BaseClient):
|
|||||||
offset_date=offset_date,
|
offset_date=offset_date,
|
||||||
add_offset=offset,
|
add_offset=offset,
|
||||||
limit=limit,
|
limit=limit,
|
||||||
max_id=max_id,
|
max_id=0,
|
||||||
min_id=min_id,
|
min_id=0,
|
||||||
hash=0
|
hash=0
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -47,26 +69,22 @@ class GetHistory(BaseClient):
|
|||||||
users = {i.id: i for i in r.users}
|
users = {i.id: i for i in r.users}
|
||||||
chats = {i.id: i for i in r.chats}
|
chats = {i.id: i for i in r.chats}
|
||||||
|
|
||||||
messages = []
|
reply_to_messages = {i.reply_to_msg_id: None for i in r.messages if i.reply_to_msg_id}
|
||||||
|
|
||||||
for i in r.messages:
|
if reply_to_messages:
|
||||||
if isinstance(i, types.Message):
|
temp = self.get_messages(chat_id, reply_to_messages.keys(), replies=0)
|
||||||
messages.append(
|
|
||||||
utils.parse_message(
|
assert len(temp) == len(reply_to_messages)
|
||||||
self, i, users, chats
|
|
||||||
)
|
for i in range(len(temp)):
|
||||||
)
|
reply_to_messages[temp[i].message_id] = temp[i]
|
||||||
elif isinstance(i, types.MessageService):
|
|
||||||
messages.append(
|
messages = utils.parse_messages(self, r.messages, users, chats, replies=0)
|
||||||
utils.parse_message_service(
|
|
||||||
self, i, users, chats
|
assert len(messages) == len(r.messages)
|
||||||
)
|
|
||||||
)
|
for i in range(len(messages)):
|
||||||
else:
|
if r.messages[i].reply_to_msg_id:
|
||||||
messages.append(
|
messages[i].reply_to_message = reply_to_messages[r.messages[i].reply_to_msg_id]
|
||||||
utils.parse_message_empty(
|
|
||||||
self, i
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return messages
|
return messages
|
||||||
|
@ -68,30 +68,11 @@ class GetMessages(BaseClient):
|
|||||||
|
|
||||||
r = self.send(rpc)
|
r = self.send(rpc)
|
||||||
|
|
||||||
users = {i.id: i for i in r.users}
|
messages = utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, r.messages,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
messages = []
|
{i.id: i for i in r.chats},
|
||||||
|
|
||||||
for i in r.messages:
|
|
||||||
if isinstance(i, types.Message):
|
|
||||||
messages.append(
|
|
||||||
utils.parse_message(
|
|
||||||
self, i, users, chats,
|
|
||||||
replies=replies
|
replies=replies
|
||||||
)
|
)
|
||||||
)
|
|
||||||
elif isinstance(i, types.MessageService):
|
|
||||||
messages.append(
|
|
||||||
utils.parse_message_service(
|
|
||||||
self, i, users, chats
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
messages.append(
|
|
||||||
utils.parse_message_empty(
|
|
||||||
self, i
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return messages if is_iterable else messages[0]
|
return messages if is_iterable else messages[0]
|
||||||
|
@ -176,7 +176,8 @@ class SendAudio(BaseClient):
|
|||||||
else:
|
else:
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -82,7 +82,8 @@ class SendContact(BaseClient):
|
|||||||
|
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -157,7 +157,8 @@ class SendDocument(BaseClient):
|
|||||||
else:
|
else:
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -79,7 +79,8 @@ class SendLocation(BaseClient):
|
|||||||
|
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -161,7 +161,8 @@ class SendPhoto(BaseClient):
|
|||||||
else:
|
else:
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -145,7 +145,8 @@ class SendSticker(BaseClient):
|
|||||||
else:
|
else:
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -96,7 +96,8 @@ class SendVenue(BaseClient):
|
|||||||
|
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -187,7 +187,8 @@ class SendVideo(BaseClient):
|
|||||||
else:
|
else:
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -155,7 +155,8 @@ class SendVideoNote(BaseClient):
|
|||||||
else:
|
else:
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -164,7 +164,8 @@ class SendVoice(BaseClient):
|
|||||||
else:
|
else:
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
from pyrogram.api import functions, types
|
from pyrogram.api import functions, types
|
||||||
from pyrogram.client import types as pyrogram_types
|
from pyrogram.client import types as pyrogram_types
|
||||||
from ....ext import utils, BaseClient
|
from ...ext import utils, BaseClient
|
||||||
|
|
||||||
|
|
||||||
class SendMessage(BaseClient):
|
class SendMessage(BaseClient):
|
||||||
@ -91,7 +91,8 @@ class SendMessage(BaseClient):
|
|||||||
|
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -69,7 +69,8 @@ class EditMessageCaption(BaseClient):
|
|||||||
|
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)):
|
if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -58,7 +58,8 @@ class EditMessageReplyMarkup(BaseClient):
|
|||||||
|
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)):
|
if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
@ -74,7 +74,8 @@ class EditMessageText(BaseClient):
|
|||||||
|
|
||||||
for i in r.updates:
|
for i in r.updates:
|
||||||
if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)):
|
if isinstance(i, (types.UpdateEditMessage, types.UpdateEditChannelMessage)):
|
||||||
users = {i.id: i for i in r.users}
|
return utils.parse_messages(
|
||||||
chats = {i.id: i for i in r.chats}
|
self, i.message,
|
||||||
|
{i.id: i for i in r.users},
|
||||||
return utils.parse_message(self, i.message, users, chats)
|
{i.id: i for i in r.chats}
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user