Transfer Telegram channel ownership using pyrogram library

Question:

How to transfer channel ownership?

I want to transfer ownership of a Telegram channel using pyrogram.

There is a promote_chat_member method but it does not have an owner input parameter. There is also a pyrogram.raw.functions.channels.EditCreator method but I don’t understand how to use it.

Asked By: Kuchizu

||

Answers:

Try this in order to use the pyrogram.raw.functions.channels.EditCreator method

NOTES:

  • This operation must be done with an user account. It’s necessary that you pass your phone number to the Client. (This operations can’t be performed by bots)
  • The user must be a member of the channel. The code will promote the
    user to Admin.
  • Get new_owner_id and channel_id using IDBot in Telegram (@username_to_id_bot). You’ll need to send the username of the new owner and the join link of the channel to the bot.
  • Set a 2FA password in your account at least 1 week before running the
    code (it’s an API requirement)

Requirements

pip install pyrogram
pip install nest-asyncio

CODE

from pyrogram import Client
from pyrogram.raw.functions.channels import EditCreator
from pyrogram.raw.functions.account import GetPassword
import nest_asyncio

nest_asyncio.apply()

app = Client("app_name_you_like",api_id="your_api_id",api_hash="your_api_hash",phone_number="your_phone_number")

channel_id = channel_id #int -> enter your channel_id here
user_id = new_owner_id #int -> enter your new_owner_id here

async def make_owner():
    await app.start()
    channel_data = await app.resolve_peer(channel_id)
    user_data = await app.resolve_peer(user_id)
    password = await app.invoke(GetPassword())
    #Make user Admin
    await app.promote_chat_member(channel_id, user_id)
    #Make user Owner
    await app.invoke(EditCreator(channel=channel_data,user_id=user_data,password=password))
    await app.stop()


app.run(make_owner())

Extra Notes:
If you don’t have your Telegram API Credentials you can get them in this link

Answered By: Pedro Rocha

Here’s a sample code how you can do it

from pyrogram import Client

# Creating Pyrogram client
app = Client("my_account")
app.start()


# replace the below channel_username with Channel you want to transfer ownership of
channel = app.get_chat("channel_username")

# Get the ID of the user whom you want to transfer
new_owner_id = 00000  # Replace 00000 wit the id of new owner

# Get the current owner ID of the channel
current_owner_id = channel.owner_id

# Transfer Ownsership
app.edit_administrator(
    chat_id=channel.id,
    user_id=new_owner_id,
    is_owner=True,
    can_change_info=True,
    can_invite_users=True,
    can_delete_messages=True,
    can_restrict_members=True,
    can_pin_messages=True,
    can_promote_members=True
)

# Revoke the old owner's admin permissions
app.edit_administrator(
    chat_id=channel.id,
    user_id=current_owner_id,
    is_owner=False,
    can_change_info=False,
    can_invite_users=False,
    can_delete_messages=False,
    can_restrict_members=False,
    can_pin_messages=False,
    can_promote_members=False
)

# Logout
app.stop()
Answered By: Hamza

Only working method:

from pyrogram.utils import compute_password_check
from pyrogram.raw.functions.account import GetPassword
from pyrogram.raw.functions.channels import EditCreator

await app.invoke(EditCreator(channel=await app.resolve_peer("channel_username"), user_id=await app.resolve_peer("new_owner_username"), password=compute_password_check(await app.invoke(GetPassword()), password="password_here")))
Answered By: Kuchizu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.