From d1bdaae81de0f73e8b0c37bed69955a74ba939bd Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Apr 2022 11:56:07 +0200 Subject: [PATCH] Add methods related to discussion threads and comments --- compiler/docs/compiler.py | 2 + pyrogram/methods/messages/__init__.py | 6 +- .../messages/get_discussion_message.py | 5 +- .../messages/get_discussion_replies.py | 84 +++++++++++++++++++ .../messages/get_discussion_replies_count.py | 60 +++++++++++++ 5 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 pyrogram/methods/messages/get_discussion_replies.py create mode 100644 pyrogram/methods/messages/get_discussion_replies_count.py diff --git a/compiler/docs/compiler.py b/compiler/docs/compiler.py index 78231f7d..3357d911 100644 --- a/compiler/docs/compiler.py +++ b/compiler/docs/compiler.py @@ -185,6 +185,8 @@ def pyrogram_api(): search_global_count download_media get_discussion_message + get_discussion_replies + get_discussion_replies_count """, chats=""" Chats diff --git a/pyrogram/methods/messages/__init__.py b/pyrogram/methods/messages/__init__.py index 18cd65e3..62509d9e 100644 --- a/pyrogram/methods/messages/__init__.py +++ b/pyrogram/methods/messages/__init__.py @@ -32,6 +32,8 @@ from .forward_messages import ForwardMessages from .get_chat_history import GetChatHistory from .get_chat_history_count import GetChatHistoryCount from .get_discussion_message import GetDiscussionMessage +from .get_discussion_replies import GetDiscussionReplies +from .get_discussion_replies_count import GetDiscussionRepliesCount from .get_history import GetHistory from .get_media_group import GetMediaGroup from .get_messages import GetMessages @@ -108,6 +110,8 @@ class Messages( SearchMessagesCount, SearchGlobalCount, GetDiscussionMessage, - SendReaction + SendReaction, + GetDiscussionReplies, + GetDiscussionRepliesCount ): pass diff --git a/pyrogram/methods/messages/get_discussion_message.py b/pyrogram/methods/messages/get_discussion_message.py index 6c72b484..cbcc7bfe 100644 --- a/pyrogram/methods/messages/get_discussion_message.py +++ b/pyrogram/methods/messages/get_discussion_message.py @@ -29,9 +29,10 @@ class GetDiscussionMessage: chat_id: Union[int, str], message_id: int, ) -> "types.Message": - """Get the discussion message from the linked discussion group of a channel post. + """Get the first discussion message of a channel post or a discussion thread in a group. - Reply to the returned message to leave a comment on the linked channel post. + Reply to the returned message to leave a comment on the linked channel post or to continue + the discussion thread. Parameters: chat_id (``int`` | ``str``): diff --git a/pyrogram/methods/messages/get_discussion_replies.py b/pyrogram/methods/messages/get_discussion_replies.py new file mode 100644 index 00000000..b1279de5 --- /dev/null +++ b/pyrogram/methods/messages/get_discussion_replies.py @@ -0,0 +1,84 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import Union, Optional, AsyncGenerator + +import pyrogram +from pyrogram import types, raw + + +class GetDiscussionReplies: + async def get_discussion_replies( + self: "pyrogram.Client", + chat_id: Union[int, str], + message_id: int, + limit: int = 0, + ) -> Optional[AsyncGenerator["types.Message", None]]: + """Get the message replies of a discussion thread. + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + message_id (``int``): + Message id. + + limit (``int``, *optional*): + Limits the number of messages to be retrieved. + By default, no limit is applied and all messages are returned. + + Example: + .. code-block:: python + + for m in app.get_discussion_replies(chat_id, message_id): + print(m) + """ + + current = 0 + total = limit or (1 << 31) - 1 + limit = min(100, total) + + while True: + r = await self.invoke( + raw.functions.messages.GetReplies( + peer=await self.resolve_peer(chat_id), + msg_id=message_id, + offset_id=0, + offset_date=0, + add_offset=current, + limit=limit, + max_id=0, + min_id=0, + hash=0 + ) + ) + + users = {u.id: u for u in r.users} + chats = {c.id: c for c in r.chats} + messages = r.messages + + if not messages: + return + + for message in messages: + yield await types.Message._parse(self, message, users, chats, replies=0) + + current += 1 + + if current >= total: + return diff --git a/pyrogram/methods/messages/get_discussion_replies_count.py b/pyrogram/methods/messages/get_discussion_replies_count.py new file mode 100644 index 00000000..cb5c1e8b --- /dev/null +++ b/pyrogram/methods/messages/get_discussion_replies_count.py @@ -0,0 +1,60 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-present Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import Union + +import pyrogram +from pyrogram import raw + + +class GetDiscussionRepliesCount: + async def get_discussion_replies_count( + self: "pyrogram.Client", + chat_id: Union[int, str], + message_id: int, + ) -> int: + """Get the total count of replies in a discussion thread. + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + message_id (``int``): + Message id. + + Example: + .. code-block:: python + + count = app.get_discussion_replies_count(chat_id, message_id) + """ + + r = await self.invoke( + raw.functions.messages.GetReplies( + peer=await self.resolve_peer(chat_id), + msg_id=message_id, + offset_id=0, + offset_date=0, + add_offset=0, + limit=1, + max_id=0, + min_id=0, + hash=0 + ) + ) + + return r.count