Core: /whois <user> - Bot crash and never respond.

Created on 7 Feb 2017  路  20Comments  路  Source: php-telegram-bot/core

Required Information

  • PHP version: 7.0.13
  • PHP Telegram Bot version: 0.39.0
  • Using MySQL database: yes
  • Update Method: Webhook
  • Self-signed certificate: no
  • RAW update (if available):

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 /whois <string>, and randomly all commands stop working. Bot don't reply anymore to any command after this.

Extra details

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 AB1234c
[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)

All 20 comments

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

(Related Issue)

@jacklul Any ideas, I'm a bit lost here at the moment...

I honestly have no idea what is causing this...

@nesttle Any extra info you could give us?
Can you call the /debug command on your bot and post the output here please?
Maybe that will help with something.

@noplanman tried /debug but not responding to any commands

I have the same behaviour.
When bot responds with 500 error, Telegram tries to resend message, afaik. Which causing 500 error and so on. When user comments out custom commands, those messages got at least Request::emptyResponce, not 500.

+350 requests inserted into db, bot still not working :(
http://i.imgur.com/erE7enW.png Screenshot

What if there is some kind of issue with PHP7?

Any chance you could try running the bot :

  • using PHP 5.6
  • with getUpdates() script

I remember getting 500 error in the past,

You could try using cgi to handle webhook which would redirect post data as a parameter to hook,php

#!/bin/bash

echo "Status: 200 OK\r\n"
POST_DATA=$(cat)

echo "Content-Type: application/json"
echo ""

/usr/local/bin/php hook.php $POST_DATA > /dev/null 2>&1 &

exit;

(adjust php part to the correct one)

You might have to add cgi handler to .htaccess:

<Files *.cgi>
    AddHandler cgi-script .cgi
    Options +ExecCGI
</Files>

Next, you will need to add this to the top of your webhook script:

if (defined('STDIN')) {
    unset($argv[0]);
    $POST = implode(' ', $argv);
}

And later, after initalizing the bot:

if (!empty($POST)) {
    $telegram->setCustomInput($POST);
}

It was the same, on 5.6 for me.
You could reproduce it with making any code in execute() that will generate
fatal error. You'll see "pending_update_count" going up on
https://api.telegram.org/botXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXX/getWebhookInfo
If it's over 100 you'll unlike get an answer from the bot.

If you can, try my cgi workaround in https://github.com/akalongman/php-telegram-bot/issues/406#issuecomment-278124634

Thanks for your replies guys.

Atm, I made it working again with the next "workaround":

1) Make a new hook_test.php without any MySQL/custom commands. Hook.php:

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

$API_KEY = '3XXXXXXXXX';
$BOT_NAME = 'MYBOT';


try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);
    // Handle telegram webhook request
    $telegram->handle();
    file_put_contents('_mylog.log','OK'.PHP_EOL, FILE_APPEND | LOCK_EX);
} catch (Longman\TelegramBot\Exception\TelegramException $e) {

    file_put_contents('_mylog.log', $e, FILE_APPEND | LOCK_EX);
}

2) Make a new set_test.php with the following code:

<?php
require __DIR__ . '/vendor/autoload.php';
$API_KEY = '3XXXXXXXXXXXXXXXX';
$BOT_NAME = 'MYBOT';
$hook_url = 'https://www.yourdomain.com/path/hook_test.php';
try {
    // Create Telegram API object
    $telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME);

    // Set webhook
    $result = $telegram->setWebhook($hook_url);
    if ($result->isOk()) {
        echo $result->getDescription();
    }
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
    echo $e;
}

3) Make a DB backup. And now TRUNCATE the following DB tables: edited_message, message, telegram_update

SET FOREIGN_KEY_CHECKS = 0; 
TRUNCATE message; 
TRUNCATE edited_message; 
TRUNCATE telegram_update; 
SET FOREIGN_KEY_CHECKS = 1;

4) Enter url https://www.yourdomain.com/path/set_test.php
5) Talk with your bot, now commands are working again for me, don't know why but lol.
6) Visit your old https://www.yourdomain.com/path/set.php to set your old hook.php again.
7) Working

--

Sorry @jacklul I didn't try your code yet, will do on next time and maybe I won't need to use my "workaround" anymore!

Ok, this seems related to an issue I had as well at some point, where updates weren't being saved to the DB any more. After truncating it worked for me again too, so some SQL query is messing stuff up...

I think this issue is related when you edited a sent message. This time bot stucks when:
1) /whois @AB1234c
2) Edited message to /whois AB1234c
3) Bot broken

@nesttle Can you reproduce that on a clean install?

It works for me without any issues 馃槙

Have you tried enabling the error logging? If so, please set up a clean install, enable the error log and try to break it again.
Really not sure what I'm looking for here...

Will try today on a clean install and update this

edit: today same problem, will need to truncate my tables again...

SET FOREIGN_KEY_CHECKS = 0; 
TRUNCATE table1; 
SET FOREIGN_KEY_CHECKS = 1;

@jacklu any plans to implement any solution like your CGI fix on next release? (to avoid bulk requests when bot comes online after a shutdown). Anyway I'll try your solution, thanks

Depending on webserver configuration errors and exceptions can modify headers with 500 error, to get rid of this you might have to dig around the webserver software you're using, you might also try overwriting the header before throwing exceptions (like header('HTTP/1.1 200 OK'); at the top of the hook script - no idea if that will work! also look here depending on your web server: https://stackoverflow.com/questions/3258634/php-how-to-send-http-response-code).

My cgi solution can also be replaced by another PHP file that will run the actual hook script in command line (for example system() call etc) and return success and empty page.

Ok! Will try to disable 500 Error response with this: http://stackoverflow.com/a/2687747 (php.ini)
error_reporting=E_ALL display_errors=On and display_startup_errors=On

Found that somewhere on stackoverflow:

  /***
   * Error handling
   ***/
  /**
   * Error handler, passes flow over the exception logger with new ErrorException.
   *
   * @param $num
   * @param $str
   * @param $file
   * @param $line
   * @param null $context
   */
  function log_error( $num, $str, $file, $line, $context = null )
  {
    log_exception( new ErrorException( $str, 0, $num, $file, $line ) );
  }

  /**
   * Uncaught exception handler.
   * @param \ErrorException $e
   */
  function log_exception( $e )
  {

    $message = "Type: " . get_class( $e ) . "; Message: {$e->getMessage()}; File: {$e->getFile()}; Line: {$e->getLine()}; Time: ".date("H:i:s / d.m.Y")."; Trace: {$e->getTraceAsString()}";
    file_put_contents( __DIR__ .'/error.log', $message . PHP_EOL, FILE_APPEND );

    exit();
  }

  /**
   * Checks for a fatal error, work around for set_error_handler not working on fatal errors.
   */
  function check_for_fatal()
  {
    $error = error_get_last();
    if ( $error["type"] == E_ERROR )
      log_error( $error["type"], $error["message"], $error["file"], $error["line"] );
  }

  register_shutdown_function( "check_for_fatal" );
  set_error_handler( "log_error" );
  set_exception_handler( "log_exception" );
  ini_set( "display_errors", "off" );
  error_reporting( E_ALL );

Maybe it could be added to hook.php to return code 200 and emptyResponce (or not empty) via log_exception()?

@nesttle Getting a 500 error code has a reason, so it's all about finding the issue, not just hacking a fix.

Is your bot behind a reverse proxy?
Could you give us more info about your setup to help find the culprit?

Was this page helpful?
0 / 5 - 0 ratings