php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.9k stars 953 forks source link

Re-open: Input is empty! - hook.php #203

Closed devdenis closed 8 years ago

devdenis commented 8 years ago

Have the same problem with webhooks. Use SSL from Let's Encrypt, webhook is set, but have the error: exception 'Longman\TelegramBot\Exception\TelegramException' with message 'Input is empty!' in path/vendor/longman/telegram-bot/src/Telegram.php:414 Stack trace: #0 path/index.php(14): Longman\TelegramBot\Telegram->handle() #1 {main}

noplanman commented 8 years ago

That error is normal if you call hook.php directly. Did you follow the installation instructions in the readme properly? Make sure you haven't left out any steps 😊

If you did and it's still not working, post again here.

devdenis commented 8 years ago

in my hook file

require_once 'vendor/autoload.php';
require_once 'config.php';

try {
  $telegram = new Longman\TelegramBot\Telegram($bot_token, $bot_name);
  $update = $telegram->handle();
  file_put_contents('value.txt', $update);
}
catch (Longman\TelegramBot\Exception\TelegramException $e) {
   echo $e;
}

after send message in telegram client I have empty value.txt, but if unset webhook and use $telegram->handleGetUpdates(); I have some json in value.txt

jacklul commented 8 years ago

after send message in telegram client I have empty value.txt, but if unset webhook and use $telegram->handleGetUpdates(); I have some json in value.txt

That's because $telegram->handle(); returns result, not input.

Just to verify... put this:

file_put_contents('input.txt', file_get_contents("php://input"));

before require_once 'vendor/autoload.php';

and then send command to the bot, is the 'input.txt' file empty? It shouldn't, if it is it MIGHT be an issue with webhost configuration.

devdenis commented 8 years ago

File input.txt have json with last message.

jacklul commented 8 years ago

Well then, try

$input = file_get_contents("php://input");
if (!empty($input)) {
    $telegram->setCustomInput($input);
}

before $update = $telegram->handle();

devdenis commented 8 years ago
try {
  $telegram = new Longman\TelegramBot\Telegram($bot_token, $bot_name);
  $input = file_get_contents("php://input");
  if (!empty($input)) {
    $telegram->setCustomInput($input);
  }
  $update = $telegram->handle();
  file_put_contents('value.txt', $update);
}
catch (Longman\TelegramBot\Exception\TelegramException $e) {
   echo $e;
}

value.txt is empty

jacklul commented 8 years ago

Does it throw 'input empty' exception now?

$update = $telegram->handle();
file_put_contents('value.txt', $update);

as I said before: this will not output the update, it will output the result of handle(), which can be reply from Telegram API to, for example, sendMessage.

devdenis commented 8 years ago
$update = $telegram->handle();
file_put_contents('value.txt', $update);

in file value.txt - I have "1" but how I can get and process the message from user?

jacklul commented 8 years ago

You're doing it wrong, $telegram->handle(); is not supposed to return anything for you to work with.

I have a feeling you haven't read the readme, it's all there.

How to create own commands: https://github.com/akalongman/php-telegram-bot#custom-commands

MBoretto commented 8 years ago

Can close here?