如何通过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')))
但是我该如何将它们设为私有?我在官方文档中找不到此信息。
答案 0 :(得分:1)
您只需传递一个空字符串作为新用户名即可将其设为私有。并使用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