How can a PHP Telegram Bot schedule daily broadcasts without external triggers?

I’m looking for a method to have a PHP Telegram bot automatically send out a broadcast message at a fixed time each day, such as at 7:00 AM, without using cron jobs or relying on external execution. I also cannot use functions like pauseTimer() (instead of sleep()) because I need the bot to remain responsive. For instance, I attempted an approach like the code snippet below:

function waitCycle($scheduledHour) {
    while (true) {
        $currentHour = (int) date('G');
        if ($currentHour === $scheduledHour) {
            executeBroadcast();
        }
        usleep(500000); // wait for half a second before checking again
    }
}

function executeBroadcast() {
    // Insert logic to broadcast your message
    echo 'Broadcast initiated!';
}

// Start the cycle at 7 AM
waitCycle(7);

Is there a better or more efficient way to achieve this without halting the bot’s operation?

hey, i’ve embedded a non blocking timer in my main loop. i calc the secs until next broadcast and check time diff continuously. its a bit hacky but works fine without external jobs and keeps the bot responsive. give it a shot!

Through trial and error, I eventually implemented Swoole’s coroutine and timer functions to handle scheduled tasks within my PHP Telegram bot. I replaced the continuous while loop with a non-blocking timer, which checks the time in a background coroutine without interfering with message handling. Using Swoole’s timer_tick function, I scheduled a check every minute and then triggered the broadcast when the desired time was reached. It required some restructuring of the code and the installation of the Swoole extension, but it resulted in a responsive and maintainable solution.

I implemented a solution using PHP’s pcntl functionality. I rely on pcntl_alarm to trigger a signal at regular intervals, and within the signal handler I check whether the current time corresponds to the broadcast schedule. This method requires inserting pcntl_signal_dispatch calls in the main loop to process signals promptly, ensuring the bot remains responsive to incoming messages. It worked reliably in my testing and production environment, provided the pcntl extension is enabled. This approach avoids continuous polling and integrates well within a long-running CLI process.

I have encountered a similar challenge with my Telegram bot project and found an effective solution using an asynchronous event loop approach. Rather than relying on blocking loops or external cron jobs, I integrated ReactPHP’s event loop into the bot. This allowed me to schedule callbacks that execute the broadcast routine when the system clock reaches the desired time. It required refactoring parts of the code to adopt a non-blocking model, but the result was a fully responsive bot that handles both messages and scheduled tasks efficiently. It was a worthwhile adjustment that improved overall system performance.

My solution came by exploring PHP’s asynchronous libraries like Amp, which helped me avoid blocking loops. I set up a recurring timer in the event loop that checks if the current time matches the scheduled broadcast time, and then triggers the sending function. While refactoring to incorporate an event-driven paradigm was initially challenging, the payoff was a bot that remained responsive and efficient. This approach avoids persistent looping and guarantees that broadcast tasks occur at set intervals without interfering with the regular message processing.