How to discover the chat ID via the Telegram API?

Asked

Viewed 9,432 times

4

I am configuring the Telegram BOT API via Guzzle. I am trying to understand what the parameter would be chat_id described in the method documentation sendMessage.

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 figure out what the chat ID is? I would like the BOT to send messages to me.

1 answer

5


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

Using Guzzle himself 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 to simulate 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 as a parameter chat_id in the call for sendMessage.

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

Observing: 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.