Skip to content

Commit 3e178a0

Browse files
author
Pyrogram-Mod - Dev
committed
Add monoforum (Direct Messages) high-level methods
Wraps the existing messages.getSavedHistory/getSavedDialogs/ readSavedHistory/deleteSavedHistory and channels.updatePaidMessagesPrice raw functions with get_monoforum_history, get_monoforum_dialogs, read_monoforum_history, delete_monoforum_history and toggle_direct_messages.
1 parent 2b13b04 commit 3e178a0

7 files changed

Lines changed: 390 additions & 1 deletion

File tree

pyrogram/methods/chats/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
from .unban_chat_member import UnbanChatMember
6161
from .unpin_all_chat_messages import UnpinAllChatMessages
6262
from .unpin_chat_message import UnpinChatMessage
63+
from .toggle_direct_messages import ToggleDirectMessages
6364

6465

6566
class Chats(
@@ -106,6 +107,7 @@ class Chats(
106107
SetChatGuardBot,
107108
SetChatProtectedContent,
108109
CreateGroupCall,
109-
GetGroupCallStreamChannels
110+
GetGroupCallStreamChannels,
111+
ToggleDirectMessages,
110112
):
111113
pass
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 typing import Union
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
24+
25+
class ToggleDirectMessages:
26+
async def toggle_direct_messages(
27+
self: "pyrogram.Client",
28+
channel_id: Union[int, str],
29+
enabled: bool,
30+
stars: int = 0,
31+
) -> bool:
32+
"""Enable or disable the monoforum (Direct Messages) for a broadcast channel.
33+
34+
.. include:: /_includes/usable-by/users.rst
35+
36+
Parameters:
37+
channel_id (``int`` | ``str``):
38+
Unique identifier (int) or username (str) of the target channel.
39+
40+
enabled (``bool``):
41+
Pass True to enable Direct Messages, False to disable.
42+
43+
stars (``int``, *optional*):
44+
Number of Stars required to send a direct message. Defaults to 0 (free).
45+
46+
Returns:
47+
``bool``: True on success.
48+
49+
Example:
50+
.. code-block:: python
51+
52+
await app.toggle_direct_messages(channel_id, True)
53+
"""
54+
await self.invoke(
55+
raw.functions.channels.UpdatePaidMessagesPrice(
56+
channel=await self.resolve_peer(channel_id),
57+
broadcast_messages_allowed=enabled,
58+
send_paid_messages_stars=stars
59+
)
60+
)
61+
62+
return True

pyrogram/methods/messages/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575
from .translate_rich_message import TranslateRichMessage
7676
from .compose_rich_message_with_ai import ComposeRichMessageWithAI
7777
from .request_chat_join_web_view import RequestChatJoinWebView
78+
from .get_monoforum_history import GetMonoforumHistory
79+
from .get_monoforum_dialogs import GetMonoforumDialogs
80+
from .read_monoforum_history import ReadMonoforumHistory
81+
from .delete_monoforum_history import DeleteMonoforumHistory
7882

7983

8084
class Messages(
@@ -137,5 +141,9 @@ class Messages(
137141
TranslateRichMessage,
138142
ComposeRichMessageWithAI,
139143
RequestChatJoinWebView,
144+
GetMonoforumHistory,
145+
GetMonoforumDialogs,
146+
ReadMonoforumHistory,
147+
DeleteMonoforumHistory,
140148
):
141149
pass
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 typing import Union
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
24+
25+
class DeleteMonoforumHistory:
26+
async def delete_monoforum_history(
27+
self: "pyrogram.Client",
28+
channel_id: Union[int, str],
29+
peer_id: Union[int, str],
30+
max_id: int = 0,
31+
) -> bool:
32+
"""Delete a monoforum sender sublist's message history.
33+
34+
.. include:: /_includes/usable-by/users.rst
35+
36+
Parameters:
37+
channel_id (``int`` | ``str``):
38+
Unique identifier (int) or username (str) of the monoforum channel.
39+
40+
peer_id (``int`` | ``str``):
41+
Unique identifier of the sender whose sublist to delete.
42+
43+
max_id (``int``, *optional*):
44+
The id of the last message to delete. Defaults to 0 (delete the whole history).
45+
46+
Returns:
47+
``bool``: True on success.
48+
49+
Example:
50+
.. code-block:: python
51+
52+
await app.delete_monoforum_history(channel_id, peer_id)
53+
"""
54+
await self.invoke(
55+
raw.functions.messages.DeleteSavedHistory(
56+
parent_peer=await self.resolve_peer(channel_id),
57+
peer=await self.resolve_peer(peer_id),
58+
max_id=max_id
59+
)
60+
)
61+
62+
return True
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 typing import Union
20+
21+
import pyrogram
22+
from pyrogram import raw
23+
24+
25+
class GetMonoforumDialogs:
26+
async def get_monoforum_dialogs(
27+
self: "pyrogram.Client",
28+
channel_id: Union[int, str],
29+
limit: int = 0,
30+
) -> "raw.base.messages.SavedDialogs":
31+
"""Get the list of per-sender sublists (dialogs) of a monoforum channel.
32+
33+
.. include:: /_includes/usable-by/users.rst
34+
35+
Parameters:
36+
channel_id (``int`` | ``str``):
37+
Unique identifier (int) or username (str) of the monoforum channel.
38+
39+
limit (``int``, *optional*):
40+
Limits the number of dialogs to be retrieved. Defaults to 100.
41+
42+
Returns:
43+
:obj:`~pyrogram.raw.base.messages.SavedDialogs`: The raw dialogs list.
44+
45+
Example:
46+
.. code-block:: python
47+
48+
dialogs = await app.get_monoforum_dialogs(channel_id)
49+
"""
50+
parent_peer = await self.resolve_peer(channel_id)
51+
52+
return await self.invoke(
53+
raw.functions.messages.GetSavedDialogs(
54+
parent_peer=parent_peer,
55+
offset_date=0,
56+
offset_id=0,
57+
offset_peer=raw.types.InputPeerEmpty(),
58+
limit=limit or 100,
59+
hash=0
60+
)
61+
)
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 Union, Optional, AsyncGenerator
21+
22+
import pyrogram
23+
from pyrogram import types, raw, utils
24+
25+
26+
async def get_chunk(
27+
*,
28+
client: "pyrogram.Client",
29+
channel_id: Union[int, str],
30+
peer_id: Union[int, str],
31+
limit: int = 0,
32+
offset: int = 0,
33+
min_id: int = 0,
34+
max_id: int = 0,
35+
from_message_id: int = 0,
36+
from_date: datetime = utils.zero_datetime()
37+
):
38+
messages = await client.invoke(
39+
raw.functions.messages.GetSavedHistory(
40+
parent_peer=await client.resolve_peer(channel_id),
41+
peer=await client.resolve_peer(peer_id),
42+
offset_id=from_message_id,
43+
offset_date=utils.datetime_to_timestamp(from_date),
44+
add_offset=offset,
45+
limit=limit,
46+
max_id=max_id,
47+
min_id=min_id,
48+
hash=0
49+
),
50+
sleep_threshold=60
51+
)
52+
53+
return await utils.parse_messages(client, messages, replies=0)
54+
55+
56+
class GetMonoforumHistory:
57+
async def get_monoforum_history(
58+
self: "pyrogram.Client",
59+
channel_id: Union[int, str],
60+
peer_id: Union[int, str],
61+
limit: int = 0,
62+
offset: int = 0,
63+
offset_id: int = 0,
64+
min_id: int = 0,
65+
max_id: int = 0,
66+
offset_date: datetime = utils.zero_datetime()
67+
) -> Optional[AsyncGenerator["types.Message", None]]:
68+
"""Get the direct-message history between a monoforum channel and a specific sender.
69+
70+
Monoforum channels (Direct Messages) route each sender's messages into a separate
71+
sublist, addressed by the sender's peer id.
72+
73+
.. include:: /_includes/usable-by/users.rst
74+
75+
Parameters:
76+
channel_id (``int`` | ``str``):
77+
Unique identifier (int) or username (str) of the monoforum channel.
78+
79+
peer_id (``int`` | ``str``):
80+
Unique identifier of the sender whose sublist to read.
81+
82+
limit (``int``, *optional*):
83+
Limits the number of messages to be retrieved.
84+
By default, no limit is applied and all messages are returned.
85+
86+
offset (``int``, *optional*):
87+
Sequential number of the first message to be returned.
88+
89+
offset_id (``int``, *optional*):
90+
Identifier of the first message to be returned.
91+
92+
offset_date (:py:obj:`~datetime.datetime`, *optional*):
93+
Pass a date as offset to retrieve only older messages starting from that date.
94+
95+
Returns:
96+
``Generator``: A generator yielding :obj:`~pyrogram.types.Message` objects.
97+
98+
Example:
99+
.. code-block:: python
100+
101+
async for message in app.get_monoforum_history(channel_id, peer_id):
102+
print(message.text)
103+
"""
104+
current = 0
105+
total = limit or (1 << 31) - 1
106+
limit = min(100, total)
107+
108+
while True:
109+
messages = await get_chunk(
110+
client=self,
111+
channel_id=channel_id,
112+
peer_id=peer_id,
113+
limit=limit,
114+
offset=offset,
115+
from_message_id=offset_id,
116+
min_id=min_id,
117+
max_id=max_id,
118+
from_date=offset_date
119+
)
120+
121+
if not messages:
122+
return
123+
124+
offset_id = messages[-1].id
125+
126+
for message in messages:
127+
yield message
128+
129+
current += 1
130+
131+
if current >= total:
132+
return

0 commit comments

Comments
 (0)