Telethon。将频道设为私有

问题描述 投票:0回答:1

如何通过Telethon将频道设为私有?我可以通过为其分配用户名来公开:

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')))

但是我如何将它们设为私密?我没有在官方文档中找到此信息。

python python-3.x telegram telethon
1个回答
0
投票

您可以仅将一个空字符串作为新用户名进行传递,以使其私有。并使用ExportChatInviteRequest获取邀请链接。

这里是示例脚本:

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

© www.soinside.com 2019 - 2024. All rights reserved.