How to get Telegram chat_id for a specific user?

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

Integer | The unique identifier of the recipient of the message is the identifier of the user or group in the chat

+49
telegram-bot
Jun 26 '15 at 17:16
source share
5 answers

Updates to messages that you receive through getUpdates , or your web check will contain the chat identifier for a specific message. It will be contained under the key message.chat.id .

This seems like the only way to get the chat id. Therefore, if you want to write something where the bot initiates a conversation, you probably have to store the chat identifier in relation to the user in some kind of key-> value storage, for example MemCache or Redis.

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

+31
Jun 26 '15 at 20:50
source share
β€” -

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

It works not only for user chat, but also for group chat id.

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

Here is a link to the bot.

+36
May 23 '16 at 17:16
source share

You can simply share the contact with your bot, and through / getUpdates you will get a contact object

+7
Dec 23 '15 at 16:48
source share

There is a bot that displays your chat id after starting a conversation.

Just search for @chat_id_echo_bot and hit /start . This will be an echo of your chat.

Chat id bot screenshot

+7
Feb 22 '19 at 1:16
source share

Using the Perl API, you can get it as follows: first you send a message to the bot from Telegram, then it displays getUpdates and there should be a chat identifier:

 #!/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 identifier should be in chat_id, but it may depend on the result, so I also added a dump of the whole result.

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

 $ sudo cpan WWW::Telegram::BotAPI 

Hope this helps

+5
Jul 27 '15 at 9:35
source share



All Articles