skip to Main Content

I’m trying to get the invite link of a public channel and a public group.

I tried using the ExportChatInviteRequest function but it raises a ChatAdminRequiredError.

The thing I don’t understand is why can I see and get the invite link of a public channel group with the telegram app but can’t get it with telethon?

I use version 1.26.1:

from telethon.tl.functions.messages import ExportChatInviteRequest

async def main():
    chat = await client.get_entity('https://t.me/bestmemes')
    invite = await client(ExportChatInviteRequest(chat))
    print(invite)

raises:

telethon.errors.rpcerrorlist.ChatAdminRequiredError: Chat admin privileges are required to do that in the specified chat (for example, to send a message in a channel which is not yours), or invalid permissions used for the channel or group (caused by ExportChatInviteRequest)

can someone help me, please?

I can see the invite of the given channel via the telegram app:
enter image description here

2

Answers


  1. I did this:

    1. Create a private chat with a bot or a friend
    2. Retrieve api_id and api_hash from https://my.telegram.org/auth
      • Login to your telegram account using your phone number
      • Click "API Development tools"
      • Fill in your application details
      • Click on "Create application" at the end
      • Copy App api_id and App api_hash
    from telethon import TelegramClient
    from telethon.tl.functions.messages import ExportChatInviteRequest
    
    
    session = 'Test'  # The first-part name of a sqlite3 database - "Test.session"
                      # Contains user name and password to your Telegram account
    
    # App ID and hash key to identify your Telegram App
    api_id = 12345                                  # Use your own api_id
    api_hash = '1234567890ABCDEFGHIJKLMNOPQRSTUV'   # Use your own api_hash
    
    # Create a connection to your telegram account using this app
    # You will be asksed phone number, password, which will be stored into
    # "Test.session" sqlite3 database
    client = TelegramClient(session, api_id, api_hash)
    
    async def main():
        # Send a hello message to yourself
        await client.send_message('me', 'Hello!')
        
        # Send a hello message to a private chat group
        await client.send_message('https://t.me/+xxxxxx', 'Hello!')
    
        # Get a specific chat via its primary link
        group = await client.get_entity('https://t.me/+xxxxxx')
    
        # Create additional link
        invite = await client(ExportChatInviteRequest(group))
        print(invite.link)  # Print secondary link
      
    with client:
        client.loop.run_until_complete(main())
    
    Login or Signup to reply.
  2. The ExportChatInviteRequest method you are trying to use creates a private chat link. Obviously you can’t do this if you’re not an administrator

    Here is a small code example that gets links to all open chats

    async def get_chat_links():
        dump = {}
    
        # get all dialogs
        for dialog in await client.get_dialogs():
            chat = dialog.entity
    
            # if the dialog does not have a title attribute, then it is not a group
            if hasattr(chat, 'username') and hasattr(chat, 'title'):
                dump[chat.title] = f'https://t.me/{chat.username}'
    
        # for example, let's save everything to a file
        with open('dump.json', 'w', encoding='utf-8') as file:
            json.dump(dump, file, indent=4)
    
    with client:
        client.loop.run_until_complete(get_chat_links())
    

    I would like to add some explanation about hasattr(chat, 'username'). For some chats you will have https://t.me/None. These are just the same closed chats that do not have an open link to join. But the ExportChatInviteRequest function will not work for these chats either, because it CREATES a link, and does not receive it. And if you are not an administrator in this chat you will get an error

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search