Search code examples
telegram-botphp-telegram-bot

Telegram bot stops working after couple of months/weeks


My Telegram bot has php backend. I've set the hook based on the official guide. Many times the bot stops responding and I realize Telegram server doesn't trigger assigned web-hook. When I delete that bot and make a new bot with the same ID, problem remains unless I create a bot with different ID. Has anyone experienced such problem?


Solution

  • So I got it now. First of all, it's not custom certificate. It's because it had took more than 60 seconds for your server to reply to Telegram servers. you have previous messages stacked on Telegram servers. So you have to let them go by deleteWebHook and then setWebook again. After that, copy this code to the top of your code:

    <?php
        set_time_limit(0);
        ignore_user_abort(true);
        $out =  json_encode([
          'method'=>'sendMessage',
          'chat_id'=>$my_chat_id,
          'text'=> "Starting process..."
          ]);   
        echo $out;
        header('Connection: close');
        header('Content-Length: '.strlen($out));
        header("Content-type:application/json");
        flush();
        if (function_exists('fastcgi_finish_request')) {
            fastcgi_finish_request();
        }
    

    It replies to Telegram servers so they stop waiting. Taken from here.