Skip to content

Commit 9a2bc25

Browse files
committed
Add support for "send_as" chats
- Add methods get_send_as_chats() and set_send_as_chat() - Add field Chat.send_as_chat
1 parent e8076d1 commit 9a2bc25

File tree

5 files changed

+158
-20
lines changed

5 files changed

+158
-20
lines changed

compiler/docs/compiler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ def get_title_list(s: str) -> list:
224224
mark_chat_unread
225225
get_chat_event_log
226226
get_chat_online_count
227+
get_send_as_chats
228+
set_send_as_chat
227229
""",
228230
users="""
229231
Users

pyrogram/methods/chats/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from .get_dialogs import GetDialogs
3636
from .get_dialogs_count import GetDialogsCount
3737
from .get_nearby_chats import GetNearbyChats
38+
from .get_send_as_chats import GetSendAsChats
3839
from .iter_chat_members import IterChatMembers
3940
from .iter_dialogs import IterDialogs
4041
from .join_chat import JoinChat
@@ -48,6 +49,7 @@
4849
from .set_chat_permissions import SetChatPermissions
4950
from .set_chat_photo import SetChatPhoto
5051
from .set_chat_title import SetChatTitle
52+
from .set_send_as_chat import SetSendAsChat
5153
from .set_slow_mode import SetSlowMode
5254
from .unarchive_chats import UnarchiveChats
5355
from .unban_chat_member import UnbanChatMember
@@ -94,6 +96,8 @@ class Chats(
9496
UnpinAllChatMessages,
9597
MarkChatUnread,
9698
GetChatEventLog,
97-
GetChatOnlineCount
99+
GetChatOnlineCount,
100+
GetSendAsChats,
101+
SetSendAsChat
98102
):
99103
pass
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 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 List, Union
20+
21+
from pyrogram import raw
22+
from pyrogram import types
23+
from pyrogram.scaffold import Scaffold
24+
25+
26+
class GetSendAsChats(Scaffold):
27+
async def get_send_as_chats(
28+
self,
29+
chat_id: Union[int, str]
30+
) -> List["types.Chat"]:
31+
"""Get the list of "send_as" chats available.
32+
33+
Parameters:
34+
chat_id (``int`` | ``str``):
35+
Unique identifier (int) or username (str) of the target chat.
36+
37+
Returns:
38+
List[:obj:`~pyrogram.types.Chat`]: The list of chats.
39+
40+
Example:
41+
.. code-block:: python
42+
43+
chats = app.get_send_as_chats(chat_id)
44+
print(chats)
45+
"""
46+
r = await self.send(
47+
raw.functions.channels.GetSendAs(
48+
peer=await self.resolve_peer(chat_id)
49+
)
50+
)
51+
52+
users = {u.id: u for u in r.users}
53+
chats = {c.id: c for c in r.chats}
54+
55+
send_as_chats = types.List()
56+
57+
for p in r.peers:
58+
if isinstance(p, raw.types.PeerUser):
59+
send_as_chats.append(types.Chat._parse_chat(self, users[p.user_id]))
60+
else:
61+
send_as_chats.append(types.Chat._parse_chat(self, chats[p.channel_id]))
62+
63+
return send_as_chats
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 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+
from pyrogram import raw
22+
from pyrogram.scaffold import Scaffold
23+
24+
25+
class SetSendAsChat(Scaffold):
26+
async def set_send_as_chat(
27+
self,
28+
chat_id: Union[int, str],
29+
send_as_chat_id: Union[int, str]
30+
) -> bool:
31+
"""Set the default "send_as" chat for a chat.
32+
33+
Use :meth:`~pyrogram.Client.get_send_as_chats` to get all the "send_as" chats available for use.
34+
35+
Parameters:
36+
chat_id (``int`` | ``str``):
37+
Unique identifier (int) or username (str) of the target chat.
38+
39+
send_as_chat_id (``int`` | ``str``):
40+
Unique identifier (int) or username (str) of the send_as chat.
41+
42+
Returns:
43+
``bool``: On success, true is returned
44+
45+
Example:
46+
.. code-block:: python
47+
48+
app.set_send_as_chat(chat_id, send_as_chat_id)
49+
"""
50+
return await self.send(
51+
raw.functions.messages.SaveDefaultSendAs(
52+
peer=await self.resolve_peer(chat_id),
53+
send_as=await self.resolve_peer(send_as_chat_id)
54+
)
55+
)

