用于将私人群组成员列入白名单的电报机器人代码
Telegram Bot code to whitelist a member of private group
我在 Telegram 中创建了一个私人超级组,并希望只允许某些电报用户 ID 或 phone 号码能够加入。
任何不在机器人白名单文件中的未知电报userid/number将被自动踢出。
也许将白名单存储在文件中,运行 机器人存储在云服务器中?有一个 API 可以踢成员 https://core.telegram.org/bots/api#kickchatmember 但我不知道 webhook 来触发添加成员事件。
有人愿意为此编写电报机器人脚本吗?抱歉给您带来不便。
试试这个
<?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]);
}
}
我在 Telegram 中创建了一个私人超级组,并希望只允许某些电报用户 ID 或 phone 号码能够加入。
任何不在机器人白名单文件中的未知电报userid/number将被自动踢出。
也许将白名单存储在文件中,运行 机器人存储在云服务器中?有一个 API 可以踢成员 https://core.telegram.org/bots/api#kickchatmember 但我不知道 webhook 来触发添加成员事件。
有人愿意为此编写电报机器人脚本吗?抱歉给您带来不便。
试试这个
<?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]);
}
}