update message summary default time range

This commit is contained in:
liuwei
2026-04-10 09:27:08 +08:00
parent 42b2014f12
commit a67b8c296d

View File

@@ -129,12 +129,15 @@ class MessageSummaryPlugin(MessagePluginInterface):
"""处理消息"""
try:
# 检查是否是总结命令
content = message.get("content", "")
content = str(message.get("content", "") or "").strip()
self.bot: WechatAPIClient = message.get("bot")
if not content.startswith(self.command_prefix):
return False, None
command = content[len(self.command_prefix):].split()[0]
command_parts = content[len(self.command_prefix):].split()
if not command_parts:
return False, None
command = command_parts[0]
if command not in self.commands:
return False, None
# 获取需要总结的内容
@@ -151,20 +154,44 @@ class MessageSummaryPlugin(MessagePluginInterface):
# 从消息历史中获取群聊记录
all_contacts: dict = message.get("all_contacts")
group_members: dict = ContactManager.get_instance().get_group_members(group_id)
chat_content = self.message_storage.get_messages(group_id, group_members)
if len(chat_content) < 100:
custom_date = self._parse_summary_date(command_parts[1] if len(command_parts) > 1 else "")
if len(command_parts) > 1 and custom_date is None:
await self.bot.send_text_message(group_id, "日期格式不对,用 `#总结 2026-04-08` 这种。", message.get("sender"))
return False, None
period_start, period_end = self._resolve_manual_summary_range(custom_date)
message_count = self.message_storage.count_messages_by_date_range(group_id, period_start, period_end)
if message_count < 100:
await self.bot.send_text_message(
group_id,
f"这段时间消息太少了,只有 {message_count} 条,不总结了。",
message.get("sender"),
)
return False, None
chat_content = self.message_storage.get_messages_by_date_range(group_id, group_members, period_start, period_end)
if not chat_content:
await self.bot.send_text_message(group_id, "这段时间没捞到可总结的聊天记录。", message.get("sender"))
return False, None
# 获取群名并处理
group_name = all_contacts.get(group_id, group_id)
group_name = self._sanitize_group_name(group_name)
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(group_id, "⏳群消息总结中… 😊")
period_label = self._format_summary_period_label(period_start, period_end, custom_date is not None)
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(
group_id,
f"⏳ 正在总结 [{period_label}] 的群消息… 😊",
)
self.revoke.add_message_to_revoke(group_id, client_msg_id, create_time, new_msg_id, 5)
# 创建线程异步处理总结生成和发送
res = await self._async_generate_and_send_summary(chat_content, group_name, group_id,
message)
res = await self._async_generate_and_send_summary(
chat_content,
group_name,
group_id,
message,
period_start=period_start,
period_end=period_end,
message_count=message_count,
)
if res:
return True, "异步总结已启动"
else:
@@ -174,8 +201,16 @@ class MessageSummaryPlugin(MessagePluginInterface):
self.LOG.error(f"处理消息总结命令失败: {e}")
return False, None
async def _async_generate_and_send_summary(self, chat_content: str, group_name: str, group_id: str,
message: Dict[str, Any]):
async def _async_generate_and_send_summary(
self,
chat_content: str,
group_name: str,
group_id: str,
message: Dict[str, Any],
period_start: Optional[datetime] = None,
period_end: Optional[datetime] = None,
message_count: Optional[int] = None,
):
"""异步生成并发送总结"""
try:
# 生成总结
@@ -184,6 +219,16 @@ class MessageSummaryPlugin(MessagePluginInterface):
if image_path:
# 图片生成成功,发送图片
await self.bot.send_image_message(group_id, Path(image_path))
if period_start and period_end and message_count is not None:
self._save_daily_summary_record(
group_id,
group_name,
period_start,
period_end,
message_count,
summary,
str(image_path),
)
self.LOG.info(f"成功发送图片总结到群 {group_id}")
return True
else:
@@ -196,6 +241,16 @@ class MessageSummaryPlugin(MessagePluginInterface):
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(group_id, summary)
self.revoke.add_message_to_revoke(group_id, client_msg_id, create_time, new_msg_id, 30)
if period_start and period_end and message_count is not None:
self._save_daily_summary_record(
group_id,
group_name,
period_start,
period_end,
message_count,
summary,
None,
)
self.LOG.info(f"图片生成失败,已发送文本总结到群 {group_id}")
return True
else:
@@ -212,6 +267,34 @@ class MessageSummaryPlugin(MessagePluginInterface):
self.revoke.add_message_to_revoke(group_id, client_msg_id, create_time, new_msg_id, 5)
return False
@staticmethod
def _parse_summary_date(raw: str) -> Optional[datetime]:
text = str(raw or "").strip()
if not text:
return None
try:
return datetime.strptime(text, "%Y-%m-%d")
except ValueError:
return None
@staticmethod
def _resolve_manual_summary_range(custom_date: Optional[datetime]) -> Tuple[datetime, datetime]:
now = datetime.now()
if custom_date is not None:
day_start = custom_date.replace(hour=0, minute=0, second=0, microsecond=0)
day_end = custom_date.replace(hour=23, minute=59, second=59, microsecond=999999)
return day_start, day_end
yesterday = now - timedelta(days=1)
start = yesterday.replace(hour=0, minute=0, second=0, microsecond=0)
return start, now
@staticmethod
def _format_summary_period_label(start_time: datetime, end_time: datetime, is_custom_day: bool) -> str:
if is_custom_day:
return start_time.strftime("%Y-%m-%d")
return f"{start_time.strftime('%Y-%m-%d 00:00')} 到现在"
def _sanitize_group_name(self, group_name: str) -> str:
"""处理群名,去除特殊字符并限制长度"""
# 去除特殊字符,只保留字母、数字、中文和基本标点