我怎样才能禁止某些号码检查机器人

how can i ban some numbers from checking on bot

比如有人输入命令“!user 123456”然后发送消息“禁止号码”

我试过了但是不行...

$ban = array('112233','123456'); if(strpos($message, "!user $ban") ===0){ sendMessage($chatId, "<u>Number Banned!</u>", $message_id); }

$ban = array('112233','123456'); 
$isbanned = false;
foreach ($ban as $b) {
  if(strpos($message, "!user $b") ===0) $isbanned = true;
}

if ($isbanned) { 
sendMessage($chatId, "<u>Number Banned!</u>", $message_id); 
}

如果你要执行更多命令,而不是使用 strpos,你应该从消息中解析出操作和数据,然后你可以使用逻辑在 user 上做一些事情,在 [= 上做一些事情15=] 等,使用消息的其余部分作为数据 或进一步拆分 .

<?php

$banned_users = ['123456', '112233'];

// parse out command from the message
$command = [
 'action' => '', // will contain user
 'data' => ''    // will contain 123456
];
if (isset($message[0], $message[1]) && $message[0] === '!') {
    $message = substr($message, 1);
    list($command['action'], $command['data']) = explode(' ', trim($message), 2);
}

// do commands
if ($command['action'] === 'user') {
    if (in_array($command['data'], $banned_users)) {
        sendMessage($chatId, "<u>Number Banned!</u>", $message_id);
    } else {
        sendMessage($chatId, "<u>Not Banned!</u>", $message_id);
    }
} elseif ($command['action'] === 'foobar') {
    //...
    echo 'Do foobar with: ' . $command['data'];
} else {
    sendMessage($chatId, "<u>Invalid command!</u>", $message_id);
}

那么如果要在多个地方使用像下面这样的东西:

if (in_array($command['data'], $banned_users)) {
    //sendMessage($chatId, "<u>Number Banned!</u>", $message_id);
    echo 'Number Banned!';
}

创建函数:

function is_banned($number) {
    return in_array($number, ['123456', '112233']);
}

然后用它代替 in_array

// ...
if ($command['action'] === 'user') {
    if (is_banned($command['data'])) {
        sendMessage($chatId, "<u>Number Banned!</u>", $message_id);
    }
    // ...
}