@@ -24,7 +24,6 @@ from utils.string_utils import remove_trailing_content
|
||||
from utils.wechat.contact_manager import ContactManager
|
||||
from utils.wechat.message_to_db import MessageStorage
|
||||
from wechat_ipad import WechatAPIClient
|
||||
from db.message_summary_db import MessageSummaryDBOperator
|
||||
|
||||
|
||||
class MessageSummaryPlugin(MessagePluginInterface):
|
||||
@@ -69,7 +68,6 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.message_storage = None
|
||||
self.summary_db = None
|
||||
self.revoke = None
|
||||
# 注册功能权限
|
||||
self.feature = self.register_feature()
|
||||
@@ -84,7 +82,6 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
self._api_key = api_config.get("api_key", "app-McGLzBhBjeBCSEi7n83MtuTo")
|
||||
self._api_url = api_config.get("api_url", "http://192.168.2.240/v1/chat-messages")
|
||||
self.message_storage = MessageStorage()
|
||||
self.summary_db = MessageSummaryDBOperator(context["db_manager"])
|
||||
|
||||
self.LOG.debug(f"初始化 {self.name} 插件成功")
|
||||
return True
|
||||
@@ -199,7 +196,6 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
|
||||
def _sanitize_group_name(self, group_name: str) -> str:
|
||||
"""处理群名,去除特殊字符并限制长度"""
|
||||
group_name = group_name or ""
|
||||
# 去除特殊字符,只保留字母、数字、中文和基本标点
|
||||
sanitized_name = re.sub(r'[^\w\s\u4e00-\u9fff,.,。]', '', group_name)
|
||||
# 限制长度为15个字符
|
||||
@@ -210,47 +206,6 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
sanitized_name = "群聊"
|
||||
return sanitized_name
|
||||
|
||||
def _save_daily_summary(
|
||||
self,
|
||||
group_id: str,
|
||||
group_name: str,
|
||||
summary_text: str,
|
||||
image_path: Optional[str],
|
||||
yesterday_start: datetime,
|
||||
yesterday_end: datetime,
|
||||
message_count: int,
|
||||
chat_content_length: int
|
||||
) -> bool:
|
||||
"""保存每日总结结果,便于后续知识库提取。"""
|
||||
if not self.summary_db:
|
||||
self.LOG.warning("消息总结数据库未初始化,跳过保存每日总结")
|
||||
return False
|
||||
|
||||
period_key = yesterday_start.strftime("%Y-%m-%d")
|
||||
meta = {
|
||||
"source": "message_summary_plugin",
|
||||
"api_url": self._api_url,
|
||||
"has_image": bool(image_path),
|
||||
"chat_content_length": int(chat_content_length or 0),
|
||||
}
|
||||
saved = self.summary_db.save_summary({
|
||||
"chatroom_id": group_id,
|
||||
"group_name": group_name,
|
||||
"summary_type": "daily",
|
||||
"period_key": period_key,
|
||||
"period_start": yesterday_start.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"period_end": yesterday_end.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"source_message_count": message_count,
|
||||
"summary_text": summary_text,
|
||||
"image_path": image_path,
|
||||
"meta": meta,
|
||||
})
|
||||
if saved:
|
||||
self.LOG.info(f"已保存群 {group_id} 的每日总结到数据库: {period_key}")
|
||||
else:
|
||||
self.LOG.error(f"保存群 {group_id} 的每日总结失败: {period_key}")
|
||||
return saved
|
||||
|
||||
async def _generate_summary(self, chat_content: str, group_name: str) -> Tuple[str, Optional[str]]:
|
||||
"""生成总结"""
|
||||
# Dify API配置
|
||||
@@ -422,18 +377,6 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
||||
# 生成总结
|
||||
summary, image_path = await self._generate_summary(chat_content, group_name)
|
||||
|
||||
if summary and len(summary.strip()) > 0:
|
||||
self._save_daily_summary(
|
||||
group_id=group_id,
|
||||
group_name=group_name,
|
||||
summary_text=summary,
|
||||
image_path=image_path,
|
||||
yesterday_start=yesterday_start,
|
||||
yesterday_end=yesterday_end,
|
||||
message_count=message_count,
|
||||
chat_content_length=len(chat_content),
|
||||
)
|
||||
|
||||
if image_path:
|
||||
# 图片生成成功,发送图片
|
||||
await self.bot.send_image_message(group_id, Path(image_path))
|
||||
|
||||
Reference in New Issue
Block a user