Skip to content

Commit 9d375f7

Browse files
authored
Add new method transfer_chat_ownership (#49)
1 parent 7cd6926 commit 9d375f7

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def get_title_list(s: str) -> list:
239239
set_send_as_chat
240240
set_chat_protected_content
241241
get_created_chats
242+
transfer_chat_ownership
242243
""",
243244
chat_topics="""
244245
Chat Forum Topics

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
1414
| Scheme layer used: 184 |
1515
+------------------------+
1616

17+
- Added the :meth:`~pyrogram.Client.transfer_chat_ownership` (`#49 <https://github.com/TelegramPlayGround/pyrogram/pull/49>`__)
1718
- Added the class :obj:`~pyrogram.types.RefundedPayment`, containing information about a refunded payment.
1819
- Added the field ``refunded_payment`` to the class :obj:`~pyrogram.types.Message`, describing a service message about a refunded payment.
1920
- `View new and changed raw API methods <https://telegramplayground.github.io/TG-APIs/TL/diff/tdesktop.html?from=183&to=184>`__.

pyrogram/methods/chats/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from .unpin_all_chat_messages import UnpinAllChatMessages
5959
from .unpin_chat_message import UnpinChatMessage
6060
from .get_created_chats import GetCreatedChats
61+
from .transfer_chat_ownership import TransferChatOwnership
6162

6263

6364
class Chats(
@@ -103,5 +104,6 @@ class Chats(
103104
SetSendAsChat,
104105
SetChatProtectedContent,
105106
GetCreatedChats,
107+
TransferChatOwnership,
106108
):
107109
pass
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 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+
from pyrogram.utils import compute_password_check
24+
25+
26+
class TransferChatOwnership:
27+
async def transfer_chat_ownership(
28+
self: "pyrogram.Client",
29+
chat_id: Union[int, str],
30+
user_id: Union[int, str],
31+
password: str,
32+
) -> bool:
33+
"""Changes the owner of a chat.
34+
35+
Requires owner privileges in the chat. Available only for supergroups and channel chats.
36+
37+
.. include:: /_includes/usable-by/users.rst
38+
39+
Parameters:
40+
chat_id (``int`` | ``str``):
41+
Unique identifier (int) or username (str) of the target chat.
42+
43+
user_id (``int`` | ``str``):
44+
Unique identifier (int) or username (str) of the new owner.
45+
The ownership can't be transferred to a bot or to a deleted user.
46+
47+
password (``str``):
48+
The 2-step verification password of the current user.
49+
50+
Returns:
51+
``bool``: True on success.
52+
53+
Raises:
54+
ValueError: In case of invalid parameters.
55+
RPCError: In case of a Telegram RPC error.
56+
57+
Example:
58+
.. code-block:: python
59+
60+
await app.transfer_chat_ownership(chat_id, user_id, "password")
61+
"""
62+
63+
peer_channel = await self.resolve_peer(chat_id)
64+
peer_user = await self.resolve_peer(user_id)
65+
66+
if not isinstance(peer_channel, raw.types.InputPeerChannel):
67+
raise ValueError("The chat_id must belong to a channel/supergroup.")
68+
69+
if not isinstance(peer_user, raw.types.InputPeerUser):
70+
raise ValueError("The user_id must belong to a user.")
71+
72+
r = await self.invoke(
73+
raw.functions.channels.EditCreator(
74+
channel=peer_channel,
75+
user_id=peer_user,
76+
password=compute_password_check(
77+
await self.invoke(raw.functions.account.GetPassword()), password
78+
),
79+
)
80+
)
81+
82+
return bool(r)

0 commit comments

Comments
 (0)