asemxin commited on
Commit
42ed4b2
·
1 Parent(s): 105fd67

fix: 改用 start_time 增量轮询+分页,彻底修复新消息漏检

Browse files
Files changed (1) hide show
  1. image_daemon.py +43 -18
image_daemon.py CHANGED
@@ -58,29 +58,43 @@ def get_bot_chats(token):
58
  log(f"❌ get_bot_chats 异常: {type(e).__name__}: {e}")
59
  return []
60
 
61
- def get_recent_messages(token, chat_id, limit=50):
62
- """获取聊天中最近的消息"""
 
 
63
  try:
64
  headers = {"Authorization": f"Bearer {token}"}
65
- resp = requests.get(f"{FEISHU_BASE}/im/v1/messages",
66
- headers=headers,
67
- params={
68
  "container_id_type": "chat",
69
  "container_id": chat_id,
70
- "sort_type": "ByCreateTimeDesc",
71
  "page_size": limit
72
- }, timeout=10)
73
- data = resp.json()
74
- code = data.get("code", -1)
75
- if code != 0:
76
- msg = data.get("msg", "unknown")
77
- log(f"❌ 获取消息失败 chat={chat_id} (code={code}): {msg}")
78
- log(f" 需要权限: im:message / im:message:readonly")
79
- return []
80
- return data.get("data", {}).get("items", [])
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  except Exception as e:
82
  log(f"❌ get_recent_messages 异常: {type(e).__name__}: {e}")
83
- return []
84
 
85
  def extract_image_keys(msg):
86
  """从消息中提取所有 image_key"""
@@ -256,6 +270,8 @@ def main():
256
  token = None
257
  token_time = 0
258
  cycle = 0
 
 
259
 
260
  while True:
261
  try:
@@ -287,7 +303,9 @@ def main():
287
 
288
  new_imgs = 0
289
  for chat_id in chats:
290
- messages = get_recent_messages(token, chat_id)
 
 
291
 
292
  # 首轮打印所有消息类型统计
293
  if cycle == 1:
@@ -301,6 +319,13 @@ def main():
301
  new_in_chat = 0
302
  for msg in messages:
303
  msg_id = msg.get("message_id", "")
 
 
 
 
 
 
 
304
  if msg_id in processed:
305
  continue
306
  processed.add(msg_id)
@@ -319,7 +344,7 @@ def main():
319
  new_imgs += 1
320
 
321
  if new_in_chat > 0 or cycle % 20 == 0:
322
- log(f"📊 chat={chat_id[:12]}... 拉取 {len(messages)} 条, 新 {new_in_chat} 条, 已知 {len(processed)} 条")
323
 
324
 
325
  if new_imgs > 0:
 
58
  log(f"❌ get_bot_chats 异常: {type(e).__name__}: {e}")
59
  return []
60
 
61
+ def get_recent_messages(token, chat_id, start_time=None, limit=50):
62
+ """获取聊天中的消息,支持 start_time 增量拉取和分页"""
63
+ all_items = []
64
+ page_token = None
65
  try:
66
  headers = {"Authorization": f"Bearer {token}"}
67
+ while True:
68
+ params = {
 
69
  "container_id_type": "chat",
70
  "container_id": chat_id,
71
+ "sort_type": "ByCreateTimeAsc",
72
  "page_size": limit
73
+ }
74
+ if start_time:
75
+ params["start_time"] = start_time
76
+ if page_token:
77
+ params["page_token"] = page_token
78
+ resp = requests.get(f"{FEISHU_BASE}/im/v1/messages",
79
+ headers=headers, params=params, timeout=10)
80
+ data = resp.json()
81
+ code = data.get("code", -1)
82
+ if code != 0:
83
+ msg = data.get("msg", "unknown")
84
+ log(f"❌ 获取消息失败 chat={chat_id} (code={code}): {msg}")
85
+ log(f" 需要权限: im:message / im:message:readonly")
86
+ return all_items
87
+ items = data.get("data", {}).get("items", [])
88
+ all_items.extend(items)
89
+ # 检查是否有下一页
90
+ has_more = data.get("data", {}).get("has_more", False)
91
+ page_token = data.get("data", {}).get("page_token")
92
+ if not has_more or not page_token:
93
+ break
94
+ return all_items
95
  except Exception as e:
96
  log(f"❌ get_recent_messages 异常: {type(e).__name__}: {e}")
97
+ return all_items
98
 
99
  def extract_image_keys(msg):
100
  """从消息中提取所有 image_key"""
 
270
  token = None
271
  token_time = 0
272
  cycle = 0
273
+ # 每个 chat 的最后已知消息时间戳(毫秒字符串)
274
+ chat_last_time = {}
275
 
276
  while True:
277
  try:
 
303
 
304
  new_imgs = 0
305
  for chat_id in chats:
306
+ # 增量拉取:首轮不带 start_time(拉最近 50 条),后续用 start_time
307
+ start_time = chat_last_time.get(chat_id)
308
+ messages = get_recent_messages(token, chat_id, start_time=start_time)
309
 
310
  # 首轮打印所有消息类型统计
311
  if cycle == 1:
 
319
  new_in_chat = 0
320
  for msg in messages:
321
  msg_id = msg.get("message_id", "")
322
+ # 更新该 chat 的最新消息时间
323
+ create_time = msg.get("create_time", "")
324
+ if create_time:
325
+ old = chat_last_time.get(chat_id, "0")
326
+ if create_time > old:
327
+ chat_last_time[chat_id] = create_time
328
+
329
  if msg_id in processed:
330
  continue
331
  processed.add(msg_id)
 
344
  new_imgs += 1
345
 
346
  if new_in_chat > 0 or cycle % 20 == 0:
347
+ log(f"📊 chat={chat_id[:12]}... 拉取 {len(messages)} 条, 新 {new_in_chat} 条, 已知 {len(processed)} 条, start_time={start_time}")
348
 
349
 
350
  if new_imgs > 0: