Mark
Mark

Reputation: 18184

Why does Telegram give me an empty POST request on the webhook?

I followed these steps:

  1. Registered a bot with BotFather.
  2. Send a post request for the webhook (https://api.telegram.org/bot[BOTID]/setWebhook) with the URL being https://example.com/myscript.php
  3. Double check with getWebhookInfo and it showed it is correctly registered.
  4. When I send a message to the bot, the script is being called but with an empty POST payload. In the documentation they say they would send an HTTPS POST request to the specified url, containing a JSON-serialized Update.

Does anyone else has this issue and perhaps know a way to resolve this?

My php script to log:

    $file = dirname(__FILE__) . '/telegram-log.txt';
    $entry = (object)array();
    $entry->date = date("r");
    $entry->GET = $_GET;
    $entry->POST = $_POST;
    $entry->REQUEST = $_REQUEST;
    $entry->HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
    $entry->REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
    file_put_contents($file, json_encode($entry) . "\n", FILE_APPEND | LOCK_EX);

Response:

{"date":"Thu, 17 Jun 2021 13:42:49 +0200","GET":[],"POST":[],"REQUEST":[],"HTTP_USER_AGENT":null,"REMOTE_ADDR":"91.108.6.133"}

Upvotes: 1

Views: 1299

Answers (1)

newsha
newsha

Reputation: 1438

Use $content = file_get_contents("php://input") to receive updates.

Telegram's response is of content-type application/json.

$_POST will only work when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.

Upvotes: 3

Related Questions