php – How to find the ID of chats by Telegram API?

Question:

I'm configuring Telegram's BOT API through Guzzle. I'm trying to understand what the chat_id parameter would be described in the sendMessage method documentation.

I have the following code:

   $cli = new \GuzzleHttp\Client([
        'base_uri' => sprintf('https://api.telegram.org/bot%s/', static::TOKEN),
   ]);

   $cli->post('sendMessage', [
      'query' =>  [
          'chat_id' => $ID_DO_CHAT,
          'text' => $this->text, 
          'parse_mode' => 'markdown'
       ]
   ]);

What do I need to be able to find out what the chat ID is? I would like the BOT to send messages to me.

Answer:

To find out the ID of the chat in question, you need to access the getUpdates method of the telegram API.

Using Guzzle itself, it would look like this:

$cli = new \GuzzleHttp\Client([
    'base_uri' => sprintf('https://api.telegram.org/bot%s/', static::TOKEN),
]);

$response = $cli->get('getUpdates');

print_r(json_decode((string) $response->getBody(), true));

The result will be similar to this:

Array
(
    [ok] => 1
    [result] => Array
        (
            [0] => Array
                (
                    [update_id] => 26020178
                    [message] => Array
                        (
                            [message_id] => 113
                            [from] => Array
                                (
                                    [id] => 9999999999
                                    [is_bot] => 
                                    [first_name] => Nick
                                    [last_name] => Qualquer
                                    [username] => nickqualquer
                                    [language_code] => pt-br
                                )

                            [chat] => Array
                                (
                                    [id] => 9999999999
                                    [first_name] => Nick
                                    [last_name] => Qualquer
                                    [username] => nickqualquer
                                    [type] => private
                                )

                            [date] => 1536859308
                            [text] => UM TEXTO DE EXEMPLO
                            [entities] => Array
                                (
                                    [0] => Array
                                        (
                                            [offset] => 0
                                            [length] => 43
                                            [type] => url
                                        )

                                )

                        )

                )

        )

)

The place with the value 9999999999 is the chat ID and you should use chat_id as a parameter in the chat_id sendMessage .

In my case, I left the same saved in a configuration file, since the value is constant.

Note : You may need to send a message to your user's BOT so you can see the chat ID appearing in the list shown above.

Scroll to Top