From 35aa690bf1e19e56441769acad6a8284251330fe Mon Sep 17 00:00:00 2001 From: OG T Date: Thu, 26 Mar 2026 19:21:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(decision=5Fmanager):=20=E4=BF=AE=E5=BE=A9?= =?UTF-8?q?=20Telegram=20=E9=87=8D=E8=A4=87=E7=99=BC=E9=80=81=E5=95=8F?= =?UTF-8?q?=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 問題: - Phase 6.5 (765ee39) 修改導致每次 poll 都建立新 decision - 觸發 Telegram 轟炸 (INC-INC-INC- prefix bug) 修復: - 移除 INC- 重複前綴 (line 83) - 加入 Redis 去重機制 (10 分鐘 TTL) Co-Authored-By: Claude Opus 4.5 --- apps/api/src/services/decision_manager.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/api/src/services/decision_manager.py b/apps/api/src/services/decision_manager.py index 4f0c0143..970c36b6 100644 --- a/apps/api/src/services/decision_manager.py +++ b/apps/api/src/services/decision_manager.py @@ -59,6 +59,18 @@ async def _push_decision_to_telegram( get_telegram_gateway, ) + # 2026-03-26 修復: 防止重複發送 Telegram (每 incident 10 分鐘只發一次) + redis_client = get_redis() + dedup_key = f"telegram_sent:{incident.incident_id}" + already_sent = await redis_client.get(dedup_key) + if already_sent: + logger.debug( + "telegram_push_skipped_dedup", + incident_id=incident.incident_id, + reason="Already sent within 10 minutes", + ) + return + # 檢查是否有設定 Bot Token if not settings.OPENCLAW_TG_BOT_TOKEN: logger.debug( @@ -79,8 +91,8 @@ async def _push_decision_to_telegram( confidence = proposal_data.get("confidence", 0.75) source = proposal_data.get("source", "unknown") - # 建立 approval_id (使用 incident_id 作為追蹤) - approval_id = f"INC-{incident.incident_id}" + # 2026-03-26 修復: incident_id 已有 INC- 前綴,不要再加 + approval_id = incident.incident_id await gateway.send_approval_card( approval_id=approval_id, @@ -94,6 +106,9 @@ async def _push_decision_to_telegram( namespace=incident.signals[0].labels.get("namespace", "default") if incident.signals else "default", ) + # 2026-03-26 修復: 標記已發送,10 分鐘內不再發送 + await redis_client.set(dedup_key, "1", ex=600) + logger.info( "telegram_decision_pushed", incident_id=incident.incident_id,