From 477aab3f6ff60361a684233d081ad4ba255c4d7e Mon Sep 17 00:00:00 2001 From: OoO Date: Sat, 2 May 2026 13:09:08 +0800 Subject: [PATCH] refactor(telegram): migrate edm_notifier text path to EventRouter (ADR-019 Phase 5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit services/edm_notifier.py 的 _send_telegram() 處理 EDM 媒體告警,原本 if/else 分流 sendPhoto / sendMessage。 行為變化: - 純文字分支(無 image_path):改走 services.event_router.dispatch_sync() event_type=edm_media_alert, severity=warning - 含圖片分支(sendPhoto with multipart file upload):依 ADR-019 任務指示 保留直連 Telegram API(EventRouter 不支援 file upload,列為 known skip) - caller 行為不變,失敗仍 logger.error 不阻斷主線 Co-Authored-By: Claude Opus 4.7 (1M context) --- services/edm_notifier.py | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/services/edm_notifier.py b/services/edm_notifier.py index 290348c..cb68742 100644 --- a/services/edm_notifier.py +++ b/services/edm_notifier.py @@ -94,20 +94,41 @@ class EdmNotifier: self.logger.error(f"Line 發送失敗: {e}") def _send_telegram(self, text, image_path): + """ + EDM / 媒體告警 Telegram 出口 + + ADR-019 Phase 5: + - 純文字(無 image_path):走 EventRouter dispatch_sync 統一入口 + - 含圖片(sendPhoto with file upload):EventRouter 不支援 multipart + file upload,保留直連 Telegram API(ADR-019 任務指示中明列為 skip 類別) + """ bot_token = config.TELEGRAM_BOT_TOKEN chat_ids = config.TELEGRAM_CHAT_IDS - + + # 純文字分支:走 EventRouter + if not (image_path and os.path.exists(image_path)): + try: + from services.event_router import dispatch_sync + dispatch_sync(event={ + "event_type": "edm_media_alert", + "severity": "warning", + "source": "EDMNotifier", + "title": "EDM 媒體告警", + "summary": text[:400], + "status": "media_alert", + "payload": {"raw_message": text}, + }, admin_chat_ids=list(chat_ids) if chat_ids else None) + except Exception as e: + self.logger.error(f"Telegram 發送失敗 (EventRouter): {e}") + return + + # 含圖片分支:保留 sendPhoto multipart upload(EventRouter 不支援檔案) for chat_id in chat_ids: try: - if image_path and os.path.exists(image_path): - url = f"https://api.telegram.org/bot{bot_token}/sendPhoto" - with open(image_path, 'rb') as f: - data = {'chat_id': chat_id, 'caption': text} - files = {'photo': f} - requests.post(url, data=data, files=files, timeout=20) - else: - url = f"https://api.telegram.org/bot{bot_token}/sendMessage" - data = {'chat_id': chat_id, 'text': text} - requests.post(url, data=data, timeout=10) + url = f"https://api.telegram.org/bot{bot_token}/sendPhoto" + with open(image_path, 'rb') as f: + data = {'chat_id': chat_id, 'caption': text} + files = {'photo': f} + requests.post(url, data=data, files=files, timeout=20) except Exception as e: self.logger.error(f"Telegram 發送失敗 (ChatID: {chat_id}): {e}") \ No newline at end of file