diff --git a/app/Services/TomTool/Telegram/Slave.php b/app/Services/TomTool/Telegram/Slave.php index 75dc21b..5f947a4 100755 --- a/app/Services/TomTool/Telegram/Slave.php +++ b/app/Services/TomTool/Telegram/Slave.php @@ -107,7 +107,7 @@ public static function fail(string $sChatCode = 'admin'): self // new public static function notify(string $sChatCode = 'admin'): self { - return self::start("fail", $sChatCode); + return self::start("notification", $sChatCode); } @@ -275,51 +275,29 @@ private function _send_master(): Flow $aData = $this->buildDataTom(); if ($this->enableSlaveQueue) { - $oFlow = ServiceHttpQueue::start("telegram_master")->arg($aData)->save(); - } else { - - $oFlow = Flow::start("_send_master"); - $sUrlMaSterBase = $_ENV["url_master_base"] ?? ""; - - if (!$sUrlMaSterBase) { - throw new \Exception("未设置env的url_master_base"); - } - - $sUrlMasterPost = "https://".$sUrlMaSterBase."/api/telegram/post"; - - try { - - $oHttpV2 = HttpV2::make($sUrlMasterPost); - - if ($this->enableAsync) { - $oHttpV2 = $oHttpV2->enableAsync(); - } - - $r = $oHttpV2->enableJsonAs()->setData($aData)->send(); - - if ($this->enableAsync) { - return $oFlow->sData("async")->done("async"); - } else { - $a = json_decode($r, true); - $isDone = $a["isDone"] ?? false; - - if ($isDone) { - return $oFlow->done(); - } else { - return $oFlow->fail($r); - } - - } - - } catch (\Throwable $e) { - $oFlow = $oFlow->fail($e->getMessage()); - } + return ServiceHttpQueue::start("telegram_master")->arg($aData)->save(); + } + + $flow = Flow::start("operations notification"); + try { + $root = dirname(__DIR__, 4); + require_once $root . '/ops/AdminNotice.php'; + $this->operationsEventKey ??= bin2hex(random_bytes(16)); + $receipts = \LegacyOperations\AdminNotice::submit($root, $aData, $this->operationsEventKey); + return $flow->done('TeleHub accepted ' . implode(',', $receipts)); + } catch (\Throwable $error) { + return $flow->fail($error->getMessage()); } - - return $oFlow; } - - // 直接从本地发到tg,几乎用不上,都走master了 + + private ?string $operationsEventKey = null; + + public function forOperationsQueueRow(int $id): self + { + $this->operationsEventKey = 'queue.' . $id; + return $this; + } + private function _send_local(): Flow { if ($this->enableSlaveQueue) { diff --git a/ops/AdminNotice.php b/ops/AdminNotice.php new file mode 100644 index 0000000..2be3332 --- /dev/null +++ b/ops/AdminNotice.php @@ -0,0 +1,66 @@ + 'log', + 'warn', 'warning' => 'warning', + 'fail', 'error' => 'error', + 'notify', 'notification' => 'notification', + 'base', 'admin', '' => 'base', + default => throw new \DomainException('Unregistered operations notification category'), + }; + } + + public static function submit(string $projectRoot, array $data, string $eventKey): array + { + $target = (string) ($data['sChatCode'] ?? ''); + if (!in_array($target, ['', 'base', 'admin'], true) || !empty($data['iChatId']) || !empty($data['sBotToken'])) { + throw new \DomainException('Explicit Telegram recipients require their own publisher'); + } + if (!empty($data['sPhotoUrl']) || !empty($data['sFileUrl'])) { + throw new \DomainException('Attachments require their original publisher'); + } + $configPath = '/etc/telehub-operations/' . hash('sha256', $projectRoot) . '.json'; + $config = json_decode((string) @file_get_contents($configPath), true, 16, JSON_THROW_ON_ERROR); + $bot = self::bot($data['sBotCode'] ?? ''); + $text = (string) ($data['sMsg'] ?? ''); + if (trim($text) === '') { + throw new \DomainException('Empty operations notification'); + } + $title = '【' . $config['project'] . '/生产 - 运行通知】' . "\n"; + $room = 4096 - mb_strlen($title, 'UTF-8'); + $receipts = []; + foreach (mb_str_split($text, $room, 'UTF-8') as $part => $body) { + $payload = json_encode(['bot' => $bot, 'target' => 'base', 'text' => $title . $body], JSON_THROW_ON_ERROR); + $http = curl_init('https://telehub.gemcook.net/api/v1/messages'); + curl_setopt_array($http, [ + CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload, CURLOPT_RETURNTRANSFER => true, + CURLOPT_CONNECTTIMEOUT => 4, CURLOPT_TIMEOUT => 15, CURLOPT_FOLLOWLOCATION => false, + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', 'Accept: application/json', + 'Authorization: Bearer ' . $config['key'], + 'Idempotency-Key: ' . $eventKey . '.' . $part, + ], + ]); + $response = curl_exec($http); + $status = curl_getinfo($http, CURLINFO_RESPONSE_CODE); + curl_close($http); + $receipt = is_string($response) ? (json_decode($response, true)['data'] ?? []) : []; + if (!in_array($status, [200, 202], true) || empty($receipt['id'])) { + throw new \RuntimeException('TeleHub submission unconfirmed (HTTP ' . $status . ')'); + } + if (in_array($receipt['status'] ?? '', ['failed', 'uncertain', 'cancelled'], true)) { + throw new \RuntimeException('TeleHub delivery requires review: ' . $receipt['id']); + } + $receipts[] = $receipt['id']; + } + return $receipts; + } +}