Telegram bot - how to get group chat id?

I used telegram_bot and tried to get the groupChat ID to send notifications to group chat, but I donโ€™t know the methods that I should use for this.

To get the chat identifier, I use message.chat.id when the bot participated in the chat, but which I must use to get the group chat identifier, I can not find /

+124
telegram telegram-bot
Sep 06 '15 at 12:58
source share
5 answers

To get the group chat ID, do the following:

  • Add the TeleOT BOT to the group.

  • Get the list of updates for your BOT:

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

    Example:

     https://api.telegram.org/botjbd78sadvbdy63d37gda37bd8/getUpdates 
  • Look for the "chat" object:

{"update_id": 8393, "message": {"message_id": 3, "c": {"ID": 7474, "first_name": "AAA"}, "chat": {"ID" :, "name ":"}, "date": 25497, "new_chat_participant": {"ID": 71, "first_name": "NAME", "username": "YOUR_BOT_NAME"}}}

This is an example response when you add your BOT to a group.

  1. Use the "id" of the "chat" object to send your messages.
+160
Sep 14 '15 at 7:10
source share

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

Suppose the bot name is my_bot .

1- Add the bot to the group.
Go to the group, click on the name of the group, click "Add members", in the search field find your bot, like this: @my_bot, select your bot and click "Add".

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

3- Go to the 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 a negative sign).

5- Testing:. You can test sending a message to a group using curl:

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

If you skip step 2, you wonโ€™t find updates for the group. In addition, if there are several groups, you can search for the group name in the response ("title": " group_name ").

Hope this helps.

+116
Jul 15 '16 at 6:01
source share

As of the end of 2017, it is simple:

  • Invite @RawDataBot to your group.

Once attached, it will output a JSON file where your chat id will be located in message.chat.id .

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

After that, be sure to remove @RawDataBot from your group.

+68
Sep 15 '17 at 20:28
source share

You can get the group id the same way. It appears in the body of the message as message.chat.id , and it is usually a negative number, where regular chats are positive.

Group IDs and chat IDs can only be obtained from a received message, there are no available calls to retrieve active groups, etc. You must remember the group ID when you receive the message, and store it in the cache or something similar.

+7
Sep 07 '15 at 9:25
source share

Using python and telethon, it is very easy to get the chat id. This solution is best for those who work with telegram APIs.

If you do not have a telethon, run this:

 pip install telethon 

If you do not have a registered application with a telegram, register it: enter image description here The link is: https://my.telegram.org/

Then run the following code:

 from telethon import InteractiveTelegramClient from telethon.utils.tl_utils import get_display_name client = InteractiveTelegramClient('session_id', 'YOUR_PHONE_NUMBER', api_id=1234YOURAPI_ID, api_hash='YOUR_API_HASH') dialog_count = 10 dialogs, entities = client.get_dialogs(dialog_count) for i, entity in enumerate(entities): i += 1 # 1-based index print('{}. {}. id: {}'.format(i, get_display_name(entity), entity.id)) 

You can send a message to your group so that the group appears at the top of the list.

+5
Jun 10 '17 at 16:17
source share



All Articles