From fdafe582d7635f4dfb9aa0e1ee986e4780eab4fb Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 7 Sep 2019 17:07:14 +0200 Subject: [PATCH 1/3] Add custom administrator titles related errors --- compiler/error/source/400_BAD_REQUEST.tsv | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compiler/error/source/400_BAD_REQUEST.tsv b/compiler/error/source/400_BAD_REQUEST.tsv index 7e930c8d..dec0ffa4 100644 --- a/compiler/error/source/400_BAD_REQUEST.tsv +++ b/compiler/error/source/400_BAD_REQUEST.tsv @@ -125,4 +125,6 @@ WEBDOCUMENT_URL_INVALID The web document URL is invalid WEBDOCUMENT_MIME_INVALID The web document mime type is invalid BUTTON_URL_INVALID The button url is invalid AUTH_BYTES_INVALID The authorization bytes are invalid -CHANNELS_TOO_MUCH You have joined too many channels or supergroups \ No newline at end of file +CHANNELS_TOO_MUCH You have joined too many channels or supergroups +ADMIN_RANK_INVALID The custom administrator title is invalid or is longer than 16 characters +ADMIN_RANK_EMOJI_NOT_ALLOWED Emojis are not allowed in custom administrator titles \ No newline at end of file From 5b27b952c406992827a5a4bbcdb39b15915b9c4d Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 7 Sep 2019 17:08:00 +0200 Subject: [PATCH 2/3] Add .title attribute for ChatMember objects (admins and owners only) --- pyrogram/client/types/user_and_chats/chat_member.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pyrogram/client/types/user_and_chats/chat_member.py b/pyrogram/client/types/user_and_chats/chat_member.py index 812a3204..8155dd70 100644 --- a/pyrogram/client/types/user_and_chats/chat_member.py +++ b/pyrogram/client/types/user_and_chats/chat_member.py @@ -33,6 +33,10 @@ class ChatMember(Object): The member's status in the chat. Can be "creator", "administrator", "member", "restricted", "left" or "kicked". + title (``str``, *optional*): + A custom title that will be shown to all members instead of "Owner" or "Admin". + Creator (owner) and administrators only. Can be None in case there's no custom title set. + until_date (``int``, *optional*): Restricted and kicked only. Date when restrictions will be lifted for this user; unix time. @@ -119,6 +123,7 @@ class ChatMember(Object): client: "pyrogram.BaseClient" = None, user: "pyrogram.User", status: str, + title: str = None, until_date: int = None, joined_date: int = None, invited_by: "pyrogram.User" = None, @@ -148,6 +153,7 @@ class ChatMember(Object): self.user = user self.status = status + self.title = title self.until_date = until_date self.joined_date = joined_date self.invited_by = invited_by @@ -193,6 +199,7 @@ class ChatMember(Object): return ChatMember( user=user, status="creator", + title=getattr(member, "rank", None), client=client ) @@ -211,6 +218,7 @@ class ChatMember(Object): return ChatMember( user=user, status="administrator", + title=member.rank, joined_date=member.date, invited_by=invited_by, promoted_by=pyrogram.User._parse(client, users[member.promoted_by]), From c0298542a5716f19e6ca1b4e48a7a37a60d82476 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sat, 7 Sep 2019 17:08:30 +0200 Subject: [PATCH 3/3] Add set_custom_title method --- pyrogram/client/methods/chats/__init__.py | 6 +- .../methods/chats/promote_chat_member.py | 3 +- .../client/methods/chats/set_custom_title.py | 90 +++++++++++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 pyrogram/client/methods/chats/set_custom_title.py diff --git a/pyrogram/client/methods/chats/__init__.py b/pyrogram/client/methods/chats/__init__.py index 13829029..b47385c4 100644 --- a/pyrogram/client/methods/chats/__init__.py +++ b/pyrogram/client/methods/chats/__init__.py @@ -31,6 +31,7 @@ from .get_chat_members import GetChatMembers from .get_chat_members_count import GetChatMembersCount from .get_dialogs import GetDialogs from .get_dialogs_count import GetDialogsCount +from .get_nearby_chats import GetNearbyChats from .iter_chat_members import IterChatMembers from .iter_dialogs import IterDialogs from .join_chat import JoinChat @@ -43,11 +44,11 @@ from .set_chat_description import SetChatDescription from .set_chat_permissions import SetChatPermissions from .set_chat_photo import SetChatPhoto from .set_chat_title import SetChatTitle +from .set_custom_title import SetCustomTitle from .unarchive_chats import UnarchiveChats from .unban_chat_member import UnbanChatMember from .unpin_chat_message import UnpinChatMessage from .update_chat_username import UpdateChatUsername -from .get_nearby_chats import GetNearbyChats class Chats( @@ -82,6 +83,7 @@ class Chats( AddChatMembers, DeleteChannel, DeleteSupergroup, - GetNearbyChats + GetNearbyChats, + SetCustomTitle ): pass diff --git a/pyrogram/client/methods/chats/promote_chat_member.py b/pyrogram/client/methods/chats/promote_chat_member.py index 9394841b..69562aec 100644 --- a/pyrogram/client/methods/chats/promote_chat_member.py +++ b/pyrogram/client/methods/chats/promote_chat_member.py @@ -97,7 +97,8 @@ class PromoteChatMember(BaseClient): invite_users=can_invite_users or None, pin_messages=can_pin_messages or None, add_admins=can_promote_members or None, - ) + ), + rank="" ) ) diff --git a/pyrogram/client/methods/chats/set_custom_title.py b/pyrogram/client/methods/chats/set_custom_title.py new file mode 100644 index 00000000..f13e667a --- /dev/null +++ b/pyrogram/client/methods/chats/set_custom_title.py @@ -0,0 +1,90 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2019 Dan Tès +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import Union + +from pyrogram.api import functions, types +from ...ext import BaseClient + + +class SetCustomTitle(BaseClient): + def set_custom_title( + self, + chat_id: Union[int, str], + user_id: Union[int, str], + title: str, + ) -> bool: + """Set a custom title to administrators or owners of a supergroup. + + Parameters: + chat_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target chat. + + user_id (``int`` | ``str``): + Unique identifier (int) or username (str) of the target user. + For a contact that exists in your Telegram address book you can use his phone number (str). + + title (``str``, *optional*): + A custom title that will be shown to all members instead of "Owner" or "Admin". + Pass None or "" (empty string) to remove the custom title. + + Returns: + ``bool``: True on success. + + Example: + .. code-block:: python + + # Set custom titles to owners or administrators of supergroups + app.set_custom_title(chat_id, user_id, "Custom Title") + """ + chat_id = self.resolve_peer(chat_id) + user_id = self.resolve_peer(user_id) + + r = self.send( + functions.channels.GetParticipant( + channel=chat_id, + user_id=user_id + ) + ).participant + + if isinstance(r, types.ChannelParticipantCreator): + admin_rights = types.ChatAdminRights( + change_info=True, + post_messages=True, + edit_messages=True, + delete_messages=True, + ban_users=True, + invite_users=True, + pin_messages=True, + add_admins=True, + ) + elif isinstance(r, types.ChannelParticipantAdmin): + admin_rights = r.admin_rights + else: + raise ValueError("Custom titles can only be applied to owners or administrators of supergroups") + + self.send( + functions.channels.EditAdmin( + channel=chat_id, + user_id=user_id, + admin_rights=admin_rights, + rank=title + ) + ) + + return True