diff --git a/.env b/.env index fcf4d3f..f41f765 100644 --- a/.env +++ b/.env @@ -8,4 +8,5 @@ TGPORT=4443 IMAGE=alpine:3.18.2 IP= VER= +TELEGRAM_API=api.telegram.org ENV=/root/.ashrc \ No newline at end of file diff --git a/app/bot.php b/app/bot.php index c5b99d3..bbb3f7d 100644 --- a/app/bot.php +++ b/app/bot.php @@ -15,12 +15,14 @@ class Bot public $reg; public $pool; public $hwid; + public $api; public function __construct($key, $i18n) { + $api = getenv('TELEGRAM_API'); $this->key = $key; - $this->api = "https://api.telegram.org/bot$key/"; - $this->file = "https://api.telegram.org/file/bot$key/"; + $this->api = "https://$api/bot$key/"; + $this->file = "https://$api/file/bot$key/"; $this->clients = '/config/clients.json'; $this->clients1 = '/config/clients1.json'; $this->pac = '/config/pac.json'; @@ -46,9 +48,9 @@ class Bot ]) . '~'; } - public function input() + public function input($data = false) { - $this->input_raw = $input = json_decode(file_get_contents('php://input'), true); + $this->input_raw = $input = $data ?: json_decode(file_get_contents('php://input'), true); $this->input = [ 'message' => $input['callback_query']['message']['text'] ?? $input['message']['text'] ?? $input['channel_post']['text'] ?? '', 'message_id' => $input['callback_query']['message']['message_id'] ?? $input['message']['message_id'] ?? $input['channel_post']['message_id'], @@ -9698,22 +9700,32 @@ DNS-over-HTTPS with IP: return $r; } - public function setwebhook() + public function polling() { - $ip = $this->ip; - if (empty($ip)) { - die('нет айпи'); - } - echo "$ip\n"; - var_dump($r = $this->request('setWebhook', [ - 'url' => "https://$ip/tlgrm?k={$this->key}", - 'certificate' => curl_file_create('/certs/self_public'), - 'allowed_updates' => json_encode(['*']), - ])); - if (!empty($r['result']) && $r['result'] == true) { + $offset = -1; + while (true) { + $r = $this->request('getUpdates', [ + 'offset' => $offset, + 'limit' => 3, + 'timeout' => 5, + ]); + if (!empty($r['description'])) { + error_log('getUpdates error: ' . $r['description']); + sleep(3); + continue; + } file_put_contents('/start', 1); - } else { - die("set webhook fail\n"); + if (!empty($r['result'])) { + foreach ($r['result'] as $v) { + try { + $this->input($v); + } catch (Throwable $e) { + var_dump('error:', $e); + } + $offset = max($offset, $v['update_id']); + } + $offset++; + } } } diff --git a/app/init.php b/app/init.php index a890ed9..71ddd53 100644 --- a/app/init.php +++ b/app/init.php @@ -11,6 +11,6 @@ if ($c['debug']) { $bot = new Bot($c['key'], $i); $bot->cleanQueue(); -$bot->setwebhook(); $bot->syncPortClients(); $bot->setcommands(); +$bot->polling(); diff --git a/scripts/start_php.sh b/scripts/start_php.sh index 9e65edf..7215eba 100755 --- a/scripts/start_php.sh +++ b/scripts/start_php.sh @@ -1,12 +1,10 @@ rm /ssh/key* ssh-keygen -m PEM -t rsa -f /ssh/key -N '' openssl req -newkey rsa:2048 -sha256 -nodes -x509 -days 365 -keyout /certs/self_private -out /certs/self_public -subj "/C=NN/ST=N/L=N/O=N/CN=$IP" -php init.php -if [[ -f "/start" && -f "/ssh/key.pub" && -s "/ssh/key.pub" ]]; then - unitd --log /logs/unit_error +if [[ -f "/ssh/key.pub" && -s "/ssh/key.pub" ]]; then + unitd --control 0.0.0.0:8080 --log /logs/unit_error curl -X PUT --data-binary @/config/unit.json --unix-socket /var/run/control.unit.sock http://localhost/config - pkill unitd - unitd --no-daemon --control 0.0.0.0:8080 --log /logs/unit_error + php init.php else exit 1; fi