How to obtain Telegram chat_id for a specific user?

前端 未结 7 1486
自闭症患者
自闭症患者 2020-12-01 02:36

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

Integer | Unique identifier for the message recipient — User or G

7条回答
  •  [愿得一人]
    2020-12-01 03:04

    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

提交回复
热议问题