strannik-j
strannik-j

Reputation: 99

Telethon. How to create a public/private channel?

How to create a public/private channel with Telethon? I did not find this information in the official documentation.

Upvotes: 4

Views: 6150

Answers (2)

Hardest
Hardest

Reputation: 358

The snipped above works but in sync mode, the asyncio call is the following:

from telethon import TelegramClient    
from telethon.tl.functions.channels import InviteToChannelRequest, CreateChannelRequest, CheckUsernameRequest, UpdateUsernameRequest
from telethon.tl.types import InputPeerChannel
    

tm = TelegramClient(username, api_id, api_hash)
tm.start(user_phone)

async def proc():
    created_private_channel = await tm(CreateChannelRequest(
        "Channel Title",
        "Channel description",
        broadcast=True, megagroup=False))
    new_channel_id = created_private_channel.created_private_channel.chats[0].id
    new_channel_access_hash = created_private_channel.chats[0].access_hash
    check_channelname_result = await tm(CheckUsernameRequest(
        InputPeerChannel(channel_id=new_channel_id,
                         access_hash=new_channel_access_hash), desired_ch_name))
    if check_channelname_result:
        update_response = await tm(UpdateUsernameRequest(
            InputPeerChannel(channel_id=new_channel_id,
                             access_hash=new_channel_access_hash), desired_ch_name))
        print(f'update_response={update_response}')
    else:
        print(f'channelname={desired_ch_name} already taken')

tm.loop.run_until_complete(proc())

Upvotes: 1

tashakori
tashakori

Reputation: 2441

you can use this procedure to create private channels(and also making them public by assigning username to them):

from telethon.tl.functions.channels import CreateChannelRequest, CheckUsernameRequest, UpdateUsernameRequest
from telethon.tl.types import InputChannel, InputPeerChannel
createdPrivateChannel = client(CreateChannelRequest("title","about",megagroup=False))

#if you want to make it public use the rest
newChannelID = createdPrivateChannel.__dict__["chats"][0].__dict__["id"]
newChannelAccessHash = createdPrivateChannel.__dict__["chats"][0].__dict__["access_hash"]
desiredPublicUsername = "myUsernameForPublicChannel"
checkUsernameResult = client(CheckUsernameRequest(InputPeerChannel(channel_id=newChannelID, access_hash=newChannelAccessHash), desiredPublicUsername))
if(checkUsernameResult==True):
    publicChannel = client(UpdateUsernameRequest(InputPeerChannel(channel_id=newChannelID, access_hash=newChannelAccessHash), desiredPublicUsername))

Upvotes: 10

Related Questions