Skip to content

Commit f8f46b9

Browse files
Add get_direct_messages_chat_topic_history method
1 parent b59ae77 commit f8f46b9

3 files changed

Lines changed: 157 additions & 1 deletion

File tree

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ def get_title_list(s: str) -> list:
280280
get_discussion_replies
281281
get_discussion_replies_count
282282
get_custom_emoji_stickers
283+
get_direct_messages_chat_topic_history
283284
send_web_page
284285
start_bot
285286
delete_chat_history

pyrogram/methods/messages/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
from .get_chat_history import GetChatHistory
3737
from .get_chat_history_count import GetChatHistoryCount
3838
from .get_custom_emoji_stickers import GetCustomEmojiStickers
39+
from .get_direct_messages_chat_topic_history import GetDirectMessagesChatTopicHistory
3940
from .get_discussion_message import GetDiscussionMessage
4041
from .get_discussion_replies import GetDiscussionReplies
4142
from .get_discussion_replies_count import GetDiscussionRepliesCount
@@ -146,6 +147,7 @@ class Messages(
146147
GetDiscussionReplies,
147148
GetDiscussionRepliesCount,
148149
StreamMedia,
149-
GetCustomEmojiStickers
150+
GetCustomEmojiStickers,
151+
GetDirectMessagesChatTopicHistory
150152
):
151153
pass
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from datetime import datetime
20+
from typing import AsyncGenerator, Union
21+
22+
import pyrogram
23+
from pyrogram import raw, types, utils
24+
25+
26+
async def get_chunk(
27+
*,
28+
client: "pyrogram.Client",
29+
chat_id: Union[int, str],
30+
topic_id: int,
31+
limit: int = 0,
32+
offset: int = 0,
33+
from_message_id: int = 0,
34+
from_date: datetime = utils.zero_datetime(),
35+
min_id: int = 0,
36+
max_id: int = 0,
37+
reverse: bool = False
38+
):
39+
from_message_id = from_message_id or (1 if reverse else 0)
40+
41+
messages = await client.invoke(
42+
raw.functions.messages.GetSavedHistory(
43+
peer=await client.resolve_peer(topic_id),
44+
offset_id=from_message_id,
45+
offset_date=utils.datetime_to_timestamp(from_date),
46+
add_offset=offset * (-1 if reverse else 1) - (limit if reverse else 0),
47+
limit=limit,
48+
max_id=max_id,
49+
min_id=min_id,
50+
hash=0,
51+
parent_peer=await client.resolve_peer(chat_id)
52+
),
53+
sleep_threshold=60
54+
)
55+
56+
messages = await utils.parse_messages(client, messages, replies=0)
57+
if reverse:
58+
messages.reverse()
59+
60+
return messages
61+
62+
class GetDirectMessagesChatTopicHistory:
63+
async def get_direct_messages_chat_topic_history(
64+
self: "pyrogram.Client",
65+
chat_id: Union[int, str],
66+
topic_id: int,
67+
limit: int = 0,
68+
offset: int = 0,
69+
offset_id: int = 0,
70+
offset_date: datetime = utils.zero_datetime(),
71+
min_id: int = 0,
72+
max_id: int = 0,
73+
reverse: bool = False
74+
) -> AsyncGenerator["types.Message", None]:
75+
"""Return messages in the topic in a channel direct messages chat administered by the current user.
76+
77+
The messages are returned in reverse chronological order.
78+
79+
.. include:: /_includes/usable-by/users.rst
80+
81+
Parameters:
82+
chat_id (``int`` | ``str``):
83+
Unique identifier (int) or username (str) of the target chat.
84+
For your personal cloud (Saved Messages) you can simply use "me" or "self".
85+
For a contact that exists in your Telegram address book you can use his phone number (str).
86+
87+
topic_id (``int``):
88+
Identifier of the topic which messages will be fetched.
89+
90+
limit (``int``, *optional*):
91+
Limits the number of messages to be retrieved.
92+
By default, no limit is applied and all messages are returned.
93+
94+
offset (``int``, *optional*):
95+
Sequential number of the first message to be returned..
96+
Negative values are also accepted and become useful in case you set offset_id or offset_date.
97+
98+
offset_id (``int``, *optional*):
99+
Identifier of the first message to be returned.
100+
101+
offset_date (:py:obj:`~datetime.datetime`, *optional*):
102+
Pass a date as offset to retrieve only older messages starting from that date.
103+
104+
min_id (``int``, *optional*):
105+
If a positive value was provided, the method will return only messages with IDs more than min_id.
106+
107+
max_id (``int``, *optional*):
108+
If a positive value was provided, the method will return only messages with IDs less than max_id.
109+
110+
reverse (``bool``, *optional*):
111+
Pass True to retrieve the messages from oldest to newest.
112+
113+
Returns:
114+
``Generator``: A generator yielding :obj:`~pyrogram.types.Message` objects.
115+
116+
Example:
117+
.. code-block:: python
118+
119+
async for message in app.get_direct_messages_chat_topic_history(chat_id, topic_id):
120+
print(message.text)
121+
"""
122+
current = 0
123+
total = limit or (1 << 31) - 1
124+
limit = min(100, total)
125+
126+
while True:
127+
messages = await get_chunk(
128+
client=self,
129+
chat_id=chat_id,
130+
topic_id=topic_id,
131+
limit=limit,
132+
offset=offset,
133+
from_message_id=offset_id,
134+
from_date=offset_date,
135+
max_id=max_id,
136+
min_id=min_id,
137+
reverse=reverse
138+
)
139+
140+
if not messages:
141+
return
142+
143+
offset_id = messages[-1].id
144+
if reverse:
145+
offset_id += 1
146+
147+
for message in messages:
148+
yield message
149+
150+
current += 1
151+
152+
if current >= total:
153+
return

0 commit comments

Comments
 (0)