删除老代码,迁移部分文件
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
import logging
|
||||
import os
|
||||
import random
|
||||
import tomllib
|
||||
|
||||
import requests
|
||||
from wcferry import WxMsg, Wcf
|
||||
|
||||
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
|
||||
import lz4.block as lb
|
||||
|
||||
|
||||
class BeautyLeg:
|
||||
def __init__(self, wcf: Wcf, gbm: GroupBotManager):
|
||||
self.LOG = logging.getLogger(__name__)
|
||||
self.wcf = wcf # 假设 wcf 对象在此类中初始化
|
||||
self.gbm = gbm # 权限功能
|
||||
with open("beautyleg/config.toml", "rb") as f:
|
||||
plugin_config = tomllib.load(f)
|
||||
|
||||
config = plugin_config["Beautyleg"]
|
||||
|
||||
self.enable = config["enable"]
|
||||
self.command = config["command"]
|
||||
self.LOG.info(f"[美腿] 组件初始化完成,指令: {self.command}")
|
||||
|
||||
def get_random_file_from_dir(self, directory):
|
||||
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
|
||||
image_files = []
|
||||
|
||||
if not os.path.exists(directory):
|
||||
print(f"Error: Directory '{directory}' does not exist.")
|
||||
return None
|
||||
|
||||
if not os.access(directory, os.R_OK):
|
||||
print(f"Error: No read access to directory '{directory}'.")
|
||||
return None
|
||||
|
||||
print(f"Scanning directory: {directory} (including subdirectories)")
|
||||
|
||||
# 使用 os.walk() 递归遍历所有子目录
|
||||
for root, _, files in os.walk(directory):
|
||||
for file in files:
|
||||
_, ext = os.path.splitext(file)
|
||||
if ext.lower() in image_extensions:
|
||||
full_path = os.path.join(root, file)
|
||||
image_files.append(full_path)
|
||||
|
||||
if not image_files:
|
||||
print("No image files found in the directory (including subdirectories).")
|
||||
return None
|
||||
|
||||
return random.choice(image_files)
|
||||
|
||||
def handle_message(self, message: WxMsg):
|
||||
if not self.enable:
|
||||
return
|
||||
|
||||
content = str(message.content).strip()
|
||||
command = content.split(" ")
|
||||
|
||||
if command[0] not in self.command:
|
||||
return
|
||||
|
||||
# 如果触发了指令,但是没有权限,则返回权限不足
|
||||
if self.gbm.get_group_permission(message.roomid, Feature.BEAUTY_LEG) == PermissionStatus.DISABLED:
|
||||
return
|
||||
|
||||
# 使用示例
|
||||
directory = 'beautyleg/download_dir' # 替换为你的目录路径
|
||||
random_file_path = self.get_random_file_from_dir(directory)
|
||||
random_file_path = os.path.abspath(random_file_path)
|
||||
self.LOG.info(f"BeautyLeg.random_file_path: {random_file_path}")
|
||||
if random_file_path:
|
||||
return self.wcf.send_file(random_file_path, message.roomid)
|
||||
else:
|
||||
return None
|
||||
@@ -1,3 +0,0 @@
|
||||
[Beautyleg]
|
||||
enable = true
|
||||
command = ["美腿", "腿来"]
|
||||
@@ -1,18 +0,0 @@
|
||||
[Summmary]
|
||||
enable = true
|
||||
|
||||
api-key = "app-McGLzBhBjeBCSEi7n83MtuTo" # Dify的API Key
|
||||
base-url = "http://192.168.2.240/v1" #Dify API接口base url
|
||||
|
||||
commands = ["深度总结", "群聊总结"]
|
||||
command-tip = """-----Bot-----
|
||||
💬AI聊天指令:
|
||||
深度总结
|
||||
"""
|
||||
|
||||
price = 0 # 用一次扣积分,如果0则不扣
|
||||
|
||||
# Http代理设置
|
||||
# 格式: http://用户名:密码@代理地址:代理端口
|
||||
# 例如:http://127.0.0.1:7890
|
||||
http-proxy = ""
|
||||
@@ -1,109 +0,0 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
|
||||
def extract_content(data_string):
|
||||
try:
|
||||
data = json.loads(data_string)
|
||||
|
||||
# 提取content字段
|
||||
content = data["choices"][0]["message"].get("content", "")
|
||||
|
||||
# 提取tokens相关内容,加入容错处理
|
||||
tokens_usage = data.get("usage", {})
|
||||
|
||||
# 确保tokens_usage是字典类型
|
||||
if isinstance(tokens_usage, dict):
|
||||
prompt_tokens = tokens_usage.get("prompt_tokens", 0)
|
||||
completion_tokens = tokens_usage.get("completion_tokens", 0)
|
||||
total_tokens = tokens_usage.get("total_tokens", 0)
|
||||
else:
|
||||
prompt_tokens = completion_tokens = total_tokens = 0
|
||||
|
||||
# 如果tokens信息为空,提供默认值或提示
|
||||
if prompt_tokens == 0 and completion_tokens == 0 and total_tokens == 0:
|
||||
tokens_info = "\n\n【tokens】暂无数据"
|
||||
else:
|
||||
tokens_info = (f"\n\n【tokens】输入: {prompt_tokens} 生成: {completion_tokens} 总: {total_tokens}")
|
||||
|
||||
# 将tokens信息添加到content后面,返回为字符串
|
||||
content_with_tokens = content + tokens_info
|
||||
|
||||
return content_with_tokens
|
||||
|
||||
except json.JSONDecodeError:
|
||||
print("Invalid JSON")
|
||||
return None
|
||||
|
||||
|
||||
def message_summary(content):
|
||||
# 设置Authorization和URL
|
||||
|
||||
authorization = "Bearer b8586595-eb81-483d-8e91-a35cc789729e" # 请替换为真实的Authorization token
|
||||
url = 'https://ark.cn-beijing.volces.com/api/v3/chat/completions'
|
||||
# 群聊精华总结生成指令
|
||||
prompt = """
|
||||
# 🌟「[最新日期] 群聊总结」🌟
|
||||
|
||||
## 📊 今日数据快报
|
||||
- **总消息数**:📩 [消息数] 条
|
||||
- **最活跃时段**:🔥 [时段] (📈 [消息数] 条/小时)
|
||||
- **聊天时段**:🕒 [开始时间] - [结束时间]
|
||||
|
||||
## 🌌 话题总结
|
||||
|
||||
### 1️⃣ 【[话题1]】 ⭐⭐⭐⭐⭐
|
||||
🕒**聊天时段**:[开始时间] - [结束时间]
|
||||
🔍 **精彩回顾**:
|
||||
> [精彩回顾内容提炼]
|
||||
🏅 **热点人物**:[@用户A] [@用户B]
|
||||
💬 **精彩语录**:"[精选金句]"
|
||||
|
||||
### 2️⃣ 【[话题2]】 ⭐⭐⭐⭐
|
||||
🕒**聊天时段**:[开始时间] - [结束时间]
|
||||
🔍 **高能回顾**:
|
||||
> [精彩回顾内容提炼]
|
||||
📌 **实用内容**:[资源链接/知识点总结]
|
||||
|
||||
## 🎖️ 今日荣誉榜
|
||||
|
||||
🏆 **群聊 MVP**:[@用户D]
|
||||
👑 **获奖理由**:
|
||||
✅ 发起 [话题数] 个热门话题
|
||||
✅ 贡献 [数量] 个表情包/段子
|
||||
✅ **创新贡献**:"薯片袋静音开封法"(已申请专利 🎉)
|
||||
|
||||
✨ *本总结由 AI 自动生成,快来看看你今天是不是最靓的崽!🔥*
|
||||
|
||||
"""
|
||||
|
||||
data = {
|
||||
# "stream": True,
|
||||
"model": "doubao-1-5-lite-32k-250115",
|
||||
"messages": [
|
||||
{
|
||||
"role": "system",
|
||||
"content": f"{prompt}"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": f"{content}"
|
||||
}
|
||||
|
||||
]
|
||||
}
|
||||
|
||||
# 设置请求头
|
||||
headers = {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
"Authorization": authorization
|
||||
}
|
||||
|
||||
# 发送POST请求
|
||||
response = requests.post(url, headers=headers, data=json.dumps(data), )
|
||||
response.encoding = 'utf-8'
|
||||
|
||||
# 输出响应内容
|
||||
print(response.status_code)
|
||||
print(response.text)
|
||||
return extract_content(response.text)
|
||||
@@ -1,94 +0,0 @@
|
||||
import requests
|
||||
import json
|
||||
|
||||
from message_summary.compress_chat_data import compress_chat_data
|
||||
from message_summary.markdown_to_image import convert_md_str_to_image
|
||||
|
||||
|
||||
def message_summary_dify(content, sender: str = None):
|
||||
"""
|
||||
使用Dify API生成群聊消息总结
|
||||
|
||||
Args:
|
||||
content: 需要总结的群聊消息内容
|
||||
|
||||
Returns:
|
||||
生成的总结内容和图片路径
|
||||
"""
|
||||
# Dify API配置
|
||||
api_key = "app-McGLzBhBjeBCSEi7n83MtuTo" # 请替换为实际的API密钥
|
||||
url = "http://192.168.2.240/v1/chat-messages"
|
||||
content_compress = content
|
||||
try:
|
||||
content_compress = compress_chat_data(content)
|
||||
print(f"压缩内容成功:{len(content_compress)}--{len(content)}")
|
||||
except Exception as e:
|
||||
print(f"压缩内容失败:{e}")
|
||||
|
||||
# 准备请求数据
|
||||
data = {
|
||||
"inputs": {},
|
||||
"query": f"请根据以下{sender}群聊记录生成一份精华总结:\n\n{content_compress}",
|
||||
"response_mode": "blocking", # 使用阻塞模式,直接获取完整响应
|
||||
"conversation_id": "",
|
||||
"user": sender if sender is not None else "message_summary_bot",
|
||||
"files": [] # 不包含文件
|
||||
}
|
||||
|
||||
# 设置请求头
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
try:
|
||||
# 发送POST请求
|
||||
response = requests.post(url, headers=headers, json=data)
|
||||
response.raise_for_status() # 检查请求是否成功
|
||||
|
||||
# 解析响应
|
||||
response_data = response.json()
|
||||
print(f"Dify API响应状态码: {response.status_code}")
|
||||
print(f"响应数据: {json.dumps(response_data, ensure_ascii=False, indent=2)}")
|
||||
|
||||
# 提取回答内容
|
||||
answer = response_data.get("answer", "")
|
||||
spath = ""
|
||||
# 提取token使用情况
|
||||
metadata = response_data.get("metadata", {})
|
||||
usage = metadata.get("usage", {})
|
||||
|
||||
if usage:
|
||||
prompt_tokens = usage.get("prompt_tokens", 0)
|
||||
completion_tokens = usage.get("completion_tokens", 0)
|
||||
total_tokens = usage.get("total_tokens", 0)
|
||||
|
||||
# 添加token信息
|
||||
tokens_info = f"\n\n【tokens】输入: {prompt_tokens} 生成: {completion_tokens} 总: {total_tokens}"
|
||||
answer += tokens_info
|
||||
try:
|
||||
spath = convert_md_str_to_image(answer, "output.png")
|
||||
except Exception as e:
|
||||
print(f"生成image失败:{e}")
|
||||
# 返回文本内容和图片路径
|
||||
return answer, spath
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"请求Dify API时出错: {e}")
|
||||
return f"生成总结时出错: {str(e)}", None
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"解析Dify API响应时出错: {e}")
|
||||
return "解析API响应时出错", None
|
||||
|
||||
except Exception as e:
|
||||
print(f"处理总结时出现未知错误: {e}")
|
||||
return f"生成总结时出现未知错误: {str(e)}", None
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
content = """
|
||||
2025-03-14 14:30:15,Jyunere,别这样啊。
|
||||
"""
|
||||
msg = compress_chat_data(content, 5)
|
||||
print(f"{msg}\n {len(msg)},{len(content)}")
|
||||
@@ -4,10 +4,10 @@ from typing import Dict, Any, Tuple, Optional, List
|
||||
import requests
|
||||
|
||||
from message_storage.message_to_db import MessageStorage
|
||||
from message_summary.compress_chat_data import compress_chat_data
|
||||
from message_summary.markdown_to_image import convert_md_str_to_image
|
||||
from compress_chat_data import compress_chat_data
|
||||
from plugin_common.message_plugin_interface import MessagePluginInterface
|
||||
from plugin_common.plugin_interface import PluginStatus
|
||||
from plugins.message_summary.markdown_to_image import convert_md_str_to_image
|
||||
from plugins.stats_collector.decorators import plugin_stats_decorator
|
||||
from robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus
|
||||
|
||||
|
||||
Reference in New Issue
Block a user