89

How to obtain user chat_id in Telegram bot API? The documentation says:

Integer | Unique identifier for the message recipient — User or GroupChat id

3
  • 2
    I just noticed doing some test that for private chat chat_id and from_user.id is the same. Assuming this is a rule ( I haven't found some reference ), if you find out his user_id ( maybe from an Update in a group ) you cat chat to him privately. Someone can refute this?
    – Thomas8
    Jan 3, 2016 at 22:50
  • I have the same question! But without the user needing to send me a message. Feb 12, 2022 at 18:11
  • According to Telegram website "Bots can't initiate conversations with users. A user must either add them to a group or send them a message first." Please read this for further information: core.telegram.org/bots/#4-how-are-bots-different-from-humans Feb 13, 2022 at 9:09

12 Answers 12

57

The message updates you receive via getUpdates or your webhook will contain the chat ID for the specific message. It will be contained under the message.chat.id key.

This seems like the only way you are able to retrieve the chat ID. So if you want to write something where the bot initiates the conversation you will probably have to store the chat ID in relation to the user in some sort of key->value store like MemCache or Redis.

I believe their documentation suggests something similar here, https://core.telegram.org/bots#deep-linking-example. You can use deep-linking to initiate a conversation without requiring the user to type a message first.

7
  • Yes, I agree. Bot is able to store User data and ID in a persistent storage, for successivly initiate a unsolicited "conversation". BTW, As far as I know chat_ID are unique. Dec 1, 2015 at 13:47
  • It is mentioned under deep linking: 'Show our user the button telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA' How to show a button?
    – kb0000
    Dec 7, 2015 at 4:20
  • 1
    Deep linking is necessary only if you have to associate the Telegram user with an user of your website/service (in your database). If you just want the user to let the bot know that he exists and wants to receive something from the bot, a standard telegram.me/TheBot link would be ok. @kb0000 it's a link, you can decide where and how you want to show it to the user
    – mcont
    Dec 8, 2015 at 18:52
  • 1
    So if I want to send messages to all users who /start-ed my bot, I have to store their chat_ids in my local storage? I cant just get from the bot all 'subscribed' ids?
    – Tosh
    Oct 19, 2016 at 20:48
  • You have to store all the chat_ids in your robot brain (using the redis brain for persistence). As far as I know, there is no way to retrieve "subscribed" users via their API. Oct 20, 2016 at 9:14
54

I created a bot to get User or GroupChat id, just send the /my_id to telegram bot @get_id_bot.

It does not only work for user chat ID, but also for group chat ID.

To get group chat ID, first you have to add the bot to the group, then send /my_id in the group.

Here's the link to the bot.

14
  • 7
    You can also share your telegram contact to @get_id_bot to reveal their chat_id Jun 30, 2016 at 9:57
  • Awesome, thanks, but it then keeps spamming me, how do I stop it ? Feb 27, 2017 at 11:40
  • 1
    @fmalaussena : sorry for that, I have it fixed, ... thanks Jun 16, 2017 at 8:49
  • 2
    Indeed. Let me add that your answer and the fix came really quickly. Thanks. Jun 16, 2017 at 15:13
  • 1
    Thanks, send /my_id to @get_id_bot bot is easiest way to get chat id.
    – iwind
    Nov 4, 2019 at 14:52
28

First, post a message in a chat where your bot is included (channel, group mentioning the bot, or one-to-one chat). Then, just run:

curl https://api.telegram.org/bot<TOKEN>/getUpdates | jq

Feel free to remove the | jq part if your dont have jq installed, it's only useful for pretty printing. You should get something like this:

enter image description here

You can see the chat ID in the returned json object, together with the chat name and associated message.

4
  • /getUpdates returns the chat.id but webhook do not returns it just returns NULL. Check my question here : stackoverflow.com/q/68815736/10302693 Aug 19, 2021 at 5:09
  • {"ok":false,"error_code":404,"description":"Not Found"}%
    – Dr.jacky
    Jan 6, 2022 at 14:22
  • @Dr.jacky I know it's been 6 months but maybe it'll help to your problem: stackoverflow.com/a/41460083/1387413
    – pash3r
    May 31, 2022 at 13:37
  • 1
    Thanks for this, it's so easy without using any thirt party bots. Helped me a lot!
    – Ksdmg
    May 27 at 10:05
21

There is a bot that echoes your chat id upon starting a conversation.

Just search for @chatid_echo_bot and tap /start. It will echo your chat id.

Chat id bot screenshot

Another option is @getidsbot which gives you much more information. This bot also gives information about a forwarded message (from user, to user, chad ids, etc) if you forward the message to the bot.

4
  • 1
    Seems no more to work. This telegram name especially doesn't seem to belong to a bot but to a normal user. May 20, 2019 at 18:13
  • 2
    @AxelBeckert, my mistake. I wrote the name wrong, it should be @chatid_echo_bot. Just checked, it's a bot and it works. Sep 17, 2019 at 11:08
  • 1
    Adding @chatid_echo_bot to a group instantly tells you the chat id! Thank you very much! Oct 24, 2020 at 20:56
  • 2
    chatid_echo_bot no response when I type /start, I think it's not working anymore, use this one, it's working the last time I check @get_id_bot Apr 23, 2022 at 11:05
12

You can just share the contact with your bot and, via /getUpdates, you get the "contact" object

8

Using the Perl API you can get it this way: first you send a message to the bot from Telegram, then issue a getUpdates and the chat id must be there:

#!/usr/bin/perl

use Data::Dumper;
use WWW::Telegram::BotAPI;

my $TOKEN = 'blablabla';
my $api = WWW::Telegram::BotAPI->new (
    token => $TOKEN
) or die "I can't connect";

my $out = $api->api_request ('getUpdates');
warn Dumper($out);
my $chat_id = $out->{result}->[0]->{message}->{chat}->{id};
print "chat_id=$chat_id\n";

The id should be in chat_id but it may depend of the result, so I also added a dump of the whole result.

You can install the Perl API from https://github.com/Robertof/perl-www-telegram-botapi. It depends on your system but I installed easily running this on my Linux server:

$ sudo cpan WWW::Telegram::BotAPI

Hope this helps

4

Straight out from the documentation:

Suppose the website example.com would like to send notifications to its users via a Telegram bot. Here's what they could do to enable notifications for a user with the ID 123.

  1. Create a bot with a suitable username, e.g. @ExampleComBot
  2. Set up a webhook for incoming messages
  3. Generate a random string of a sufficient length, e.g. $memcache_key = "vCH1vGWJxfSeofSAs0K5PA"
  4. Put the value 123 with the key $memcache_key into Memcache for 3600 seconds (one hour)
  5. Show our user the button https://telegram.me/ExampleComBot?start=vCH1vGWJxfSeofSAs0K5PA
  6. Configure the webhook processor to query Memcached with the parameter that is passed in incoming messages beginning with /start. If the key exists, record the chat_id passed to the webhook as telegram_chat_id for the user 123. Remove the key from Memcache.
  7. Now when we want to send a notification to the user 123, check if they have the field telegram_chat_id. If yes, use the sendMessage method in the Bot API to send them a message in Telegram.
4

chat_id is nothing but id of user (telegram user account id). You can start a chat with @get_my_chat_id_bot. It will send you back the chat_id (your user_id). There are following commonly used ids: channel id, group id, bot id, chat id(user id).

1
2

DO NOT USE third party BOTS, they will hack your account later on.

Just look up your bot in telegram and start a chat, then use this link https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates

It will return obj with chatId.

1

Whenever user communicate with bot it send information like below:

$response = {
        "update_id":640046715,
        "message":{
            "message_id":1665,
            "from":{"id":108177xxxx,"is_bot":false,"first_name":"Suresh","last_name":"Kamrushi","language_code":"en"},
            "chat":{"id":108xxxxxx,"first_name":"Suresh","last_name":"Kamrushi","type":"private"},
            "date":1604381276,
            "text":"1"            
            }        
        }

So you can access chat it like:
$update["message"]["chat"]["id"]

Assuming you are using PHP.

1

Extending @Roberto Santalla answer and if you prefer to use Telegram API together with javascript and axios library then you might want the following:

const method = 'get'

const headers: any = {
  'Access-Control-Allow-Origin': '*',
  'Content-Type': 'application/json',
  timestamp: +new Date(),
}

const options = { headers: { ...headers } }

const urlTelegramBase =
  'https://api.telegram.org/bot123456:ABCDEF'

const urlGetUpdates = `${urlTelegramBase}/getUpdates`
const username = 'user_name'

const {
  data: { result: messages },
} = await axios[method](urlGetUpdates, options)

const chat_id = messages.find(
  messageBlock => messageBlock.message.chat.username === username
).message.chat.id

console.info('chat_id': chat_id)
0

Read message : (chat_id is in response of this request, it contains all messages sent by anyone till now, on this bot)

https://api.telegram.org/bot{bot_token}/getUpdates

Write Message : (bot_chatID will define, whom to send message, and bot_token will define who will send message)

https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={bot_chatID}&parse_mode=Markdown&text={bot_message}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.