为定时群总结提示增加自动撤回
- 为定时总结开始提示增加自动撤回能力 - 为定时总结失败提示增加自动撤回能力 - 新增统一的文本发送并登记撤回辅助方法 - 定时任务场景下自动懒初始化撤回器,避免提醒消息长期留在群里
This commit is contained in:
@@ -69,6 +69,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
super().__init__()
|
||||
self.message_storage = None
|
||||
self.revoke = None
|
||||
self._auto_revoke = None
|
||||
# 注册功能权限
|
||||
self.feature = self.register_feature()
|
||||
# 注册定时任务:每天早上9点总结昨天的聊天信息
|
||||
@@ -206,6 +207,22 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
sanitized_name = "群聊"
|
||||
return sanitized_name
|
||||
|
||||
def _get_revoke_manager(self) -> Optional[MessageAutoRevoke]:
|
||||
"""优先使用消息上下文中的撤回器,定时任务场景则懒初始化一个"""
|
||||
if self.revoke:
|
||||
return self.revoke
|
||||
if self._auto_revoke is None and self.bot:
|
||||
self._auto_revoke = MessageAutoRevoke(self.bot)
|
||||
return self._auto_revoke
|
||||
|
||||
async def _send_text_with_revoke(self, target: str, content: str, delay: int) -> Tuple[int, int, int]:
|
||||
"""发送文本并按需登记自动撤回"""
|
||||
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(target, content)
|
||||
revoke_manager = self._get_revoke_manager()
|
||||
if revoke_manager:
|
||||
revoke_manager.add_message_to_revoke(target, client_msg_id, create_time, new_msg_id, delay)
|
||||
return client_msg_id, create_time, new_msg_id
|
||||
|
||||
async def _generate_summary(self, chat_content: str, group_name: str) -> Tuple[str, Optional[str]]:
|
||||
"""生成总结"""
|
||||
# Dify API配置
|
||||
@@ -375,9 +392,10 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
f"开始为群 {group_name} 生成总结,消息数量: {message_count},内容长度: {len(chat_content)}")
|
||||
|
||||
# 发送提示消息
|
||||
await self.bot.send_text_message(
|
||||
await self._send_text_with_revoke(
|
||||
group_id,
|
||||
f"⏳ 正在生成 [{yesterday.strftime('%Y-%m-%d')}]聊天总结… 😊"
|
||||
f"⏳ 正在生成 [{yesterday.strftime('%Y-%m-%d')}]聊天总结… 😊",
|
||||
5
|
||||
)
|
||||
|
||||
# 生成总结
|
||||
@@ -394,8 +412,15 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
if len(summary) > max_length:
|
||||
summary = summary[:max_length] + "\n\n... (内容过长,已截断)"
|
||||
|
||||
await self.bot.send_text_message(group_id, summary)
|
||||
self.LOG.info(f"成功发送群 {group_name} 的昨日总结文本")
|
||||
if summary.strip() == "生成总结时出错":
|
||||
await self._send_text_with_revoke(group_id, f"❌ [{yesterday.strftime('%Y-%m-%d')}] 聊天总结生成失败,请稍后再试", 5)
|
||||
self.LOG.warning(f"群 {group_name} 的昨日总结生成失败,已发送可撤回失败提醒")
|
||||
else:
|
||||
await self.bot.send_text_message(group_id, summary)
|
||||
self.LOG.info(f"成功发送群 {group_name} 的昨日总结文本")
|
||||
else:
|
||||
await self._send_text_with_revoke(group_id, f"❌ [{yesterday.strftime('%Y-%m-%d')}] 聊天总结生成失败,请稍后再试", 5)
|
||||
self.LOG.warning(f"群 {group_name} 的昨日总结无有效内容,已发送可撤回失败提醒")
|
||||
|
||||
# 避免请求过快
|
||||
await asyncio.sleep(2)
|
||||
|
||||
Reference in New Issue
Block a user