0

hello so i am trying to add broadcast command to my telegram bot which broadcast a specific message to all my bot subscribers which ids are saved in mysql database but the loop never seem to end and restarts after a random amount of sent messages

for example : the bot start messaging and then stop at 987 users and restart the loop over and over or a different number of users too

this is the code that i am using:

<?php
http_response_code(200);

$admin_id = ''; // my id
$bot_token = ''; // my bot token 

$message_object = json_decode(file_get_contents("php://input"),true);
$message_text = $message_object['message']['text'];


if(startsWith($message_text, '/broadcast') !== false){
    $text_to_send = trim(str_replace('/broadcast','',$message_text));
    $start_message = sendmessage($admin_id, 'Broadcasting Started', $bot_token, 'markdown');
    $start_message_id = $start_message['result']['message_id'];
    $query = mysqli_query($con, "SELECT * FROM users");
    if($query and mysqli_num_rows($query) >= 1){
        $all_users = mysqli_fetch_all($query,MYSQLI_ASSOC);
        $sent = 0;
        foreach($all_users as $user){
            $user_id = $user['userid'];
            $sent += 1;
            sendmessage($user_id,$text_to_send,$bot_token,'markdown');
            sleep(1);
            editmessage($admin_id,"Messages Sent : $sent",$bot_token,$start_message_id);
        }
        sendmessage($admin_id,'finished broadcasting '.$sent.' messages',$bot_token,'markdown');
    }
}

?>

and i never manage to get to the end of the loop to get the broadcast finished message and stuck on an infinite loop

same issue happen when i try to import amount of data that is more than 50 items so mysql database using the same method used in broadcast one

mega chad
  • 1
  • 1

1 Answers1

0

I think that's a problem of PHP maximum execution time: by default PHP has a max execution time of 30s. After 30 seconds the script is terminated and an error is reported to the client who made the initial request (in this case the Telegram API). Telegram sees the error thrown by your script and repeat the same request, so the script is executed again and again every 30 seconds. A possible solution may be the following:

Add the following code before $admin_id = '';

set_time_limit(100); // Set the max execution time

ignore_user_abort(true);
header('Connection: close');
flush();
fastcgi_finish_request();

This code will immediately close the connection with Telegram so it doesn't have to wait until the script terminates and it doesn't call the script again if an error occurs. With the set_time_limit(100) function you can increase the execution limit (for example to 100 seconds) so PHP doesn't kill everything before you have sent the broadcast message to everyone. If this operation takes more than 100 seconds just increse the limit or set it to 0 so it never ends, because according to the set_time_limit docs: If set to zero, no time limit is imposed.

GioIacca9
  • 406
  • 4
  • 8
  • i have set_time_limit set to zero already and the same issue presist and my script return response code 200 at all times – mega chad Jan 09 '22 at 15:37
  • Hi, does your script return 200 eaven when it runs for a while? Anyway, Telegram may have a "max connection time" after which it closes the connection and send the same request again. Have you tried closing the connection with Telegram immediately using the second block of code (the one that starts with `ignore_user_abort`...)? – GioIacca9 Jan 10 '22 at 16:12
  • i tried to close connection which resulted in closing the loop for some reason – mega chad Jan 14 '22 at 10:26