Search code examples
pythonbotstelegramtelethon

How do I create invites with usage limit to a private channel on Telegram with Telethon?


I'm using Telethon to create a Telegram BOT and I needed to automate a task of generating a one time use invite

I tried searching about it but didn't find anything at their docs neither stackoverflow (i did find somethings about invites with this library but didn't understood how to handle my situation)

Anything that may help me with this is welcome!!

Thank you in advance!


Solution

  • From telethon.dev, You can mod the example like:

    from telethon.sync import TelegramClient
    from telethon import functions, types
    
    session_file_name = ""        # TODO: session file name without extension
    api_id = ""                   # TODO: api id 
    api_hash = ""                 # TODO: api hash 
    channel_id = -1001109500936   # TODO: channel ID 
    
    with TelegramClient(session_file_name, api_id, api_hash) as client:
        result = client(functions.messages.ExportChatInviteRequest(
            peer=client.get_input_entity(channel_id),
            legacy_revoke_permanent=True,
            expire_date=None, # Set expiry date if needed like datetime.datetime(2018, 6, 25),
            usage_limit=1     # Only one person can use your invite link
        ))
        print(result.stringify())