0

lately Im facing a problem with telegram bot api which i haven't had before... Im using Webhook method of connection to catch bot requests and respoding with PHP script and sometimes the triggered script takes more than a minute to finish processing. about a month ago everything was working fine and telegram bot wait long enough for script to completely execute, but now my connection suspend and im getting this Webhook error through telegram api "Read timeout expired" after 60 Seconds of execution and then it attempt the same request again and these goes on and on until my server overloads with too many open entries... i already tried connection-handling although it seemed useless since my connection is not browser-end. i realize it should have something to do with Webhook's setting itself but i cant figure it out... any ideas?

Here is something that could give you the figure:

my code:

<?php

...running hundreds of thousands of multi-curl requests that take 10 min for example
...or/ sleep(61);
...or/ basically anything that takes more than 60 seconds to run

?>

Telegram's response to my Webhook's state after 60 seconds of running the above script:

{"ok":true,"result":{"url":"https://????.com/??.php","has_custom_certificate":false,"pending_update_count":1,"last_error_date":1499351442,"last_error_message":"Read timeout expired","max_connections":40}}

Samuel Dagon
  • 28
  • 1
  • 4
  • Welcome to SO! Please review the following post and update the question appropriately: https://stackoverflow.com/help/how-to-ask – garfbradaz Jul 06 '17 at 13:37
  • 1
    hey garfbradaz, tnx! i did read the manual beforehand and tried to write my post accordingly but im newbie anyhow... could you be more specific? – Samuel Dagon Jul 06 '17 at 13:59
  • Hey @Samuel - Please post some example code you have tried and any errors experienced. It helps show you have tried to work it out and where you are doing wrong. Good luck and enjoy the SO community. – garfbradaz Jul 06 '17 at 14:17
  • thanks for the tip @garfbradaz :) – Samuel Dagon Jul 06 '17 at 14:37
  • alternatively for now im scheduling long responding requests for cronjob and checking for it each minute. but even i can see that it can't be the solution! – Samuel Dagon Jul 08 '17 at 18:07

2 Answers2

0

this is the code I put at the top of my scripts. it responds to telegram so they stop waiting, and the scripts continues processing.

<?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();
    }
Yoily L
  • 412
  • 3
  • 8
0

Put this code after the update variable :

<?php
    $update = json_decode(file_get_contents('php://input'));
       if(isset($update->message) || isset($update->edited_message)) {
                if(time()-((@$update->message->date)?:(@$update->edited_message->date)) > 59) {
           /* print json update*/   exit('Update Time Out !');
                }
            }

NOTE :Of course, you can also get the update to receive Json and find out which time out request has been made.

NOTE : If the method of receiving your source update is different from mine, change it according to your source.

NOTE : If you know your script takes more than a minute, this will not work for you.