Skip to content

Commit

Permalink
Add support for t.me/m links in get_messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SpEcHiDe committed Apr 5, 2024
1 parent 2667461 commit c6b9b70
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ it can take advantage of new goodies!
| Scheme layer used: 177 |
+------------------------+

- Added support for ``https://t.me/m/blah`` links in the ``link`` parameter of :meth:`~pyrogram.Client.get_messages`
- Added the parameter ``message_thread_id`` to the :meth:`~pyrogram.Client.search_messages` and :meth:`~pyrogram.Client.search_messages_count`.
- Added the parameter ``chat_list`` to the :meth:`~pyrogram.Client.search_global` and :meth:`~pyrogram.Client.search_global_count`.
- PR from upstream: `1411 <https://github.com/pyrogram/pyrogram/pull/1411>`_ without attribution.
Expand Down
45 changes: 45 additions & 0 deletions pyrogram/methods/messages/get_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import pyrogram
from pyrogram import raw, types, utils
from pyrogram.types.messages_and_media.message import Str

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -163,10 +164,54 @@ async def get_messages(
raw_chat_id = linkps[3]
message_thread_id = int(linkps[4])
message_id = int(linkps[5])

elif (
not self.me.is_bot and
len(linkps) == 5 and
linkps[3] == "m"
):
r = await self.invoke(
raw.functions.account.ResolveBusinessChatLink(
slug=linkps[4]
)
)
users = {i.id: i for i in r.users}
# chats = {i.id: i for i in r.chats}
entities = [
types.MessageEntity._parse(
self, entity, users
)
for entity in getattr(r, "entities", [])
]
entities = types.List(
filter(lambda x: x is not None, entities)
)
sender_chat = None
cat_id = utils.get_raw_peer_id(r.peer)
if isinstance(r.peer, raw.types.PeerUser):
sender_chat = types.Chat._parse_user_chat(self, users[cat_id])
# elif isinstance(r.peer, raw.types.PeerChat):
# sender_chat = types.Chat._parse_chat_chat(self, chats[cat_id])
# else:
# sender_chat = types.Chat._parse_channel_chat(
# self, chats[cat_id]
# )
return types.Message(
id=0, # TODO modify this later with a Draft type
text=Str(r.message).init(entities) or None,
entities=entities or None,
sender_chat=sender_chat,
)

elif len(linkps) == 5:
# https://t.me/pyrogramchat/609282
raw_chat_id = linkps[3]
if raw_chat_id == "m":
raise ValueError(
"Invalid ClientType used to parse this message link"
)
message_id = int(linkps[4])

return await self.get_messages(
chat_id=raw_chat_id,
message_ids=message_id
Expand Down

0 comments on commit c6b9b70

Please sign in to comment.