How can I find the chats ID for the Telegram API?

3

I'm setting up the Telegram BOT API via 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.

    
asked by anonymous 14.09.2018 / 18:35

1 answer

3

To find out the chat ID 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 simliar 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 in the sendMessage call.

In my case, I left it 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 above.

    
14.09.2018 / 18:42