From 6f8aaa449cc5b901f4c817432f863dc45143741f Mon Sep 17 00:00:00 2001 From: wulan17 Date: Thu, 10 Nov 2022 23:34:18 +0700 Subject: [PATCH] Pyrogram: Add message_thread_id and is_topic_message fields to Message class, and is_forum fields to Chat class Signed-off-by: wulan17 --- pyrogram/types/messages_and_media/message.py | 21 ++++++++++++++++++++ pyrogram/types/user_and_chats/chat.py | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index d844b060..1dfc81d4 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -63,6 +63,9 @@ class Message(Object, Update): id (``int``): Unique message identifier inside this chat. + message_thread_id (``int``, *optional*): + Unique identifier of a message thread to which the message belongs; for supergroups only + from_user (:obj:`~pyrogram.types.User`, *optional*): Sender, empty for messages sent to channels. @@ -96,6 +99,9 @@ class Message(Object, Update): forward_date (:py:obj:`~datetime.datetime`, *optional*): For forwarded messages, date the original message was sent. + is_topic_message (``bool``, *optional*): + True, if the message is sent to a forum topic + reply_to_message_id (``int``, *optional*): The id of the message which this message directly replied to. @@ -309,6 +315,7 @@ class Message(Object, Update): *, client: "pyrogram.Client" = None, id: int, + message_thread_id: int = None, from_user: "types.User" = None, sender_chat: "types.Chat" = None, date: datetime = None, @@ -319,6 +326,7 @@ class Message(Object, Update): forward_from_message_id: int = None, forward_signature: str = None, forward_date: datetime = None, + is_topic_message: bool = None, reply_to_message_id: int = None, reply_to_top_message_id: int = None, reply_to_message: "Message" = None, @@ -385,6 +393,7 @@ class Message(Object, Update): super().__init__(client) self.id = id + self.message_thread_id = message_thread_id self.from_user = from_user self.sender_chat = sender_chat self.date = date @@ -395,6 +404,7 @@ class Message(Object, Update): self.forward_from_message_id = forward_from_message_id self.forward_signature = forward_signature self.forward_date = forward_date + self.is_topic_message = is_topic_message self.reply_to_message_id = reply_to_message_id self.reply_to_top_message_id = reply_to_top_message_id self.reply_to_message = reply_to_message @@ -612,6 +622,7 @@ class Message(Object, Update): return parsed_message if isinstance(message, raw.types.Message): + message_thread_id = None entities = [types.MessageEntity._parse(client, entity, users) for entity in message.entities] entities = types.List(filter(lambda x: x is not None, entities)) @@ -621,6 +632,7 @@ class Message(Object, Update): forward_from_message_id = None forward_signature = None forward_date = None + is_topic_message = None forward_header = message.fwd_from # type: raw.types.MessageFwdHeader @@ -751,6 +763,7 @@ class Message(Object, Update): parsed_message = Message( id=message.id, + message_thread_id=message_thread_id, date=utils.timestamp_to_datetime(message.date), chat=types.Chat._parse(client, message, users, chats, is_chat=True), from_user=from_user, @@ -783,6 +796,7 @@ class Message(Object, Update): forward_from_message_id=forward_from_message_id, forward_signature=forward_signature, forward_date=forward_date, + is_topic_message=is_topic_message, mentioned=message.mentioned, scheduled=is_scheduled, from_scheduled=message.from_scheduled, @@ -817,6 +831,13 @@ class Message(Object, Update): parsed_message.reply_to_message_id = message.reply_to.reply_to_msg_id parsed_message.reply_to_top_message_id = message.reply_to.reply_to_top_id + if message.reply_to.forum_topic: + if message.reply_to.reply_to_top_id: + parsed_message.message_thread_id = message.reply_to.reply_to_top_id + else: + parsed_message.message_thread_id = message.reply_to.reply_to_msg_id + parsed_message.is_topic_message = True + if replies: try: key = (parsed_message.chat.id, parsed_message.reply_to_message_id) diff --git a/pyrogram/types/user_and_chats/chat.py b/pyrogram/types/user_and_chats/chat.py index f37542fb..5b4114f6 100644 --- a/pyrogram/types/user_and_chats/chat.py +++ b/pyrogram/types/user_and_chats/chat.py @@ -55,6 +55,9 @@ class Chat(Object): is_support (``bool``): True, if this chat is part of the Telegram support team. Users and bots only. + is_forum (``bool``, *optional*): + True, if the supergroup chat is a forum + title (``str``, *optional*): Title, for supergroups, channels and basic group chats. @@ -143,6 +146,7 @@ class Chat(Object): is_scam: bool = None, is_fake: bool = None, is_support: bool = None, + is_forum: bool = None, title: str = None, username: str = None, first_name: str = None, @@ -174,6 +178,7 @@ class Chat(Object): self.is_scam = is_scam self.is_fake = is_fake self.is_support = is_support + self.is_forum = is_forum self.title = title self.username = username self.first_name = first_name @@ -246,6 +251,7 @@ class Chat(Object): is_creator=getattr(channel, "creator", None), is_scam=getattr(channel, "scam", None), is_fake=getattr(channel, "fake", None), + is_forum=getattr(channel, "forum", None), title=channel.title, username=getattr(channel, "username", None), photo=types.ChatPhoto._parse(client, getattr(channel, "photo", None), peer_id,