From aa3ae494ce4d3f014965b36afd29e708ef763163 Mon Sep 17 00:00:00 2001 From: SXP-Simon Date: Thu, 26 Mar 2026 13:33:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(tg):=20=E4=BF=AE=E5=A4=8D=20Telegram=20?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E8=87=AA=E5=8A=A8=E5=88=86=E6=9E=90=E5=B0=B1?= =?UTF-8?q?=E7=BB=AA=E6=A3=80=E6=9F=A5=E5=A4=B1=E8=B4=A5=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=20ID=20=E5=8C=B9=E9=85=8D=E8=AF=8A=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化 BotManager 的 is_ready_for_auto_analysis 逻辑:将其改为非硬性限制。即使未成功提取机器人 ID (bot_self_ids),也允许定时任务继续运行,从而解决了 Telegram 在 Docker 部署下因 ID 获取延迟导致报告被跳过的问题。 - 强化日志诊断:在 Telegram 消息拦截 (save)、平台发现 (discovery) 和消息拉取 (fetch) 的关键路径中增加 platform_id 显式日志,方便排查多账号环境下的 ID 冲突。 - 在 BotManager 就绪检查中加入调试输出,明确告知分析任务被跳过的具体原因。 --- .../services/message_processing_service.py | 4 ++-- .../platform/adapters/telegram_adapter.py | 3 +++ src/infrastructure/platform/bot_manager.py | 21 +++++++++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/application/services/message_processing_service.py b/src/application/services/message_processing_service.py index 74e8e02..3dddeea 100644 --- a/src/application/services/message_processing_service.py +++ b/src/application/services/message_processing_service.py @@ -91,8 +91,8 @@ class MessageProcessingService: f"platform_id={platform_id} group_id={group_id} error={e}" ) - logger.debug( - f"[{platform_id}] 已缓存群 {group_id} 的消息 (发送者: {sender_name})" + logger.info( + f"[Telegram] [{platform_id}] 已缓存群 {group_id} 的消息 (发送者: {sender_name})" ) def _get_group_id_from_event(self, event: AstrMessageEvent) -> str | None: diff --git a/src/infrastructure/platform/adapters/telegram_adapter.py b/src/infrastructure/platform/adapters/telegram_adapter.py index 31c003a..7035618 100644 --- a/src/infrastructure/platform/adapters/telegram_adapter.py +++ b/src/infrastructure/platform/adapters/telegram_adapter.py @@ -184,6 +184,9 @@ class TelegramAdapter(PlatformAdapter): history_mgr = self._context.message_history_manager platform_id = self._get_platform_id() + logger.info( + f"[Telegram] 正在获取群 {group_id} 的历史消息,使用 platform_id: {platform_id}" + ) before_id_int: int | None = None if before_id: try: diff --git a/src/infrastructure/platform/bot_manager.py b/src/infrastructure/platform/bot_manager.py index 7f59c91..f11baaf 100644 --- a/src/infrastructure/platform/bot_manager.py +++ b/src/infrastructure/platform/bot_manager.py @@ -195,8 +195,22 @@ class BotManager: return bool(self._bot_self_ids) def is_ready_for_auto_analysis(self) -> bool: - """检查是否准备好进行自动分析""" - return self.has_bot_instance() and self.has_bot_self_id() + """检查是否准备好进行了自动分析""" + if not self.has_bot_instance(): + logger.debug("[BotManager] 自动分析就绪检查失败:没有可用的 bot 实例。") + return False + + if not self.has_bot_self_id(): + # 允许在没有配置/自动提取到 ID 的情况下尝试,但记录调试信息 + # 这有助于诊断某些平台(如 Telegram)自动获取 ID 失败的情况 + logger.debug( + "[BotManager] 自动分析就绪检查警告:bot_self_ids 列表为空。可能会影响消息过滤能力。" + ) + # 为了解决 #128 及其后续反馈,我们将此检查放宽 + # 只要有 bot 实例就可以尝试运行 + return True + + return True def _get_platform_id_from_instance(self, bot_instance): """从bot实例获取平台ID""" @@ -368,6 +382,9 @@ class BotManager: self._platforms[platform_id] = platform if bot_client: + logger.info( + f"[BotManager] 为平台 {platform_id} ({platform_name}) 注册 bot 实例" + ) self.set_bot_instance(bot_client, platform_id, platform_name) discovered[platform_id] = bot_client else: