From 30f9bb7877a4de56c2607efd89eb21cd0a5ff9b6 Mon Sep 17 00:00:00 2001 From: liuwei Date: Thu, 18 Dec 2025 08:59:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E5=A4=A9=E6=B0=94=E6=8E=A8?= =?UTF-8?q?=E9=80=81=EF=BC=8C=E5=8A=A0=E5=85=A5debug=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/weather/main.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/plugins/weather/main.py b/plugins/weather/main.py index d1f69ec..b02b3e4 100644 --- a/plugins/weather/main.py +++ b/plugins/weather/main.py @@ -234,8 +234,13 @@ class WeatherPlugin(MessagePluginInterface): async def _execute_daily_push(self): """执行全量推送 (基于 ID 聚合)""" - self.LOG.info("🚀 执行每日推送...") + self.LOG.info("🚀 [Weather] 开始执行每日推送任务...") + + if not self.bot: + self.LOG.warning("⚠️ [Weather] Bot 实例未初始化,无法发送消息!(若本次推送失败,请先在群里发送任意消息激活机器人)") + subs = self.redis_manager.get_all_subscriptions() + self.LOG.info(f"📋 [Weather] 共获取到 {len(subs)} 条订阅记录") if not subs: return # 1. 按 [city_id] 聚合 (真正的去重) @@ -244,21 +249,28 @@ class WeatherPlugin(MessagePluginInterface): for key, sub in subs.items(): cid = sub.get('city_id') cname = sub.get('city_name') - if not cid: continue # 兼容旧数据或错误数据 + if not cid: + self.LOG.warning(f"⚠️ [Weather] 订阅记录 {key} 缺少 city_id,已跳过") + continue # 兼容旧数据或错误数据 if cid not in agg_map: agg_map[cid] = {"name": cname, "users": []} agg_map[cid]["users"].append(sub) + + self.LOG.info(f"🏙️ [Weather] 聚合为 {len(agg_map)} 个城市待处理: {[info['name'] for info in agg_map.values()]}") # 2. 遍历 ID 获取天气 for city_id, info in agg_map.items(): try: city_name = info["name"] user_list = info["users"] + self.LOG.info(f"🔄 [Weather] 正在处理城市: {city_name} (ID: {city_id}), 关联订阅: {len(user_list)} 个") # 直接用 ID 查天气 (无需 Geo API,极速) api_data = await self._fetch_weather_by_id(city_id) - if not api_data: continue + if not api_data: + self.LOG.error(f"❌ [Weather] 获取城市 {city_name} (ID: {city_id}) 天气数据失败,跳过此城市推送") + continue # 获取 Redis 历史 (Key 也是 ID) history_data = self.redis_manager.get_history(city_id) @@ -269,7 +281,7 @@ class WeatherPlugin(MessagePluginInterface): if push_content: today_str = datetime.datetime.now().strftime("%m月%d日") final_msg = f"📅 {today_str} 天气早报\n{push_content}" - + # 聚合到目标ID维度:同一群只发一次,并@所有订阅者 target_map = {} for user in user_list: @@ -284,7 +296,9 @@ class WeatherPlugin(MessagePluginInterface): # 仅在群聊中@订阅者;私聊无需@列表 if room_id: target_map[target_id]['mentions'].add(sender_id) - + + self.LOG.info(f"📤 [Weather] 准备向 {len(target_map)} 个目标(群/人) 发送推送") + for target_id, info in target_map.items(): await asyncio.sleep(0.5) try: @@ -295,14 +309,19 @@ class WeatherPlugin(MessagePluginInterface): else: # 私聊:直接发送文本 await self.bot.send_text_message(target_id, final_msg) + self.LOG.info(f"✅ [Weather] 推送成功 -> {target_id}") + else: + self.LOG.error(f"❌ [Weather] Bot未就绪,无法推送给 -> {target_id}") except Exception as send_e: - self.LOG.error(f"推送给 {target_id} 失败: {send_e}") + self.LOG.error(f"❌ [Weather] 推送给 {target_id} 失败: {send_e}") + else: + self.LOG.warning(f"⚠️ [Weather] 城市 {city_name} 分析后无推送内容(可能是数据缺失)") # 存档 self._save_today_as_history(city_id, api_data) except Exception as e: - self.LOG.error(f"处理城市ID {city_id} 推送失败: {e}") + self.LOG.error(f"❌ [Weather] 处理城市ID {city_id} 发生异常: {e}") # ================= 核心分析算法 (逻辑不变) =================