【问题标题】:Telethon. How to create a public/private channel?马拉松。如何创建公共/私人频道?
【发布时间】:2018-09-04 07:25:39
【问题描述】:

如何使用 Telethon 创建公共/私人频道?我在官方文档中没有找到这个信息。

【问题讨论】:

    标签: python python-3.x telegram telethon


    【解决方案1】:

    您可以使用此过程来创建私人频道(也可以通过为其分配用户名将其公开):

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

    【讨论】:

    • 我如何创建邀请链接?
    • 请深入研究库 API,以便您可以找到所需的任何内容:exportInviteResult = client(ExportInviteRequest(InputPeerChannel(channel_id=createdSupergroup.__dict__["chats"][0].__dict__["id" ], access_hash=createdSupergroup.__dict__["chats"][0].__dict__["access_hash"]))
    • 从 telethon.tl.functions.channels 导入 ExportInviteRequest
    • 现在不行了:from telethon.tl.functions.channels import ExportInviteRequest ImportError: cannot import name 'ExportInviteRequest' from 'telethon.tl.functions.channels'
    • 谢谢。一个好主意是使用 await(和异步模式)。
    【解决方案2】:

    上面的片段可以工作,但在同步模式下,asyncio 调用如下:

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

    【讨论】:

      猜你喜欢
      • 2021-04-06
      • 2017-12-25
      • 1970-01-01
      • 2019-12-11
      • 2019-04-27
      • 2020-02-05
      • 2018-07-27
      • 2019-11-30
      • 2018-07-22
      相关资源
      最近更新 更多