【发布时间】:2016-03-11 09:31:59
【问题描述】:
我用 php 编写电报机器人。我保存用户聊天ID以发送消息;使用此命令发送消息:
/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);
}
大多数时候它工作正常,但有时在向所有用户发送消息后会一次又一次地重复,只要我删除数据库就不会停止。 有什么问题?
【问题讨论】:
-
我认为你需要关闭连接。
标签: php telegram telegram-bot