wrote a bot that should block absolutely all new members of the chat (excluding those usernames in the database)

Here is my code:

$data = json_decode(file_get_contents('php://input')); if (isset($data->{'message'}->{'chat'}->{'type'}) && $data->{'message'}->{'chat'}->{'type'} == 'supergroup') { $id_user = $data->{'message'}->{'new_chat_members'}->{'id'}; $id_chat = $data->{'message'}->{'chat'}->{'id'}; $username_chat = $data->{'message'}->{'chat'}->{'username'}; $username_user = $data->{'message'}->{'new_chat_members'}->{'username'}; $query_count = mysqli_query($connect, "SELECT * FROM `telegram` WHERE `chat_username` = '".$username_chat."' AND `username` = '".$username_user."'"); $query_count = mysqli_num_rows($query_count); if (!($query_count)) file_get_contents("https://api.telegram.org/botHASH/restrictChatMember?chat_id=".$id_chat."&user_id=".$id_user); } 

The problem is that it does not work clearly, those who are in the database are not clearly banned, but ordinary users will be banned once or later.

What is the problem?

Thank you in advance!

    2 answers 2

    This is because you only take one invite, and you must accept more than 1 invite at a time.

    $ data-> message-> new_chat_members is an array, in this array all users who have been invited.

    Corrected your code:

      if (isset($data->message->chat->type) && $data->message->chat->type === 'supergroup') { $id_chat = $data->message->chat->id; $username_chat = $data->message->chat->username; foreach ($data->message->new_chat_members as $value) { $query_count = mysqli_query($connect, "SELECT * FROM `tm` WHERE `chat_username` = '".$username_chat."' AND `username` = '".$value->username."'"); $query_count = mysqli_num_rows($query_count); if (!($query_count)) file_get_contents("https://api.telegram.org/botHASH/restrictChatMember?chat_id=".$id_chat."&user_id=".$value->id); } } 

      It is quite difficult to answer the reason without logging in this case.

      I would act as follows:

      1. I found a hare that slipped through and was able to drive to the chat and not be banned.
      2. make a request to the database with this user data and chat.
      3. Checked that the base sends on this request. It may be that you do not know everything about the data that is stored in your database :-)

         $username_user = 'тут имя пользователя которого не забанили хотя должны были'; $username_chat = 'тут соответственно чат пользователя имя которого написали выше'; $query_count = mysqli_query($connect, "SELECT * FROM `telegram` WHERE `chat_username` = '".$username_chat."' AND `username` = '".$username_user."'"); // и дальше смотрим что нам приезжает из базы. если Ваш if не отрабатывает значит база шлет какие-то данные, надо понять что там, следовательно var_dump в помощь. var_dump($query_count);