telegramtelegram-botphp-telegram-bot

Telegram bots API responds 403 Forbidden - when send direct message to bot


$apiToken = "5232750517:AAF7gbXCwZdfhSQeZZb53-2NhTm2xxNuT3U";

          $data = [
              'chat_id' => '5232750517',
              'text' => 'Hello World'
          ];
          $response = file_get_contents("https://api.telegram.org/bot$apiToken/sendMessage?" .
                                         http_build_query($data) );

The error message

Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

Then I am followed this solution - Telegram bots API responds 403 Forbidden

My bot username is @test247_bot and change the code to:

$apiToken = "5232750517:AAF7gbXCwZdfhSQeZZb53-2NhTm2xxNuT3U";

  $data = [
      'chat_id' => '5232750517',
      'text' => 'Hello World'
  ];
  $response = file_get_contents("https://api.telegram.org/@test247_bot$apiToken/sendMessage?" .
                                 http_build_query($data) );

But also did not work, the error message

Failed to open stream: HTTP request failed! HTTP/1.1 404

Please help


Solution

  • That's not the correct way to build your URL.

    Remove your bot's username. It is simply "bot" followed by api token instead.

    Try:

    file_get_contents("https://api.telegram.org/bot5232750517:AAF7gbXCwZdfhSQeZZb53-2NhTm2xxNuT3U/sendMessage?" .http_build_query($data) );