总结+音乐
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import threading
|
import threading
|
||||||
|
from pathlib import Path
|
||||||
from typing import Dict, Any, Tuple, Optional, List
|
from typing import Dict, Any, Tuple, Optional, List
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from message_util import MessageUtil
|
|
||||||
from utils.string_utils import remove_trailing_content
|
from utils.string_utils import remove_trailing_content
|
||||||
from utils.wechat.message_to_db import MessageStorage
|
from utils.wechat.message_to_db import MessageStorage
|
||||||
from utils.compress_chat_data import compress_chat_data
|
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.markdown_to_image import convert_md_str_to_image
|
||||||
from utils.decorator.plugin_decorators import plugin_stats_decorator
|
from utils.decorator.plugin_decorators import plugin_stats_decorator
|
||||||
from utils.robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
|
from utils.robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
|
||||||
|
from wechat_ipad import WechatAPIClient
|
||||||
|
|
||||||
|
|
||||||
class MessageSummaryPlugin(MessagePluginInterface):
|
class MessageSummaryPlugin(MessagePluginInterface):
|
||||||
@@ -46,6 +47,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.bot: WechatAPIClient = None
|
||||||
|
|
||||||
def initialize(self, context: Dict[str, Any]) -> bool:
|
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_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._api_url = api_config.get("api_url", "http://192.168.2.240/v1/chat-messages")
|
||||||
self.message_storage = MessageStorage()
|
self.message_storage = MessageStorage()
|
||||||
self.message_util: MessageUtil = context.get("message_util")
|
|
||||||
|
|
||||||
self.LOG.info(f"初始化 {self.name} 插件成功")
|
self.LOG.info(f"初始化 {self.name} 插件成功")
|
||||||
return True
|
return True
|
||||||
@@ -80,11 +81,12 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
|
|
||||||
@plugin_stats_decorator(plugin_name="群聊总结")
|
@plugin_stats_decorator(plugin_name="群聊总结")
|
||||||
@plugin_points_cost(10, "群聊总结消耗积分", Feature.SUMMARY_CAPABILITY)
|
@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:
|
try:
|
||||||
# 检查是否是总结命令
|
# 检查是否是总结命令
|
||||||
content = message.get("content", "")
|
content = message.get("content", "")
|
||||||
|
self.bot: WechatAPIClient = message.get("bot")
|
||||||
if not content.startswith(self.command_prefix):
|
if not content.startswith(self.command_prefix):
|
||||||
return False, None
|
return False, None
|
||||||
|
|
||||||
@@ -94,7 +96,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
# 获取需要总结的内容
|
# 获取需要总结的内容
|
||||||
group_id = message.get("roomid")
|
group_id = message.get("roomid")
|
||||||
if not group_id:
|
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
|
return False, None
|
||||||
# 权限判断
|
# 权限判断
|
||||||
gbm: GroupBotManager = message.get("gbm")
|
gbm: GroupBotManager = message.get("gbm")
|
||||||
@@ -110,7 +112,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
# 获取群名并处理
|
# 获取群名并处理
|
||||||
group_name = all_contacts.get(group_id, group_id)
|
group_name = all_contacts.get(group_id, group_id)
|
||||||
group_name = self._sanitize_group_name(group_name)
|
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(
|
summary_thread = threading.Thread(
|
||||||
@@ -134,12 +136,12 @@ class MessageSummaryPlugin(MessagePluginInterface):
|
|||||||
summary, image_path = self._generate_summary(chat_content, group_name)
|
summary, image_path = self._generate_summary(chat_content, group_name)
|
||||||
|
|
||||||
if image_path:
|
if image_path:
|
||||||
self.message_util.send_file(image_path, group_id)
|
self.bot.send_image_message(group_id, Path(image_path))
|
||||||
else:
|
else:
|
||||||
self.message_util.send_text("❌ 生成总结图片失败", group_id)
|
self.bot.send_text_message(group_id, "❌ 生成总结图片失败")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.LOG.error(f"异步生成总结失败: {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:
|
def _sanitize_group_name(self, group_name: str) -> str:
|
||||||
"""处理群名,去除特殊字符并限制长度"""
|
"""处理群名,去除特殊字符并限制长度"""
|
||||||
|
|||||||
@@ -151,55 +151,51 @@ class MusicPlugin(MessagePluginInterface):
|
|||||||
singer_pic = song_info.get("singer_pic", "")
|
singer_pic = song_info.get("singer_pic", "")
|
||||||
data_url = song_info.get("data_url", "")
|
data_url = song_info.get("data_url", "")
|
||||||
|
|
||||||
xml_message = f"""
|
xml_message = f"""<appmsg appid="wx904fb3ecf62c7dea" sdkver="0">
|
||||||
<?xml version="1.0"?>
|
<title>{song_name}</title>
|
||||||
<msg>
|
<des>{singer_name}</des>
|
||||||
<appmsg appid="wx904fb3ecf62c7dea" sdkver="0">
|
<action>view</action>
|
||||||
<title>{song_name}</title>
|
<type>3</type>
|
||||||
<des>{singer_name}</des>
|
<showtype>0</showtype>
|
||||||
<action>view</action>
|
<content />
|
||||||
<type>3</type>
|
<url>{data_url}</url>
|
||||||
<showtype>0</showtype>
|
<dataurl>{play_url}</dataurl>
|
||||||
<content />
|
<lowurl/>
|
||||||
<url>{data_url}</url>
|
<lowdataurl/>
|
||||||
<dataurl>{play_url}</dataurl>
|
<recorditem />
|
||||||
<lowurl/>
|
<thumburl />
|
||||||
<lowdataurl/>
|
<messageaction />
|
||||||
<recorditem />
|
<laninfo />
|
||||||
<thumburl />
|
<extinfo />
|
||||||
<messageaction />
|
<sourceusername />
|
||||||
<laninfo />
|
<sourcedisplayname />
|
||||||
<extinfo />
|
<commenturl />
|
||||||
<sourceusername />
|
<appattach>
|
||||||
<sourcedisplayname />
|
<totallen>0</totallen>
|
||||||
<commenturl />
|
<attachid />
|
||||||
<appattach>
|
<emoticonmd5></emoticonmd5>
|
||||||
<totallen>0</totallen>
|
<fileext />
|
||||||
<attachid />
|
<aeskey></aeskey>
|
||||||
<emoticonmd5></emoticonmd5>
|
</appattach>
|
||||||
<fileext />
|
<webviewshared>
|
||||||
<aeskey></aeskey>
|
<publisherId />
|
||||||
</appattach>
|
<publisherReqId>0</publisherReqId>
|
||||||
<webviewshared>
|
</webviewshared>
|
||||||
<publisherId />
|
<weappinfo>
|
||||||
<publisherReqId>0</publisherReqId>
|
<pagepath />
|
||||||
</webviewshared>
|
<username />
|
||||||
<weappinfo>
|
<appid />
|
||||||
<pagepath />
|
<appservicetype>0</appservicetype>
|
||||||
<username />
|
</weappinfo>
|
||||||
<appid />
|
<websearch />
|
||||||
<appservicetype>0</appservicetype>
|
<songalbumurl>{singer_pic}</songalbumurl>
|
||||||
</weappinfo>
|
</appmsg>
|
||||||
<websearch />
|
<scene>0</scene>
|
||||||
<songalbumurl>{singer_pic}</songalbumurl>
|
<appinfo>
|
||||||
</appmsg>
|
<version>49</version>
|
||||||
<scene>0</scene>
|
<appname>汽水音乐</appname>
|
||||||
<appinfo>
|
</appinfo>
|
||||||
<version>49</version>
|
<commenturl />"""
|
||||||
<appname>汽水音乐</appname>
|
|
||||||
</appinfo>
|
|
||||||
<commenturl />
|
|
||||||
</msg>"""
|
|
||||||
|
|
||||||
self.LOG.info(f"发送音乐消息:{xml_message}")
|
self.LOG.info(f"发送音乐消息:{xml_message}")
|
||||||
res = await bot.send_app_message(wxid=receiver, xml=xml_message, type=0)
|
res = await bot.send_app_message(wxid=receiver, xml=xml_message, type=0)
|
||||||
|
|||||||
Reference in New Issue
Block a user