我寫php與電報機器人。我保存用戶chatid發送消息;使用該命令發送消息:發送消息在電報機器人
/admin sendall:hellow
,並在PHP應用程序中使用此代碼:
case '/admin':
if ($chat_id == 'my chatid') {
$array = str_replace('/admin', '', $message);
$array = trim($array);
$array = explode(':', $array);
$Admin = new AdminCommand();
$Admin->getCommand($array[0], $array[1]);
} else {
sendMessage($chat_id, 'block ');
}
break;
AdminCommand類:
class AdminCommand extends Database {
public function getCommand($command, $action = null) {
switch ($command) {
case 'sendall':
$this->sendall($action);
break;
default:
# code...
break;
}
}
public function sendall($message) {
$sql = $this->con->prepare('SELECT * FROM `users`');
$sql->execute();
$res = $sql->fetchAll();
foreach ($res as $row) {
sendMessage($row['chatid'], $message);
}
exit();
}
}
SendMessage函數:
function sendMessage($chatId, $message) {
$url = WEBSITE . "/sendMessage?chat_id=" . $chatId . "&text=" . urlencode($message);
file_get_contents($url);
}
最它是工作的時代e但有時在向所有用戶發送消息之後重複一遍又一遍,不停止只要我刪除數據庫。 有什麼問題?
我想你需要關閉連接。 – mhtb