Telegram: How to find Group Chat ID?
Asked Answered
I

4

8

I've been trying to solve this for three days and none pf the solutions I've found online seem to work, so I am going to have to ask for help.

I want to create a Telegram "Group Chat" to be used by members of a club I'm in. I created the chat named with the initials of the club, like: "ABCD" and added some members. Now I want to automate the sending of occasional messages to the group for all members to see. Weather forecast, random photograph from our gallery, etc.

Using @BotFather I created a bot called "ABCDbot" and noted the token for that bot. Now I have two "ABCD"s on my browser left side-panel. Selecting one gives me "ABCD bot" and selecting the other gives me "ABCD 123 members".

Using a perl script and LWP I can send a photo using

#!/usr/bin/perl -w

use feature 'say';
use LWP;
my $api = LWP::UserAgent->new ();

my $chat_id = '1234567890';
my $photo = '/home/user/gallery/photo999.jpg';

my $response = $api->post(
  "https://api.telegram.org/bot<ABCDbot's token>/sendPhoto",
  [ 'chat_id' => $chat_id,
    'caption' => 'Photo Randomly Selected by the gallery',
    'photo' => $photo,
  ],
  'Content_Type' => 'form-data',
);
if ($response->is_success) {
  say "Response..... Success!";
} else {
  say "Response..... Failure!";
}

This works, providing I give it a legitimate chat_id and a legitimate file to send. But the trouble is: I can't find the chat_id for the group chat with 123 members! Every method I've tried now proves to be obsolete or simply doesn't return the desired chat_id for the ABCD group chat. I can get my own chat ID or that of individual members of the group, or of the bot itself, and can successfully send photos, messages, etc to those destinations, but I just can't send anything to the group.

Can anybody walk me through the process of getting the chat_id for my group chat? Or direct me to a document describing an up-to-date, working method for obtaining same?

Assistance much appreciated.

Ila answered 16/6, 2022 at 5:3 Comment(0)
G
22

Method 1 (Web A)

This is based on JayeshRocks’s question with some extra steps to make the ID work with Bot API.

  1. Login to Telegram Web A.
  2. Open the chat you want to get its ID.
  3. Your browser’s address should look like https://web.telegram.org/a/#-1527776602.
  4. Remove the scheme, the hostname and the path, keeping the anchor so your result looks like #-1527776602.
  5. Replace “#-” with “-100” so it looks like -1001527776602.
  6. You can now use your final result which should look like -1001527776602.

Method 2 (Private Supergroups)

If the chat is a private channel/supergroup, you can do the following:

  1. Copy a link of a message. (It will look like https://t.me/c/1527776602/1002.)
  2. Remove the protocol and domain name, so it looks like c/1527776602/1002.
  3. Remove the first and last path, so it looks like 1527776602.
  4. Append “-100” to the beginning of the result, so it looks like -1001527776602.
  5. You can now use your final result which looks like -1001527776602.

Method 3 (Third-Party Bots)

If you trust 3rd party bots, there are many of them. A known one is @MissRose_bot which you can add it to your group and use its /id command. Another one is @usinfobot which works inline and only for public chats.

Ghislainegholston answered 16/6, 2022 at 16:56 Comment(2)
I tried method #1 (WebZ) and was successful when sending to the group chat using the chat_id derived from the URL. I will experiment more with different message types, but in the mean time I send you my thanks.Ila
When you think this answer resolves your problem, you can click the checkmark on the left to mark your question as resolved.Ghislainegholston
C
1

To get a telegram group ID you need to open the group on https://web.telegram.org/z/ when you do so click on the group you want to gain the ID of then if you look at the URL it will say something like https://web.telegram.org/z/#-1234567 and the numbers there is the ID of the telegram group! Hope this helps

Courtyard answered 16/6, 2022 at 5:14 Comment(1)
Ok, I tried Opening the group on web.telegram.org/z and got a number like "-1234567890'. Using this number as a chat_id, I send a photo using the code given above. The response I get is: {"ok":false,"error_code":400,"description":"Bad Request: chat not found"} I am including the leading dash/minus sign. Any ideas?Ila
H
0

Here's a Python code for a simple Telegram bot that prints any chat ID. Add your bot to the group chat and send a message mentioning the bot by @ in the group chat.

To create a bot and get the API token, write to @BotFather.

import telebot
bot = telebot.TeleBot('token')


@bot.message_handler(func=lambda _: True)
def handle(message):
    bot.send_message(message.chat.id, f'{message.chat.id}')


def main():
    bot.infinity_polling()


if __name__ == "__main__":
    main()
Helmand answered 30/8, 2023 at 9:24 Comment(0)
S
0

I tried the anwers above but did not succeed.

The only way to get the correct chat id of a group chat was to add @getmyid_bot to the group of interest and then send a message to the group. The answer is the users chat id and the chat id of the group.

Warning: Verify, that you add the correct bot to your group (name exactly as mentioned above) - which can only be distinguished in the app as far as I know.

Sawyor answered 12/8 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.