Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dazeb/5d6587e9f778ca37345a4f4956031ec1 to your computer and use it in GitHub Desktop.

Select an option

Save dazeb/5d6587e9f778ca37345a4f4956031ec1 to your computer and use it in GitHub Desktop.
How to retrieve Telegram Chat ID's, Group ID's and Group Topic ID's.

How to Retrieve Your Telegram Bot's Chat ID

Setting Up Your Telegram Bot and Acquiring the Bot Token

  1. Launch the Telegram app and use the search function to locate @BotFather.

  2. Initiate interaction by selecting Start.

  3. Access the Menu and choose /newbot or simply type /newbot into the chat and press Send.

  4. Proceed with the provided instructions until you receive a message similar to the following:

  5. Success! Your new bot is ready. You can find it at t.me/your_bot.
    Feel free to add a description...
    
    Here is your token for the HTTP API:
    63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c
    Guard this token carefully and store it securely, as it can be utilized by others to manage your bot.
    
    For detailed information on the Bot API, refer to this documentation: https://core.telegram.org/bots/api
    
  6. Take note of your bot token 63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c and ensure its confidentiality.

Obtaining the Chat ID for a Private Conversation

  1. Locate and initiate a conversation with your newly created Telegram bot.

  2. Either click Start or send any message to activate the bot.

  3. Navigate to the following URL in your web browser: https://api.telegram.org/bot{your_bot_token}/getUpdates

    • Remember to prepend the word bot to your token.
    • For example: https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/getUpdates
  4. A JSON response will be displayed as follows:

  5. {
      "ok": true,
      "result": [
        {
          "update_id": 83xxxxx35,
          "message": {
            "message_id": 2643,
            "from": {...},
            "chat": {
              "id": 21xxxxx38,
              "first_name": "...",
              "last_name": "...",
              "username": "@username",
              "type": "private"
            },
            "date": 1703062972,
            "text": "/start"
          }
        }
      ]
    }
    
  6. Locate the result[0].message.chat.id value to find your Chat ID, which in this case is 21xxxxx38.

  7. Test sending a message with the following URL: https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/sendMessage?chat_id=21xxxxx38&text=test123

  8. If the bot token and chat id are correctly configured, the message test123 will appear in your Telegram bot chat.

Retrieving the Chat ID for a Telegram Channel

  1. Include your Telegram bot as a member of the desired channel.

  2. Post a message within the channel.

  3. Visit https://api.telegram.org/bot{your_bot_token}/getUpdates

  4. The JSON response will appear as follows:

    {
      "ok": true,
      "result": [
        {
          "update_id": 838xxxx36,
          "channel_post": {...},
            "chat": {
              "id": -1001xxxxxx062,
              "title": "....",
              "type": "channel"
            },
            "date": 1703065989,
            "text": "test"
          }
        }
      ]
    }
    
  5. Identify the result[0].channel_post.chat.id value to obtain your Chat ID, which is -1001xxxxxx062 in this example.

  6. Attempt message delivery using the URL: https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/sendMessage?chat_id=-1001xxxxxx062&text=test123

  7. Upon successful configuration of the bot token and chat id, the message test123 will be delivered to your Telegram channel.

Acquiring the Chat ID for a Group Conversation

The most straightforward method to obtain a group chat ID is via the Telegram desktop application.

  1. Open the Telegram desktop app.
  2. Integrate your Telegram bot into the group chat.
  3. Post a message within the group chat.
  4. Right-click on the message and select Copy Message Link.
    • The link will resemble: https://t.me/c/194xxxx987/11/13
    • The format is: https://t.me/c/{group_chat_id}/{group_topic_id}/{message_id}
    • The Chat ID in this instance is 194xxxx987
  5. For API usage, prefix the group chat ID with -100, resulting in -100194xxxx987.
  6. Send a test message with the URL: https://api.telegram.org/bot63xxxxxx71:AAFoxxxxn0hwA-2TVSxxxNf4c/sendMessage?chat_id=-100194xxxx987&text=test123
  7. With the correct bot token and chat id, the message test123 will be received in the group chat.

Obtaining the Chat ID for a Specific Topic in a Group Chat

To send a message to a particular topic within a Telegram group, you must acquire the topic ID.

  1. Following the steps above, after selecting Copy Message Link, you will obtain a link like: https://t.me/c/194xxxx987/11/13, where the group Topic ID is 11.
  2. Utilize the message_thread_id parameter as shown: https://api.telegram.org/bot783114779:AAEuRWDTFD2UQ7agBtFSuhJf2-NmvHN3OPc/sendMessage?chat_id=-100194xxxx987&message_thread_id=11&text=test123
  3. With the appropriate bot token and chat id, the message test123 will be sent to the specified topic within the group chat.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment