Telegram Bot - how to get a group chat id?

I've been using telegram_bot, and trying to get groupChat id to send notifications to group chat, but don't know which methods I have to use for it.

For getting chat id I use to message.chat.id when the bot participated in the chat but which I have to use for getting group chat id can't find/


Solution 1:

In order to get the group chat id, do as follows:

  1. Add the Telegram BOT to the group.

  2. Get the list of updates for your BOT:

     https://api.telegram.org/bot<YourBOTToken>/getUpdates
    

    Ex:

     https://api.telegram.org/bot123456789:jbd78sadvbdy63d37gda37bd8/getUpdates
    
  3. Look for the "chat" object:

{"update_id":8393,"message":{"message_id":3,"from":{"id":7474,"first_name":"AAA"},"chat":{"id":<group_ID>,"title":""},"date":25497,"new_chat_participant":{"id":71,"first_name":"NAME","username":"YOUR_BOT_NAME"}}}

This is a sample of the response when you add your BOT into a group.

  1. Use the "id" of the "chat" object to send your messages.

  2. (If you created the new group with the bot and you only get {"ok":true,"result":[]}, remove and add the bot again to the group)

  3. Private chart only works in image argoprojlabs/argocd-notifications:v1.1.0 or above.

Solution 2:

Here is the sequence that worked for me after struggling for several hours:

Assume the bot name is my_bot.

1- Add the bot to the group.
Go to the group, click on group name, click on Add members, in the searchbox search for your bot like this: @my_bot, select your bot and click add.

2- Send a dummy message to the bot.
You can use this example: /my_id @my_bot
(I tried a few messages, not all the messages work. The example above works fine. Maybe the message should start with /)

3- Go to following url: https://api.telegram.org/botXXX:YYYY/getUpdates
replace XXX:YYYY with your bot token

4- Look for "chat":{"id":-zzzzzzzzzz,
-zzzzzzzzzz is your chat id (with the negative sign).

5- Testing: You can test sending a message to the group with a curl:

curl -X POST "https://api.telegram.org/botXXX:YYYY/sendMessage" -d "chat_id=-zzzzzzzzzz&text=my sample text"

If you miss step 2, there would be no update for the group you are looking for. Also if there are multiple groups, you can look for the group name in the response ("title":"group_name").

Hope this helps.

Solution 3:

As of May 2021, simply:

  • Invite @RawDataBot to your group.

Upon joining it will output a JSON file where your chat id will be located at message.chat.id.

"message": {
    "chat": {
        "id": -210987654,
        "title": ...,
        "type": "group",
        ...
    }
    ...
}

Be sure to kick @RawDataBot from your group afterwards.