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 ch
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.
If you are implementing your bot, keep stored a group name -> id table, and ask it with a command. Then you can also send per name.
Using python and telethon it's very easy to get chat id. This solution is best for those who work with telegram API.
If you don't have telethon, run this:
pip install telethon
If you don't have a registered app with telegram, register one: The link is this: 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 may want to send a message to your group so the group show up in top of the list.
bot receives the following message originated on a Telegram group having a bot co-existing:
extract data as required
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
// GmailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Telegram Bot Update", JSON.stringify(contents, null, 4));
var chat_id = contents.message.chat.id;
var text = contents.message.text;
var name = contents.message.from.first_name + " " + contents.message.from.last_name;
var sResponse = telegramBotMachine( chat_id, text, name );
}
/* contents
{
"update_id": 20383255,
"message": {
"message_id": 147,
"from": {
"id": 999999999,
"is_bot": false,
"first_name": "Trajano",
"last_name": "Roberto",
"username": "TrajanoRoberto",
"language_code": "en"
},
"chat": {
"id": -666666666,
"title": "Test Ataque Media Flamengo",
"type": "group",
"all_members_are_administrators": true
},
"date": 1585450075,
"text": "Menu"
}
}
You can retrieve the group ID the same way. It appears in the message body as message.chat.id
and it's usually a negative number, where normal chats are positive.
Group IDs and Chat IDs can only be retrieved from a received message, there are no calls available to retrieve active groups etc. You have to remember the group ID when you receive the message and store it in cache or something similar.
As of March 2020, simply:
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.