pyrogram/types/user_and_chats/chat.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ class Chat(Object):
120120
linked_chat (:obj:`~pyrogram.types.Chat`, *optional*):
121121
The linked discussion group (in case of channels) or the linked channel (in case of supergroups).
122122
Returned only in :meth:`~pyrogram.Client.get_chat`.
123+
124+
send_as_chat (:obj:`~pyrogram.types.Chat`, *optional*):
125+
The default "send_as" chat.
126+
Returned only in :meth:`~pyrogram.Client.get_chat`.
123127
"""
124128

125129
def __init__(
@@ -151,7 +155,8 @@ def __init__(
151155
restrictions: List["types.Restriction"] = None,
152156
permissions: "types.ChatPermissions" = None,
153157
distance: int = None,
154-
linked_chat: "types.Chat" = None
158+
linked_chat: "types.Chat" = None,
159+
send_as_chat: "types.Chat" = None
155160
):
156161
super().__init__(client)
157162

@@ -181,6 +186,7 @@ def __init__(
181186
self.permissions = permissions
182187
self.distance = distance
183188
self.linked_chat = linked_chat
189+
self.send_as_chat = send_as_chat
184190

185191
@staticmethod
186192
def _parse_user_chat(client, user: raw.types.User) -> "Chat":
@@ -275,43 +281,51 @@ def _parse_dialog(client, peer, users: dict, chats: dict):
275281

276282
@staticmethod
277283
async def _parse_full(client, chat_full: Union[raw.types.messages.ChatFull, raw.types.users.UserFull]) -> "Chat":
284+
users = {u.id: u for u in chat_full.users}
285+
chats = {c.id: c for c in chat_full.chats}
286+
278287
if isinstance(chat_full, raw.types.users.UserFull):
279-
parsed_chat = Chat._parse_user_chat(client, chat_full.users[0])
280-
parsed_chat.bio = chat_full.full_user.about
288+
full_user = chat_full.full_user
281289

282-
if chat_full.full_user.pinned_msg_id:
290+
parsed_chat = Chat._parse_user_chat(client, users[full_user.id])
291+
parsed_chat.bio = full_user.about
292+
293+
if full_user.pinned_msg_id:
283294
parsed_chat.pinned_message = await client.get_messages(
284295
parsed_chat.id,
285-
message_ids=chat_full.full_user.pinned_msg_id
296+
message_ids=full_user.pinned_msg_id
286297
)
287298
else:
288299
full_chat = chat_full.full_chat
289-
chat = None
290-
linked_chat = None
291-
292-
for c in chat_full.chats:
293-
if full_chat.id == c.id:
294-
chat = c
295-
296-
if isinstance(full_chat, raw.types.ChannelFull):
297-
if full_chat.linked_chat_id == c.id:
298-
linked_chat = c
300+
chat_raw = chats[full_chat.id]
299301

300302
if isinstance(full_chat, raw.types.ChatFull):
301-
parsed_chat = Chat._parse_chat_chat(client, chat)
303+
parsed_chat = Chat._parse_chat_chat(client, chat_raw)
302304
parsed_chat.description = full_chat.about or None
303305

304306
if isinstance(full_chat.participants, raw.types.ChatParticipants):
305307
parsed_chat.members_count = len(full_chat.participants.participants)
306308
else:
307-
parsed_chat = Chat._parse_channel_chat(client, chat)
309+
parsed_chat = Chat._parse_channel_chat(client, chat_raw)
308310
parsed_chat.members_count = full_chat.participants_count
309311
parsed_chat.description = full_chat.about or None
310312
# TODO: Add StickerSet type
311313
parsed_chat.can_set_sticker_set = full_chat.can_set_stickers
312314
parsed_chat.sticker_set_name = getattr(full_chat.stickerset, "short_name", None)
313-
if linked_chat:
314-
parsed_chat.linked_chat = Chat._parse_channel_chat(client, linked_chat)
315+
316+
linked_chat_raw = chats.get(full_chat.linked_chat_id, None)
317+
318+
if linked_chat_raw:
319+
parsed_chat.linked_chat = Chat._parse_channel_chat(client, linked_chat_raw)
320+
321+
default_send_as = full_chat.default_send_as
322+
323+
if isinstance(default_send_as, raw.types.PeerUser):
324+
send_as_raw = users[default_send_as.user_id]
325+
else:
326+
send_as_raw = chats[default_send_as.channel_id]
327+
328+
parsed_chat.send_as_chat = Chat._parse_chat(client, send_as_raw)
315329

316330
if full_chat.pinned_msg_id:
317331
parsed_chat.pinned_message = await client.get_messages(

0 commit comments

Comments
 (0)