I write telegram bot with php . I save users chatid for send message ; use this command for send message :
/admin sendall:hellow
and in php app use this code :
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:
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:
function sendMessage($chatId, $message) {
$url = WEBSITE . "/sendMessage?chat_id=" . $chatId . "&text=" . urlencode($message);
file_get_contents($url);
}
Most of the times it's work fine but sometimes after send message to all users repeats that again and again and don't stop As long as i'm delete database . what's the problem ?