Search code examples
telegramtelegram-bot

How to add a bot to a Telegram Group via API?


I want to add a bot to a Telegram Group, but not via manual approach:

but via API instead, i.e., something like this:

which I had not been able to make it working. I've tried.

https://telegram.me/mybotname?startgroup=mygroupname,

or /start@mybotname within "mygroupname". Neither way get my bot added to the Telegram Group.

The reason I need to add a bot to Telegram Groups via API is that I have over a dozen of such Telegram Groups to add this new bot to.


Solution

  • 2 years old question but, that's what helped me:

    Use user API (not bot API) and messages.addChatUser method.
    It works not only for adding users, but also bots.
    https://core.telegram.org/method/messages.addChatUser

    For getting bot_id use https://api.telegram.org/bot{TOKEN}/getMe (use token from bot father)

    Here is example of the code on python:

    from telethon.sync import TelegramClient
    from telethon.sessions import StringSession
    from telethon.tl.functions.messages import AddChatUserRequest
    
    session = StringSession("")  # You should put your string session here
    client = TelegramClient(session, api_id, api_hash)
    
    async def run():
        await client.connect()  # This assumes you have already authenticated 
    
        result = await client(AddChatUserRequest(
            chat_id=chat_id,
            user_id=bot_id,
            fwd_limit=43,
        ))
        print(result)  # prints the result
    
    with client:
        client.loop.run_until_complete(run())