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