php-telegram-bot / core

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

Uncaught Error: Call to a member function isOk() #403

Closed nesttle closed 7 years ago

nesttle commented 7 years ago

Required Information

Expected behaviour

Bot should reply to commands.

Actual behaviour

Bot stucks and is not responding to any messages/commands after an unknown issue. Sometimes I can solve this cleaning DB or removing/uploading again files.

Steps to reproduce

Execute some /command, and randomly all commands stop working. Bot don't reply anymore to any command after this. OR call directly hook.php, and next error appears

Extra details (error.log Apache)

PHP Fatal error:  Uncaught Error: Call to a member function isOk() on boolean in /var/www/html/_tg/vendor/longman/telegram-bot/src/Telegram.php:363
Stack trace:\n#0 /var/www/html/_tg/hook.php(29): Longman\\TelegramBot\\Telegram->handle()
#1 {main}\n  thrown in /var/www/html/_tg/vendor/longman/telegram-bot/src/Telegram.php on line 363
noplanman commented 7 years ago

Is this the case for all commands or just custom ones?

It looks like the command you are calling returns a boolean instead of a ServerResponse object.

nesttle commented 7 years ago

Stack trace:

0 /var/www/html/tg/hook.php(29): Longman\TelegramBot\Telegram->handle()

1 {main}Longman\TelegramBot\Exception\TelegramException: Input is empty! in /var/www/html/tg/vendor/longman/telegram-bot/src/Telegram.php:354

Stack trace:

noplanman commented 7 years ago

That is a normal response when opening hook.php with your browser.

When you install a fresh version of the latest version and an empty database, do all commands work without any issues?

If issues start when you add custom commands, could you please post one of the commands that creates the issue? That way we can see what is causing it, thanks!

nesttle commented 7 years ago

Thanks for your reply @noplanman

Yes, all commands works without any issues on a fresh install. Also, when I use my customs commands works too, but after 1-2 days something broke, and all commands stops working.

Some times I can fix this commenting my custom commands include on hook.php: //$telegram->addCommandsPath(__DIR__ . '/vendor/longman/telegram-bot/src/Commands/CustomComm'); and calling hook.php again.

This time I can't fix this, neither commenting line above :(

noplanman commented 7 years ago

So you always get the PHP Fatal error: Uncaught Error: Call to a member function isOk() on boolean error in your Apache log then?

Have you enabled the error logging in your bot?

nesttle commented 7 years ago

@noplanman hope I can enable error loggin, but having issues* with write permissions (tried with 755 also on all bot files/folders and same). Will try to enable logging to give you best acurate details about error!

nesttle commented 7 years ago

@noplanman Permissions issue was solved with chown & chmod: http://stackoverflow.com/questions/5165183/apache-permissions-php-file-create-mkdir-fail

Now bot can write _error.log + _access.log + _update.log

Dumps: _error.log = empty

_access.log:

{"update_id":990XXXXX, "edited_message":{"message_id":5XXX,"from":{"id":32XXXXXX"first_name":"nesttle","username":"nesttle"},"chat":{"id":32XXXXXX,"first_name":"nesttle","username":"nesttle","type":"private"},"date":148646XXX,"edit_date":1486468XXX,"text":"/whois testing","entities":[{"type":"bot_command","offset":0,"length":6}]}}
{"update_id":990XXXXX, "edited_message":{"message_id":5XXX,"from":{"id":32XXXXXX"first_name":"nesttle","username":"nesttle"},"chat":{"id":32XXXXXX,"first_name":"nesttle","username":"nesttle","type":"private"},"date":148646XXX,"edit_date":1486468XXX,"text":"/whois testing","entities":[{"type":"bot_command","offset":0,"length":6}]}}
{"update_id":990XXXXX, "edited_message":{"message_id":5XXX,"from":{"id":32XXXXXX"first_name":"nesttle","username":"nesttle"},"chat":{"id":32XXXXXX,"first_name":"nesttle","username":"nesttle","type":"private"},"date":148646XXX,"edit_date":1486468XXX,"text":"/whois testing","entities":[{"type":"bot_command","offset":0,"length":6}]}}
{"update_id":990XXXXX, "edited_message":{"message_id":5XXX,"from":{"id":32XXXXXX"first_name":"nesttle","username":"nesttle"},"chat":{"id":32XXXXXX,"first_name":"nesttle","username":"nesttle","type":"private"},"date":148646XXX,"edit_date":1486468XXX,"text":"/whois testing","entities":[{"type":"bot_command","offset":0,"length":6}]}}
{"update_id":990XXXXX, "edited_message":{"message_id":5XXX,"from":{"id":32XXXXXX"first_name":"nesttle","username":"nesttle"},"chat":{"id":32XXXXXX,"first_name":"nesttle","username":"nesttle","type":"private"},"date":148646XXX,"edit_date":1486468XXX,"text":"/whois testing","entities":[{"type":"bot_command","offset":0,"length":6}]}}

_update.log

{"update_id":990XXXXX, "edited_message":{"message_id":5XXX,"from":{"id":32XXXXXX,"first_name":"nesttle","username":"nesttle"},"chat":{"id":32XXXXXX,"first_name":"nesttle","username":"nesttle","type":"private"},"date":14864XXXXX,"edit_date":14864XXXXX,"text":"/whois testing","entities":[{"type":"bot_command","offset":0,"length":6}]}}

Command which made my bot crash:

[12:57] nesttle: /whois testing
[12:57] MYBOT: Chat not found!

After that, I executed correctly 2 commands, but looks like bot stucks on command above (I can see again and again same input on dumps: _update.log and _access.log)

nesttle commented 7 years ago

Also, on db table.edited_message I can see same request again and again (200+ same entries): http://i.imgur.com/s1fsHZG.png :: Database

nesttle commented 7 years ago

Target username on /whois has numbers. Username is like A1234b.

Also I tried to rename WhoisCommand.php to WhoisCommand_.php, but bot is still into same loop.

Custom commands are disabled now by the way, but bot still not responding to any message.

hook.php

<?php
// Load composer
require __DIR__ . '/vendor/autoload.php';

$API_KEY = '30XXXX:aaa';
$BOT_NAME = 'MYBOT';
$mysql_credentials = [
   'host'     => 'localhost',
   'user'     => 'aaa',
   'password' => 'bbb',
   'database' => 'ccc',
];

try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

    \Longman\TelegramBot\TelegramLog::initUpdateLog(__DIR__ . '/' . $BOT_NAME . '_update.log');
    \Longman\TelegramBot\TelegramLog::initDebugLog(__DIR__ . '/' . $BOT_NAME . '_debug.log');
    \Longman\TelegramBot\TelegramLog::initErrorLog(__DIR__ . '/' . $BOT_NAME . '_error.log');

    $telegram->enableMySQL($mysql_credentials);
    $telegram->enableAdmin(xxx);
    //$telegram->addCommandsPath(__DIR__ . '/vendor/longman/telegram-bot/src/Commands/CustomCommands');

    // Handle telegram webhook request
    $telegram->handle();
    file_put_contents('_mylog.log','OK'.PHP_EOL, FILE_APPEND | LOCK_EX);
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    // Silence is golden!
    // log telegram errors
    // echo $e;
    file_put_contents('_mylog.log', $e, FILE_APPEND | LOCK_EX);
}
nesttle commented 7 years ago

@noplanman you can close this, I made a new issue better explained with this error, ty!