Auto-banning specific user IDs when adding a Telegram bot to a group

Issue at Hand

I’m developing a Telegram bot that should automatically ban certain users when it’s added to any group. The user IDs I want to ban are saved in a text file.

Code I’m Using

$bot = new TelegramBot($token);
$group_id = $bot->getChatID();
$command = $bot->getMessage();

if ($command == "InitiateBanning"){
    $file = fopen("banned_users.txt","r");
    while(! feof($file))  { 
        $line = fgets($file);
        $user_id = (int)$line;
        $args = array('chat_id' => $group_id, 'user_id' => "$user_id");
        $bot->banChatMember($args);
    }
    fclose($file);
    $response = array('chat_id' => $group_id, 'text' => "Banning complete!");
    $bot->sendMessage($response);
    exit();
}

Current Challenges

Challenge 1: The bot is unable to ban users that it hasn’t “seen” before. It can only process bans if the users have interacted with the bot or I have manually banned them previously in another group.

Challenge 2: After the banning process, the bot keeps repeating the “Banning complete!” message indefinitely.

What I’ve Attempted

I’ve added HTTP headers and tried to adjust the update_id, but the issues continue:

$bot = new TelegramBot($token);
header("HTTP/1.1 200 OK");
http_response_code(200);

$group_id = $bot->getChatID();
$data = $bot->getUpdates();
$message = $data['message']['text'];
$current_update = $data['update_id'];
$data['update_id'] = ++$current_update;

What is the recommended way to manage the update_id increment properly and to set the HTTP response to resolve these problems?

The repeated messages happen because of webhook processing issues, not update_id problems. When Telegram sends an update to your webhook, it wants a clean HTTP 200 response quickly. If your script takes too long or doesn’t respond right, Telegram thinks it failed and sends the update again. The banning limitation is normal - that’s how Telegram works. You can only ban users who are currently in the group or were recently members. You can’t ban random user IDs that never joined your group. It’s a security feature to stop abuse. Here’s a better approach: don’t try to ban users ahead of time. Instead, set up a monitoring system that checks new members against your banned list when they join, then kicks them immediately. This works with Telegram’s rules and still protects your groups.

your update_id approach is wrong - don’t manually increment it. Telegram handles that automatically. for the infinite loop, add exit() right after sendMessage or set up your webhook properly. also check if your webhook url is actually responding - Telegram keeps retrying failed requests.

Your repeating message issue is probably webhook-related. Telegram sends updates to your bot, but if you don’t acknowledge them properly, the bot keeps processing the same update over and over. About banning users the bot hasn’t seen - that’s just how Telegram’s API works, not a bug in your code. Bots can only ban current members or people who recently left the group. You can’t preemptively ban random user IDs who’ve never joined. To fix the webhook problem: make sure your banning logic only runs once per actual command. Add a simple state check or timestamp to stop duplicates. And make sure your webhook endpoint immediately returns a 200 response after processing - otherwise Telegram keeps resending the same update.