dvec
dvec

Reputation: 347

Telethon. Make channel private

How to make channel private with Telethon? I can make it public with assigning username to them:

from telethon import TelegramClient, utils, functions
from telethon.tl.types import InputChannel

channel: InputChannel = utils.get_input_channel(await client.get_input_entity('https://t.me/joinchat/xxxxx'))
print(await client(functions.channels.UpdateUsernameRequest(channel=channel, username='fuckdadasfa21')))

But how can I make them private back? I did not find this information in the official documentation.

Upvotes: 0

Views: 730

Answers (1)

Tibebes. M
Tibebes. M

Reputation: 7538

You can just pass an empty string as a new username to make it private. And use ExportChatInviteRequest to get an invite link.

Here is a sample script:

from telethon import TelegramClient, utils, functions

API_ID = ...
API_HASH = " ... "

client = TelegramClient('session', api_id=API_ID, api_hash=API_HASH)


async def makeChannelPublic(channel_handler, new_handler):
    return await client(functions.channels.UpdateUsernameRequest(channel=channel_handler, username=new_handler))


async def makeChannelPrivate(channel_handler):
    await client(functions.channels.UpdateUsernameRequest(channel=channel_handler, username=''))
    return await client(functions.messages.ExportChatInviteRequest(
        peer=channel_handler
    ))

with client:
    # client.loop.run_until_complete(makeChannelPublic('https://t.me/joinchat/ .... ', 'publicusername'))
    invite_link = client.loop.run_until_complete(
        makeChannelPrivate('publicusername'))
    print(invite_link.link)  # invite link for the private channel

Upvotes: 1

Related Questions