php-telegram-bot / core

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

Webhook problem #892

Closed sup3rgiu closed 6 years ago

sup3rgiu commented 6 years ago

Required Information

I'm using this to perform some actions on my Instagram account https://github.com/mgp25/Instagram-API/.

In the code of a telegram Command, I put $ig = new \InstagramAPI\Instagram(); as requested by the API.

... code1 ...
Request::sendMessage(['chat_id' => $id, 'text' => 'I'm Here']);
$ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);
Request::sendMessage(['chat_id' => $id, 'text' => 'I'm Here 2']);
... code2 ...

I receive only "I'm Here" and not "I'm Here 2", and also ...code2... is not executed.

N.B: no errors are generated in the error log file

noplanman commented 6 years ago

Well, if it works without the ...new Instagram(...); line, then the issue is probably with that library, not the bot itself.

Strange that there is no error being logged.

Using getUpdates method, when you send /command to your bot, everything works fine?

What version of the bot are you using?

sup3rgiu commented 6 years ago

I'm using version 0.53.0.

I thought too that the problem could be with the Instagram library, but when I use getUpdates and send /command everything works fine.

Also, if I set webhook and call the command each X hours with cron.php and a cron job, it works too.

So the problem exists only when I use webhook and try to call the /command manually

noplanman commented 6 years ago

You have the error log activated? Also, in your webhook, make sure that the errors are logged properly in the catch block:

} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // Log telegram errors
    Longman\TelegramBot\TelegramLog::error($e);
}
jacklul commented 6 years ago

@sup3rgiu why not trying to debug it by wrapping it in try...catch?

try {
   $ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);
} catch (\Exception $e) {
   file_put_contents(__DIR__ . '/error.log', $e, FILE_APPEND);
}
sup3rgiu commented 6 years ago

@sup3rgiu why not trying to debug it by wrapping it in try...catch?

try {
   $ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);
} catch (\Exception $e) {
   file_put_contents(__DIR__ . '/error.log', $e, FILE_APPEND);
}

The file is not created (obviously not permission problem). It's like if the code immediatly stops at the $ig = new \InstagramAPI\Instagram($debug, $truncatedDebug); line

@noplanman contacted me on Telegram and is investigating

noplanman commented 6 years ago

This is a security feature from the Instagram API.

Look here: https://github.com/mgp25/Instagram-API/blob/9d20c73eb8d55249bd596c5007a496a9944defe3/src/Instagram.php#L243

It only allows safe calls that come via CLI, which are both the case when you use getUpdates and cron. Webhook however, comes through the webserver, so it's blocked.

Simply call this somewhere at the beginning of your webhook:

\InstagramAPI\Instagram::$allowDangerousWebUsageAtMyOwnRisk = true;