完整 AgentSkill:词库外部化 + 全部脚本/Skill 脱敏上传
- 高危词库从 process.py 抽到 config.toml [security] section - process.py 改为 _get_high_risk_words() 动态加载(配置优先) - 新增 15 个 QQ 操作脚本(已脱敏:QQ号/IP/Token → 占位符) - 新增 8 个 SKILL.md(qq-messenger/management/resolver/napcat-extras 等) - 新增 SKILL.md 入口(完整部署方案文档) - 保留 SDK 框架(plugin_modules.py, file_store_api.py, package.py 等)
This commit is contained in:
113
scripts/qq_get_history.py
Normal file
113
scripts/qq_get_history.py
Normal file
@ -0,0 +1,113 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
获取历史消息 - 通过 NapCat (OneBot) HTTP API
|
||||
|
||||
用于 qq-agent 回忆之前和某个群/用户聊过什么。当用户提到当前 session
|
||||
不清楚的内容时,调用此工具补充上下文。
|
||||
|
||||
用法:
|
||||
# 获取群聊历史消息
|
||||
python3 qq_get_history.py --gid 812704915 --num 10
|
||||
|
||||
# 获取私聊历史消息
|
||||
python3 qq_get_history.py --uid YOUR_ADMIN_QQ --num 10
|
||||
|
||||
# JSON 格式输出(供 agent 解析)
|
||||
python3 qq_get_history.py --gid 812704915 --num 5 --json
|
||||
python3 qq_get_history.py --uid YOUR_ADMIN_QQ --num 20 --json
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
import argparse
|
||||
import requests
|
||||
from datetime import datetime
|
||||
|
||||
CQHTTP_URL = "http://YOUR_NAP CAT_HOST:25570"
|
||||
|
||||
|
||||
def get_group_msg_history(group_id: str, count: int) -> list[dict]:
|
||||
"""获取群聊历史消息"""
|
||||
resp = requests.post(
|
||||
f"{CQHTTP_URL}/get_group_msg_history",
|
||||
json={"group_id": int(group_id), "count": count},
|
||||
timeout=10
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if data.get("status") != "ok":
|
||||
raise Exception(f"API error: {data}")
|
||||
return data.get("data", {}).get("messages", [])
|
||||
|
||||
|
||||
def get_private_msg_history(user_id: str, count: int) -> list[dict]:
|
||||
"""获取私聊历史消息"""
|
||||
resp = requests.post(
|
||||
f"{CQHTTP_URL}/get_friend_msg_history",
|
||||
json={"user_id": int(user_id), "count": count},
|
||||
timeout=10
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if data.get("status") != "ok":
|
||||
raise Exception(f"API error: {data}")
|
||||
return data.get("data", {}).get("messages", [])
|
||||
|
||||
|
||||
def format_messages(messages: list[dict]) -> list[dict]:
|
||||
"""提取消息中的关键字段,便于 agent 解析"""
|
||||
formatted = []
|
||||
for msg in messages:
|
||||
sender = msg.get("sender", {})
|
||||
ts = msg.get("time", 0)
|
||||
time_str = datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
|
||||
formatted.append({
|
||||
"time": time_str,
|
||||
"sender_id": sender.get("user_id"),
|
||||
"sender_name": sender.get("nickname", ""),
|
||||
"sender_card": sender.get("card", ""),
|
||||
"message": msg.get("raw_message", ""),
|
||||
"message_type": msg.get("message_type", ""),
|
||||
})
|
||||
return formatted
|
||||
|
||||
|
||||
def print_readable(messages: list[dict]):
|
||||
"""可读格式输出到终端"""
|
||||
for m in messages:
|
||||
name = m["sender_card"] or m["sender_name"]
|
||||
print(f"[{m['time']}] {name}({m['sender_id']}): {m['message']}")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="获取 QQ 历史消息")
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("--gid", help="群号")
|
||||
group.add_argument("--uid", help="QQ号(私聊历史)")
|
||||
parser.add_argument("--num", type=int, default=10, help="拉取消息数量")
|
||||
parser.add_argument("--json", action="store_true", help="JSON 格式输出")
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
if args.gid:
|
||||
messages = get_group_msg_history(args.gid, args.num)
|
||||
else:
|
||||
messages = get_private_msg_history(args.uid, args.num)
|
||||
|
||||
formatted = format_messages(messages)
|
||||
|
||||
if args.json:
|
||||
print(json.dumps(formatted, ensure_ascii=False, indent=2))
|
||||
else:
|
||||
print_readable(formatted)
|
||||
|
||||
except requests.exceptions.ConnectionError:
|
||||
print("错误:无法连接到 NapCat,检查 CQHTTP_URL 是否正确")
|
||||
sys.exit(1)
|
||||
except Exception as e:
|
||||
print(f"错误:{e}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user