总结+音乐

This commit is contained in:
liuwei
2025-04-30 16:04:55 +08:00
parent 4029f77cc2
commit d2cd6a66d2
2 changed files with 55 additions and 57 deletions

View File

@@ -1,11 +1,11 @@
import json
import re
import threading
from pathlib import Path
from typing import Dict, Any, Tuple, Optional, List
import requests
from message_util import MessageUtil
from utils.string_utils import remove_trailing_content
from utils.wechat.message_to_db import MessageStorage
from utils.compress_chat_data import compress_chat_data
@@ -15,6 +15,7 @@ from utils.decorator.points_decorator import plugin_points_cost
from utils.markdown_to_image import convert_md_str_to_image
from utils.decorator.plugin_decorators import plugin_stats_decorator
from utils.robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
from wechat_ipad import WechatAPIClient
class MessageSummaryPlugin(MessagePluginInterface):
@@ -46,6 +47,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
def __init__(self):
super().__init__()
self.bot: WechatAPIClient = None
def initialize(self, context: Dict[str, Any]) -> bool:
"""初始化插件"""
@@ -55,7 +57,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.message_util: MessageUtil = context.get("message_util")
self.LOG.info(f"初始化 {self.name} 插件成功")
return True
@@ -80,11 +81,12 @@ class MessageSummaryPlugin(MessagePluginInterface):
@plugin_stats_decorator(plugin_name="群聊总结")
@plugin_points_cost(10, "群聊总结消耗积分", Feature.SUMMARY_CAPABILITY)
def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
async def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""处理消息"""
try:
# 检查是否是总结命令
content = message.get("content", "")
self.bot: WechatAPIClient = message.get("bot")
if not content.startswith(self.command_prefix):
return False, None
@@ -94,7 +96,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
# 获取需要总结的内容
group_id = message.get("roomid")
if not group_id:
self.message_util.send_text("只支持群聊消息总结", message.get("sender"))
await self.bot.send_text_message(group_id, "只支持群聊消息总结", message.get("sender"))
return False, None
# 权限判断
gbm: GroupBotManager = message.get("gbm")
@@ -110,7 +112,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
# 获取群名并处理
group_name = all_contacts.get(group_id, group_id)
group_name = self._sanitize_group_name(group_name)
self.message_util.send_text("⏳群消息总结中… 😊", group_id)
await self.bot.send_text_message(group_id, "⏳群消息总结中… 😊")
# 创建线程异步处理总结生成和发送
summary_thread = threading.Thread(
@@ -134,12 +136,12 @@ class MessageSummaryPlugin(MessagePluginInterface):
summary, image_path = self._generate_summary(chat_content, group_name)
if image_path:
self.message_util.send_file(image_path, group_id)
self.bot.send_image_message(group_id, Path(image_path))
else:
self.message_util.send_text("❌ 生成总结图片失败", group_id)
self.bot.send_text_message(group_id, "❌ 生成总结图片失败")
except Exception as e:
self.LOG.error(f"异步生成总结失败: {e}")
self.message_util.send_text(f"❌ 生成总结失败: {str(e)}", group_id)
self.bot.send_text_message(group_id, f"❌ 生成总结失败: {str(e)}")
def _sanitize_group_name(self, group_name: str) -> str:
"""处理群名,去除特殊字符并限制长度"""

View File

@@ -151,55 +151,51 @@ class MusicPlugin(MessagePluginInterface):
singer_pic = song_info.get("singer_pic", "")
data_url = song_info.get("data_url", "")
xml_message = f"""
<?xml version="1.0"?>
<msg>
<appmsg appid="wx904fb3ecf62c7dea" sdkver="0">
<title>{song_name}</title>
<des>{singer_name}</des>
<action>view</action>
<type>3</type>
<showtype>0</showtype>
<content />
<url>{data_url}</url>
<dataurl>{play_url}</dataurl>
<lowurl/>
<lowdataurl/>
<recorditem />
<thumburl />
<messageaction />
<laninfo />
<extinfo />
<sourceusername />
<sourcedisplayname />
<commenturl />
<appattach>
<totallen>0</totallen>
<attachid />
<emoticonmd5></emoticonmd5>
<fileext />
<aeskey></aeskey>
</appattach>
<webviewshared>
<publisherId />
<publisherReqId>0</publisherReqId>
</webviewshared>
<weappinfo>
<pagepath />
<username />
<appid />
<appservicetype>0</appservicetype>
</weappinfo>
<websearch />
<songalbumurl>{singer_pic}</songalbumurl>
</appmsg>
<scene>0</scene>
<appinfo>
<version>49</version>
<appname>汽水音乐</appname>
</appinfo>
<commenturl />
</msg>"""
xml_message = f"""<appmsg appid="wx904fb3ecf62c7dea" sdkver="0">
<title>{song_name}</title>
<des>{singer_name}</des>
<action>view</action>
<type>3</type>
<showtype>0</showtype>
<content />
<url>{data_url}</url>
<dataurl>{play_url}</dataurl>
<lowurl/>
<lowdataurl/>
<recorditem />
<thumburl />
<messageaction />
<laninfo />
<extinfo />
<sourceusername />
<sourcedisplayname />
<commenturl />
<appattach>
<totallen>0</totallen>
<attachid />
<emoticonmd5></emoticonmd5>
<fileext />
<aeskey></aeskey>
</appattach>
<webviewshared>
<publisherId />
<publisherReqId>0</publisherReqId>
</webviewshared>
<weappinfo>
<pagepath />
<username />
<appid />
<appservicetype>0</appservicetype>
</weappinfo>
<websearch />
<songalbumurl>{singer_pic}</songalbumurl>
</appmsg>
<scene>0</scene>
<appinfo>
<version>49</version>
<appname>汽水音乐</appname>
</appinfo>
<commenturl />"""
self.LOG.info(f"发送音乐消息:{xml_message}")
res = await bot.send_app_message(wxid=receiver, xml=xml_message, type=0)