Skip to content

Commit c029854

Browse files
committed
Add set_custom_title method
1 parent 5b27b95 commit c029854

File tree

3 files changed

+96
-3
lines changed

3 files changed

+96
-3
lines changed

pyrogram/client/methods/chats/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from .get_chat_members_count import GetChatMembersCount
3232
from .get_dialogs import GetDialogs
3333
from .get_dialogs_count import GetDialogsCount
34+
from .get_nearby_chats import GetNearbyChats
3435
from .iter_chat_members import IterChatMembers
3536
from .iter_dialogs import IterDialogs
3637
from .join_chat import JoinChat
@@ -43,11 +44,11 @@
4344
from .set_chat_permissions import SetChatPermissions
4445
from .set_chat_photo import SetChatPhoto
4546
from .set_chat_title import SetChatTitle
47+
from .set_custom_title import SetCustomTitle
4648
from .unarchive_chats import UnarchiveChats
4749
from .unban_chat_member import UnbanChatMember
4850
from .unpin_chat_message import UnpinChatMessage
4951
from .update_chat_username import UpdateChatUsername
50-
from .get_nearby_chats import GetNearbyChats
5152

5253

5354
class Chats(
@@ -82,6 +83,7 @@ class Chats(
8283
AddChatMembers,
8384
DeleteChannel,
8485
DeleteSupergroup,
85-
GetNearbyChats
86+
GetNearbyChats,
87+
SetCustomTitle
8688
):
8789
pass

pyrogram/client/methods/chats/promote_chat_member.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ def promote_chat_member(
9797
invite_users=can_invite_users or None,
9898
pin_messages=can_pin_messages or None,
9999
add_admins=can_promote_members or None,
100-
)
100+
),
101+
rank=""
101102
)
102103
)
103104

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2019 Dan Tès <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.api import functions, types
22+
from ...ext import BaseClient
23+
24+
25+
class SetCustomTitle(BaseClient):
26+
def set_custom_title(
27+
self,
28+
chat_id: Union[int, str],
29+
user_id: Union[int, str],
30+
title: str,
31+
) -> bool:
32+
"""Set a custom title to administrators or owners of a supergroup.
33+
34+
Parameters:
35+
chat_id (``int`` | ``str``):
36+
Unique identifier (int) or username (str) of the target chat.
37+
38+
user_id (``int`` | ``str``):
39+
Unique identifier (int) or username (str) of the target user.
40+
For a contact that exists in your Telegram address book you can use his phone number (str).
41+
42+
title (``str``, *optional*):
43+
A custom title that will be shown to all members instead of "Owner" or "Admin".
44+
Pass None or "" (empty string) to remove the custom title.
45+
46+
Returns:
47+
``bool``: True on success.
48+
49+
Example:
50+
.. code-block:: python
51+
52+
# Set custom titles to owners or administrators of supergroups
53+
app.set_custom_title(chat_id, user_id, "Custom Title")
54+
"""
55+
chat_id = self.resolve_peer(chat_id)
56+
user_id = self.resolve_peer(user_id)
57+
58+
r = self.send(
59+
functions.channels.GetParticipant(
60+
channel=chat_id,
61+
user_id=user_id
62+
)
63+
).participant
64+
65+
if isinstance(r, types.ChannelParticipantCreator):
66+
admin_rights = types.ChatAdminRights(
67+
change_info=True,
68+
post_messages=True,
69+
edit_messages=True,
70+
delete_messages=True,
71+
ban_users=True,
72+
invite_users=True,
73+
pin_messages=True,
74+
add_admins=True,
75+
)
76+
elif isinstance(r, types.ChannelParticipantAdmin):
77+
admin_rights = r.admin_rights
78+
else:
79+
raise ValueError("Custom titles can only be applied to owners or administrators of supergroups")
80+
81+
self.send(
82+
functions.channels.EditAdmin(
83+
channel=chat_id,
84+
user_id=user_id,
85+
admin_rights=admin_rights,
86+
rank=title
87+
)
88+
)
89+
90+
return True

0 commit comments

Comments
 (0)