This telegram bot tries to ban a predetermined user IDs each time added to a group. IDs are listed in a file. I'm using this telegram bot class and in webhook method.
$telegram = new Telegram($bot_id);
$chat_id = $telegram->ChatID();
$button = $telegram->text();
if ($button == "DoBan"){
$fn = fopen("ids.txt","r");
while(! feof($fn)) {
$result = fgets($fn);
$int_result = (int)$result;
$content = array('chat_id' => $chat_id, 'user_id' => "$int_result");
$telegram->kickChatMember($content);
}
fclose($fn);
$content2 = array('chat_id' => $chat_id, 'text' => "ban done! ");
$telegram->sendMessage($content2);
die();
}
But there is two problems here:
1. If IDs listed in our file haven't been banned before in the group bot is added to, the bot can't ban any of them. the point is if you ban IDs manually in any other group and perform a banning by bot there one time you can ban those IDs in any other group after that.
2. When bot receives the "DoBan" after it finishes the banning it get stuck in a some kind of loop and prints "ban done!" over and over like it's doing the banning all over again.
UPDATE:
Based on further researches there are two reasons for the two expressed problems
For the first problem: If your bot hasn't seen the user ID before it cannot ban it. So the bot has to see the user first somewhere, either by starting the bot or seeing that user inside the same group with bot
For the second problem: In case of an unsuccessful request telegram bot API thinks something is wrong with your server and requests again every few seconds so you have to add header("HTTP/1.1 200 OK");
to your script or do the increment on update_id
So this is the updated code
$telegram = new Telegram($bot_id);
header("HTTP/1.1 200 OK");
http_response_code(200);
$chat_id = $telegram->ChatID();
$result = $telegram->getData();
$text = $result['message'] ['text'];
$update = $result ['update_id'];
$result ['update_id'] = ++$update;
if ($button == "DoBan"){
$fn = fopen("ids.txt","r");
while(! feof($fn)) {
$result = fgets($fn);
$int_result = (int)$result;
$content = array('chat_id' => $chat_id, 'user_id' => "$int_result");
$telegram->kickChatMember($content);
}
fclose($fn);
$content2 = array('chat_id' => $chat_id, 'text' => "ban done! ");
$telegram->sendMessage($content2);
die();
}
So I need to know how should I exactly increase the update_id
or what is the proper way to add header("HTTP/1.1 200 OK");
becasue the problems still exist.