Skip to content
Advertisement

$_POST variable is empty when code is executed using Telegram webhook

I am creating a Telegram Bot using PHP. I have set the webhook to “https://example.com/bot.php”. As you can see below, I used echo to run a script that creates HTML form. After being created, the form then is submitted automatically and I get variables using POST method.

echo "<script>
        document.write('<form method="post" action="" id="myForm">');
        document.write('<textarea name="price">' + 12345 + '</textarea><br>');
        document.write('<input type="submit" value="submit"></form>');
      </script>";

if (!isset($_POST['price']) ) {
  echo "<script>document.getElementById("myForm").submit();</script>";
}

$price = $_POST['price'];

If I run "bot.php” manually (using the URL), the code works just fine. However, when I send the commands in a Telegram bot, $_POST['price'] returns empty. To check that I sent isset($_POST['price']) as a message to the bot, and I received 0.

Advertisement

Answer

As you know the JS is client-side and that’s why it works on your web browser and it doesn’t work on Bot request because the bots can’t run JavaScript.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement