Files
chatrebot_aireply_plug/scripts/qq_get_groups.py
root 9dff73e7bc fix: 通过本地完整测试发现的 11 个 bug
🔴 致命(5个):
- Bug1: onebot11.json 格式错误(旧 {http:{},ws:{}} → network.httpServers/httppClients)
- Bug2: 端口映射错位(WebUI 6099, OneBot API 6097 独立端口)
- Bug3: 变量名含空格 NAP CAT_HOST → NAPCAT_HOST(14脚本+2SKILL+README)
- Bug4: heredoc 单引号阻止变量展开
- Bug5: process.py 缺失 _get_high_risk_words() 方法

🟡 重要(3个):
- Bug6: onebot11.json 默认不存在(cat > 而非 edit)
- Bug7: API 须登录后才响应(新增扫码引导步骤)
- Bug8: qqrebot systemd 依赖 napcat 服务

🟢 中等(3个):
- Bug9: Skills 占位符缺 YOUR_NAPCAT_HOST sed 覆盖
- Bug10: venv 不可用需降级方案
- Bug11: 端口 25580 冲突处理
已验证全链路:NapCat + qqrebot + 插件 + OpenClaw Gateway + AI 回复
2026-05-04 19:47:26 +08:00

86 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
获取群聊列表 - 通过 NapCat (OneBot) HTTP API
用法:
python3 qq_get_groups.py # 输出格式化群列表
python3 qq_get_groups.py --json # 输出 JSON供 agent 使用)
python3 qq_get_groups.py --keyword 我的世界 # 搜索群名包含"我的世界"的群
"""
import json
import sys
import argparse
import requests
CQHTTP_URL = "http://YOUR_NAPCAT_HOST:25570"
def get_group_list():
"""获取群聊列表,返回群字典列表"""
url = f"{CQHTTP_URL}/get_group_list"
resp = requests.post(url, json={}, timeout=10)
data = resp.json()
if data.get("status") == "ok":
return data.get("data", [])
raise Exception(f"API 返回异常: {json.dumps(data, ensure_ascii=False)}")
def format_groups(groups, keyword=None):
"""格式化为可读文本"""
if keyword:
keyword = keyword.lower()
filtered = []
for g in groups:
gid = str(g.get("group_id", ""))
name = g.get("group_name", "")
if keyword in gid or keyword in name.lower():
filtered.append(g)
groups = filtered
if not groups:
return "暂无群聊数据" if not keyword else f"未找到包含「{keyword}」的群"
lines = [f"{len(groups)} 个群\n"]
for g in groups:
gid = g.get("group_id", "?")
name = g.get("group_name", "?")
member_count = g.get("member_count", "?")
max_member = g.get("max_member_count", "?")
lines.append(f"{name} ({gid}) — {member_count}/{max_member}")
return "\n".join(lines)
def main():
parser = argparse.ArgumentParser(description="获取 QQ 群聊列表")
parser.add_argument("--json", action="store_true", help="输出 JSON 格式")
parser.add_argument("--keyword", type=str, help="按群名关键词筛选")
args = parser.parse_args()
try:
groups = get_group_list()
if args.keyword:
kw = args.keyword.lower()
groups = [
g for g in groups
if kw in str(g.get("group_id", ""))
or kw in g.get("group_name", "").lower()
]
if args.json:
print(json.dumps(groups, ensure_ascii=False, indent=2))
else:
print(format_groups(groups, args.keyword))
except requests.exceptions.ConnectionError:
print(f"❌ 连接失败: 无法连接到 NapCat ({CQHTTP_URL})")
sys.exit(1)
except Exception as e:
print(f"❌ 获取群聊列表失败: {e}")
sys.exit(1)
if __name__ == "__main__":
main()