Issue with continuous message sending in Telegram bot created with PHP

I’m currently working on a Telegram bot using PHP, and I’ve run into a problem. I keep user chat IDs in my database to send mass messages. When I enter the command /admin sendall:hello, it should ideally send the message just once to all users. However, at times, it ends up sending the same message repeatedly without stopping, and I have to clear my database to fix it.

Here’s an excerpt of my command handling code:

case '/admin':
    if ($chat_id === 'admin_chat_id') {
        $commandParts = str_replace('/admin', '', $message);
        $commandParts = trim($commandParts);
        $splitCommands = explode(':', $commandParts);
        $AdminCommand = new AdminCommand();
        $AdminCommand->executeCommand($splitCommands[0], $splitCommands[1]);
    } else {
        sendMessage($chat_id, 'You have been blocked.');
    }
    break;

Here’s how the AdminCommand class looks:

class AdminCommand extends Database {

    public function executeCommand($command, $argument = null) {
        switch ($command) {
            case 'sendall':
                $this->sendToAll($argument);
                break;
            default:
                break;
        }
    }

    public function sendToAll($message) {
        $stmt = $this->con->prepare('SELECT * FROM `users`');
        $stmt->execute();
        $userList = $stmt->fetchAll();
        foreach ($userList as $user) {
            sendMessage($user['chatid'], $message);
        }
        exit();
    }
}

This is the sendMessage function I use:

function sendMessage($chatId, $message) {
    $url = BOT_URL . "/sendMessage?chat_id=" . $chatId . "&text=" . urlencode($message);
    file_get_contents($url);
}

Can anyone suggest what might be causing this issue where messages keep looping endlessly?

sounds like telegram’s hitting your webhook multiple times for the same update. try adding update_id tracking in ur db to avoid dupes. also, that exit() after sending looks kinda strange - maybe use return instead?

Had this exact issue with my first PHP bot. Telegram keeps retrying because it’s not getting a proper HTTP 200 response. Your exit() is probably killing the script before it can respond to Telegram, so Telegram thinks the update failed and keeps retrying. Add http_response_code(200); before your exit, or just ditch the exit and return from the method instead. Also wrap your database stuff in a transaction - prevents half-finished executions if something breaks during mass sending. Fixed my infinite loop problem completely.

This happens because of webhook timeouts plus Telegram’s retry system. You’re sending to hundreds or thousands of users, which takes forever and blows past Telegram’s timeout limit. Telegram doesn’t get a response fast enough, thinks your webhook died, and resends the same update - boom, your mass message fires again.

I’ve been through this hell before. Fix it with a queue system. Don’t process everything right away - dump the broadcast job into a separate table and let a background cron handle it. Your webhook responds to Telegram instantly while the actual sending runs in the background.

Also throw in a simple flag or timestamp check so the same command can’t run twice within a few seconds.