0

I create a private supergroup in Telegram and want to only allow certain telegram userid or phone number to be able to join.

Any unknown telegram userid/number that is not in a whitelist file of the bot will be automatically kicked.

Maybe store a whitelist in a file and run the bot in Cloud Server? There is an API to kick a member https://core.telegram.org/bots/api#kickchatmember but I don't know the webhook to trigger adding member event.

Anyone mind to code a telegram bot script for this? Sorry for inconvenience.

1 Answer 1

1

try this

<?php
$API_KEY = 'token';
define('API_KEY', $API_KEY);

function bot($method,$datas=[]){
$url = "https://api.telegram.org/bot" . API_KEY . "/" . $method;
$ch  = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datas);
$res = curl_exec($ch);
if (curl_error($ch)) {
    var_dump(curl_error($ch));
} else {
    return json_decode($res);
}
}

$update     = json_decode(file_get_contents('php://input'));
$message    = $update->message;
$text       = $message->text;
$chat_id    = $message->chat->id;
$from_id    = $message->from->id;
$new_member = $message->new_chat_member->id;
$memberid   = file_get_contents('whitelist.txt'); //put userid in whitelist.txt
$whitelist  = explode("\n", $memberid);

if ($new_member) {
    if (!in_array($chat_id, $whitelist)) {
           bot('kickChatMember',[
          'chat_id'=>$chat_id,
          'user_id'=>$message->new_chat_member->id]);
    }
}
1
  • Thank you. A little bit of editing and this perfectly suit my needs :) Aug 10, 2018 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.