Skip to content
1 change: 1 addition & 0 deletions compiler/docs/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def get_title_list(s: str) -> list:
set_send_as_chat
set_chat_protected_content
get_created_chats
transfer_chat_ownership
""",
chat_topics="""
Chat Forum Topics
Expand Down
1 change: 1 addition & 0 deletions docs/source/releases/changes-in-this-fork.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ If you found any issue or have any suggestions, feel free to make `an issue <htt
| Scheme layer used: 184 |
+------------------------+

- Added the :meth:`~pyrogram.Client.transfer_chat_ownership` (`#49 <https://github.com/TelegramPlayGround/pyrogram/pull/49>`__)
- Added the class :obj:`~pyrogram.types.RefundedPayment`, containing information about a refunded payment.
- Added the field ``refunded_payment`` to the class :obj:`~pyrogram.types.Message`, describing a service message about a refunded payment.
- `View new and changed raw API methods <https://telegramplayground.github.io/TG-APIs/TL/diff/tdesktop.html?from=183&to=184>`__.
Expand Down
2 changes: 2 additions & 0 deletions pyrogram/methods/chats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from .unpin_all_chat_messages import UnpinAllChatMessages
from .unpin_chat_message import UnpinChatMessage
from .get_created_chats import GetCreatedChats
from .transfer_chat_ownership import TransferChatOwnership


class Chats(
Expand Down Expand Up @@ -103,5 +104,6 @@ class Chats(
SetSendAsChat,
SetChatProtectedContent,
GetCreatedChats,
TransferChatOwnership,
):
pass
82 changes: 82 additions & 0 deletions pyrogram/methods/chats/transfer_chat_ownership.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.

from typing import Union

import pyrogram
from pyrogram import raw
from pyrogram.utils import compute_password_check


class TransferChatOwnership:
async def transfer_chat_ownership(
self: "pyrogram.Client",
chat_id: Union[int, str],
user_id: Union[int, str],
password: str,
) -> bool:
"""Changes the owner of a chat.

Requires owner privileges in the chat. Available only for supergroups and channel chats.

.. include:: /_includes/usable-by/users.rst

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 new owner.
The ownership can't be transferred to a bot or to a deleted user.

password (``str``):
The 2-step verification password of the current user.

Returns:
``bool``: True on success.

Raises:
ValueError: In case of invalid parameters.
RPCError: In case of a Telegram RPC error.

Example:
.. code-block:: python

await app.transfer_chat_ownership(chat_id, user_id, "password")
"""

peer_channel = await self.resolve_peer(chat_id)
peer_user = await self.resolve_peer(user_id)

if not isinstance(peer_channel, raw.types.InputPeerChannel):
raise ValueError("The chat_id must belong to a channel/supergroup.")

if not isinstance(peer_user, raw.types.InputPeerUser):
raise ValueError("The user_id must belong to a user.")

r = await self.invoke(
raw.functions.channels.EditCreator(
channel=peer_channel,
user_id=peer_user,
password=compute_password_check(
await self.invoke(raw.functions.account.GetPassword()), password
),
)
)

return bool(r)