Skip to content

Commit 6a0ed47

Browse files
API 9.2 (#191)
* Rename direct_message_topic_id to direct_messages_topic_id * Added the field checklist_task_id to the class ReplyParameters, allowing bots to reply to a specific checklist task. * Added the field reply_to_checklist_task_id to the class Message. * Added the field can_manage_direct_messages to ChatPrivileges * Added the field is_direct_messages to the class Chat which can be used to identify supergroups that are used as channel direct messages chats. * Added the field parent_chat and direct_messages_chat_idto the class Chat which indicates the parent channel chat for a channel direct messages chat. * Fix direct_messages_chat_id similar to BOT API 9.2 * Added the class DirectMessagesTopic and the field direct_messages_topic to the class Message, describing a topic of a direct messages chat. * Add get_direct_messages_topics_by_id, get_direct_messages_topics, set_chat_direct_messages_group Co-authored-by: KurimuzonAkuma <[email protected]>
1 parent 8ac84cd commit 6a0ed47

File tree

13 files changed

+512
-29
lines changed

13 files changed

+512
-29
lines changed

compiler/docs/compiler.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ def get_title_list(s: str) -> list:
255255
get_chat_members
256256
get_chat_members_count
257257
get_chat_member
258-
258+
get_direct_messages_topics_by_id
259+
get_direct_messages_topics
260+
set_chat_direct_messages_group
259261
get_dialogs
260262
get_dialogs_count
261263
set_chat_username
@@ -505,6 +507,7 @@ def get_title_list(s: str) -> list:
505507
InviteLinkImporter
506508
Restriction
507509
RtmpUrl
510+
DirectMessagesTopic
508511
""",
509512
messages_media="""
510513
Messages & Media

docs/source/releases/changes-in-this-fork.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ Changes in this Fork
3535
| Scheme layer used: 211 |
3636
+------------------------+
3737

38+
- Rename direct_message_topic_id to ``direct_messages_topic_id`` in :obj:`~pyrogram.types.ReplyParameters`. This parameter can be used to send a message to a direct messages chat topic.
39+
- Added the field ``checklist_task_id`` to the class :obj:`~pyrogram.types.ReplyParameters`, allowing bots to reply to a specific checklist task.
40+
- Added the field ``reply_to_checklist_task_id`` to the class :obj:`~pyrogram.types.Message`.
41+
- Added the field ``can_manage_direct_messages`` to :obj:`~pyrogram.types.ChatPrivileges`.
42+
- Added the field ``is_direct_messages`` to the classes :obj:`~pyrogram.types.Chat` which can be used to identify supergroups that are used as channel direct messages chats.
43+
- Added the fields ``parent_chat`` and ``direct_messages_chat_id`` to the class :obj:`~pyrogram.types.Chat` which indicates the parent channel chat for a channel direct messages chat.
44+
- Added the class :obj:`~pyrogram.types.DirectMessagesTopic` and the field ``direct_messages_topic`` to the class :obj:`~pyrogram.types.Message`, describing a topic of a direct messages chat.
45+
- Added :meth:`~pyrogram.Client.get_direct_messages_topics_by_id`, :meth:`~pyrogram.Client.get_direct_messages_topics`, :meth:`~pyrogram.Client.set_chat_direct_messages_group`.
3846
- View `new and changed <https://telegramplayground.github.io/TG-APIs/TL/diff/tdlib.html?from=205&to=211>`__ `raw API methods <https://telegramplayground.github.io/TG-APIs/TL/diff/tdesktop.html?from=205&to=211>`__.
3947

4048
+------------------------+

pyrogram/methods/chats/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from .get_chat_members_count import GetChatMembersCount
3434
from .get_chat_online_count import GetChatOnlineCount
3535
from .get_dialogs import GetDialogs
36+
from .get_direct_messages_topics_by_id import GetDirectMessagesTopicsByID
37+
from .get_direct_messages_topics import GetDirectMessagesTopics
3638
from .get_dialogs_count import GetDialogsCount
3739
from .get_nearby_chats import GetNearbyChats
3840
from .get_send_as_chats import GetSendAsChats
@@ -45,6 +47,7 @@
4547
from .search_chats import SearchChats
4648
from .set_administrator_title import SetAdministratorTitle
4749
from .set_chat_description import SetChatDescription
50+
from .set_chat_direct_messages_group import SetChatDirectMessagesGroup
4851
from .set_chat_permissions import SetChatPermissions
4952
from .set_chat_photo import SetChatPhoto
5053
from .set_chat_protected_content import SetChatProtectedContent
@@ -76,10 +79,13 @@ class Chats(
7679
DeleteChatPhoto,
7780
SetChatTitle,
7881
SetChatDescription,
82+
SetChatDirectMessagesGroup,
7983
SetChatMessageAutoDeleteTime,
8084
PinChatMessage,
8185
UnpinChatMessage,
8286
GetDialogs,
87+
GetDirectMessagesTopicsByID,
88+
GetDirectMessagesTopics,
8389
GetChatMembersCount,
8490
SetChatUsername,
8591
SetChatPermissions,
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present <https://github.com/KurimuzonAkuma>
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 asyncio import sleep
20+
from typing import AsyncGenerator, Optional, Union
21+
22+
import pyrogram
23+
from pyrogram import raw, types, utils
24+
25+
26+
class GetDirectMessagesTopics:
27+
async def get_direct_messages_topics(
28+
self: "pyrogram.Client",
29+
chat_id: Union[int, str],
30+
limit: int = 0
31+
) -> AsyncGenerator["types.DirectMessagesTopic", None]:
32+
"""Get one or more topic from a direct messages channel chat.
33+
34+
.. include:: /_includes/usable-by/users.rst
35+
36+
Parameters:
37+
chat_id (``int`` | ``str``):
38+
Unique identifier (int) or username (str) of the target chat.
39+
40+
limit (``int``, *optional*):
41+
Limits the number of topics to be retrieved.
42+
By default, no limit is applied and all topics are returned.
43+
44+
Returns:
45+
``Generator``: A generator yielding :obj:`~pyrogram.types.DirectMessagesTopic` objects.
46+
47+
Example:
48+
.. code-block:: python
49+
50+
# Iterate through all topics
51+
async for topic in app.get_direct_messages_topics(chat_id):
52+
print(topic)
53+
54+
"""
55+
current = 0
56+
total = limit or (1 << 31) - 1
57+
limit = min(100, total)
58+
59+
offset_date = 0
60+
offset_id = 0
61+
offset_peer = raw.types.InputPeerEmpty()
62+
63+
while True:
64+
r = await self.invoke(
65+
raw.functions.messages.GetSavedDialogs(
66+
offset_date=offset_date,
67+
offset_id=offset_id,
68+
offset_peer=offset_peer,
69+
limit=limit,
70+
hash=0,
71+
exclude_pinned=None,
72+
parent_peer=await self.resolve_peer(chat_id)
73+
)
74+
)
75+
76+
users = {i.id: i for i in r.users}
77+
chats = {i.id: i for i in r.chats}
78+
79+
messages = {}
80+
81+
for message in r.messages:
82+
if isinstance(message, raw.types.MessageEmpty):
83+
continue
84+
85+
messages[message.id] = await types.Message._parse(
86+
self,
87+
message,
88+
users,
89+
chats,
90+
replies=self.fetch_replies
91+
)
92+
93+
topics = []
94+
95+
for topic in r.dialogs:
96+
topics.append(
97+
types.DirectMessagesTopic._parse_dialog(
98+
client=self,
99+
topic=topic,
100+
messages=messages,
101+
users=users,
102+
chats=chats
103+
)
104+
)
105+
106+
if not topics:
107+
return
108+
109+
last = topics[-1]
110+
111+
offset_id = last.last_message.id
112+
offset_date = utils.datetime_to_timestamp(last.last_message.date)
113+
offset_peer = await self.resolve_peer(last.topic_id)
114+
115+
for topic in topics:
116+
await sleep(0)
117+
118+
yield topic
119+
120+
current += 1
121+
122+
if current >= total:
123+
return
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present <https://github.com/KurimuzonAkuma>
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+
import logging
20+
from typing import Iterable, Union
21+
22+
import pyrogram
23+
from pyrogram import raw, types, utils
24+
25+
log = logging.getLogger(__name__)
26+
27+
28+
class GetDirectMessagesTopicsByID:
29+
async def get_direct_messages_topics_by_id(
30+
self: "pyrogram.Client",
31+
chat_id: Union[int, str],
32+
topic_ids: Union[int, Iterable[int]]
33+
) -> Union[
34+
"types.DirectMessagesTopic",
35+
list["types.DirectMessagesTopic"]
36+
]:
37+
"""Get one or more direct message topic from a chat by using topic identifiers.
38+
39+
.. include:: /_includes/usable-by/users.rst
40+
41+
Parameters:
42+
chat_id (``int`` | ``str``):
43+
Unique identifier (int) or username (str) of the target chat.
44+
45+
topic_ids (``int`` | Iterable of ``int``, *optional*):
46+
Pass a single topic identifier or an iterable of topic ids (as integers) to get the information of the
47+
topic themselves.
48+
49+
Returns:
50+
:obj:`~pyrogram.types.DirectMessagesTopic` | List of :obj:`~pyrogram.types.DirectMessagesTopic`: In case *topic_ids* was not
51+
a list, a single topic is returned, otherwise a list of topics is returned.
52+
53+
Example:
54+
.. code-block:: python
55+
56+
# Get one topic
57+
await app.get_direct_messages_topics_by_id(chat_id, 12345)
58+
59+
# Get more than one topic (list of topics)
60+
await app.get_direct_messages_topics_by_id(chat_id, [12345, 12346])
61+
62+
"""
63+
64+
is_iterable = utils.is_list_like(topic_ids)
65+
ids = list(topic_ids) if is_iterable else [topic_ids]
66+
67+
r = await self.invoke(
68+
raw.functions.messages.GetSavedDialogsByID(
69+
ids=[await self.resolve_peer(i) for i in ids],
70+
parent_peer=await self.resolve_peer(chat_id)
71+
)
72+
)
73+
74+
users = {i.id: i for i in r.users}
75+
chats = {i.id: i for i in r.chats}
76+
77+
topics = types.List()
78+
79+
for i in r.dialogs:
80+
topics.append(
81+
types.DirectMessagesTopic._parse_dialog(
82+
client=self,
83+
topic=i,
84+
users=users,
85+
chats=chats
86+
)
87+
)
88+
89+
return topics if is_iterable else topics[0] if topics else None
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-present <https://github.com/KurimuzonAkuma>
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 Optional, Union
20+
21+
import pyrogram
22+
from pyrogram import types, raw
23+
24+
25+
class SetChatDirectMessagesGroup:
26+
async def set_chat_direct_messages_group(
27+
self: "pyrogram.Client",
28+
chat_id: Union[int, str],
29+
is_enabled: bool = Optional[None],
30+
paid_message_star_count: int = 0,
31+
) -> Union["types.Message", bool]:
32+
"""Change direct messages group settings for a channel chat.
33+
34+
.. include:: /_includes/usable-by/users.rst
35+
36+
Requires owner privileges in the chat.
37+
38+
Parameters:
39+
chat_id (``int`` | ``str``):
40+
Unique identifier (int) or username (str) of the target chat.
41+
42+
is_enabled (``bool``, *optional*):
43+
Pass True if the direct messages group is enabled for the channel chat. Pass False otherwise.
44+
45+
paid_message_star_count (``bool``):
46+
The new number of Telegram Stars that must be paid for each message that is sent to the direct messages chat unless the sender is an administrator of the channel chat, 0-``stars_paid_message_amount_max``.
47+
48+
Returns:
49+
:obj:`~pyrogram.types.Message` | ``bool``: On success, a service message will be returned (when applicable),
50+
otherwise, in case a message object couldn't be returned, True is returned.
51+
52+
Raises:
53+
RPCError: In case of a Telegram RPC error.
54+
55+
Example:
56+
.. code-block:: python
57+
58+
# Enable direct messages
59+
await app.set_chat_direct_messages_group(chat_id, is_enabled=True)
60+
61+
"""
62+
63+
r = await self.invoke(
64+
raw.functions.channels.UpdatePaidMessagesPrice(
65+
channel=await self.resolve_peer(chat_id),
66+
send_paid_messages_stars=paid_message_star_count,
67+
broadcast_messages_allowed=is_enabled
68+
)
69+
)
70+
users = {i.id: i for i in r.users}
71+
chats = {i.id: i for i in r.chats}
72+
for i in r.updates:
73+
if isinstance(i, (raw.types.UpdateNewMessage, raw.types.UpdateNewChannelMessage)):
74+
return await types.Message._parse(
75+
self,
76+
i.message,
77+
users,
78+
chats,
79+
replies=self.fetch_replies
80+
)
81+
else:
82+
return True

pyrogram/types/input_message_content/reply_parameters.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ class ReplyParameters(Object):
5656
quote_position (``int``, *optional*):
5757
Position of the quote in the original message in UTF-16 code units
5858
59-
direct_message_topic_id (``int``, *optional*):
60-
TEMPORARY till the NEXT update.
61-
Unique identifier of the topic in a channel direct messages chat administered by the current user; pass None if the chat is not a channel direct messages chat administered by the current user.
59+
checklist_task_id (``int``, *optional*):
60+
Identifier of the specific checklist task to be replied to.
61+
62+
direct_messages_topic_id (``int``, *optional*):
63+
Identifier of the direct messages topic to which the message will be sent; **required** if the message is sent to a direct messages chat; pass None if the chat is not a channel direct messages chat administered by the current user.
6264
6365
"""
6466

@@ -73,7 +75,8 @@ def __init__(
7375
quote_parse_mode: Optional["enums.ParseMode"] = None,
7476
quote_entities: list["types.MessageEntity"] = None,
7577
quote_position: int = None,
76-
direct_message_topic_id: int = None,
78+
checklist_task_id: int = None,
79+
direct_messages_topic_id: int = None,
7780
):
7881
super().__init__()
7982

@@ -84,4 +87,5 @@ def __init__(
8487
self.quote_parse_mode = quote_parse_mode
8588
self.quote_entities = quote_entities
8689
self.quote_position = quote_position
87-
self.direct_message_topic_id = direct_message_topic_id
90+
self.checklist_task_id = checklist_task_id
91+
self.direct_messages_topic_id = direct_messages_topic_id

0 commit comments

Comments
 (0)