refactor(telegram): migrate edm_notifier text path to EventRouter (ADR-019 Phase 5)
Some checks failed
CD Pipeline / deploy (push) Has been cancelled

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) <noreply@anthropic.com>
This commit is contained in:
OoO
2026-05-02 13:09:08 +08:00
parent 3e9d53c98c
commit 477aab3f6f

View File

@@ -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 uploadEventRouter 不支援 multipart
file upload保留直連 Telegram APIADR-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 uploadEventRouter 不支援檔案)
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}")