@cxyhhhhh/openclaw-qqbot 1.6.7-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +470 -0
- package/README.zh.md +465 -0
- package/bin/qqbot-cli.js +243 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +26 -0
- package/dist/src/admin-resolver.d.ts +33 -0
- package/dist/src/admin-resolver.js +157 -0
- package/dist/src/api.d.ts +264 -0
- package/dist/src/api.js +777 -0
- package/dist/src/channel.d.ts +29 -0
- package/dist/src/channel.js +452 -0
- package/dist/src/config.d.ts +56 -0
- package/dist/src/config.js +278 -0
- package/dist/src/credential-backup.d.ts +31 -0
- package/dist/src/credential-backup.js +66 -0
- package/dist/src/deliver-debounce.d.ts +74 -0
- package/dist/src/deliver-debounce.js +174 -0
- package/dist/src/gateway.d.ts +18 -0
- package/dist/src/gateway.js +2021 -0
- package/dist/src/group-history.d.ts +136 -0
- package/dist/src/group-history.js +226 -0
- package/dist/src/image-server.d.ts +87 -0
- package/dist/src/image-server.js +570 -0
- package/dist/src/inbound-attachments.d.ts +60 -0
- package/dist/src/inbound-attachments.js +248 -0
- package/dist/src/known-users.d.ts +100 -0
- package/dist/src/known-users.js +263 -0
- package/dist/src/message-gating.d.ts +53 -0
- package/dist/src/message-gating.js +107 -0
- package/dist/src/message-queue.d.ts +86 -0
- package/dist/src/message-queue.js +257 -0
- package/dist/src/onboarding.d.ts +10 -0
- package/dist/src/onboarding.js +203 -0
- package/dist/src/outbound-deliver.d.ts +48 -0
- package/dist/src/outbound-deliver.js +392 -0
- package/dist/src/outbound.d.ts +205 -0
- package/dist/src/outbound.js +926 -0
- package/dist/src/proactive.d.ts +170 -0
- package/dist/src/proactive.js +399 -0
- package/dist/src/ref-index-store.d.ts +70 -0
- package/dist/src/ref-index-store.js +250 -0
- package/dist/src/reply-dispatcher.d.ts +35 -0
- package/dist/src/reply-dispatcher.js +311 -0
- package/dist/src/request-context.d.ts +18 -0
- package/dist/src/request-context.js +30 -0
- package/dist/src/runtime.d.ts +3 -0
- package/dist/src/runtime.js +10 -0
- package/dist/src/session-store.d.ts +52 -0
- package/dist/src/session-store.js +254 -0
- package/dist/src/slash-commands.d.ts +77 -0
- package/dist/src/slash-commands.js +1461 -0
- package/dist/src/startup-greeting.d.ts +30 -0
- package/dist/src/startup-greeting.js +97 -0
- package/dist/src/streaming.d.ts +250 -0
- package/dist/src/streaming.js +914 -0
- package/dist/src/stt.d.ts +21 -0
- package/dist/src/stt.js +70 -0
- package/dist/src/tools/channel.d.ts +16 -0
- package/dist/src/tools/channel.js +234 -0
- package/dist/src/tools/remind.d.ts +2 -0
- package/dist/src/tools/remind.js +248 -0
- package/dist/src/types.d.ts +364 -0
- package/dist/src/types.js +17 -0
- package/dist/src/typing-keepalive.d.ts +27 -0
- package/dist/src/typing-keepalive.js +64 -0
- package/dist/src/update-checker.d.ts +34 -0
- package/dist/src/update-checker.js +160 -0
- package/dist/src/utils/audio-convert.d.ts +98 -0
- package/dist/src/utils/audio-convert.js +755 -0
- package/dist/src/utils/chunked-upload.d.ts +59 -0
- package/dist/src/utils/chunked-upload.js +289 -0
- package/dist/src/utils/file-utils.d.ts +61 -0
- package/dist/src/utils/file-utils.js +172 -0
- package/dist/src/utils/image-size.d.ts +51 -0
- package/dist/src/utils/image-size.js +234 -0
- package/dist/src/utils/media-send.d.ts +148 -0
- package/dist/src/utils/media-send.js +456 -0
- package/dist/src/utils/media-tags.d.ts +14 -0
- package/dist/src/utils/media-tags.js +164 -0
- package/dist/src/utils/payload.d.ts +112 -0
- package/dist/src/utils/payload.js +186 -0
- package/dist/src/utils/pkg-version.d.ts +5 -0
- package/dist/src/utils/pkg-version.js +51 -0
- package/dist/src/utils/platform.d.ts +137 -0
- package/dist/src/utils/platform.js +390 -0
- package/dist/src/utils/ssrf-guard.d.ts +25 -0
- package/dist/src/utils/ssrf-guard.js +91 -0
- package/dist/src/utils/text-parsing.d.ts +32 -0
- package/dist/src/utils/text-parsing.js +69 -0
- package/dist/src/utils/upload-cache.d.ts +34 -0
- package/dist/src/utils/upload-cache.js +93 -0
- package/index.ts +31 -0
- package/node_modules/@eshaz/web-worker/LICENSE +201 -0
- package/node_modules/@eshaz/web-worker/README.md +134 -0
- package/node_modules/@eshaz/web-worker/browser.js +17 -0
- package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
- package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
- package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
- package/node_modules/@eshaz/web-worker/node.js +223 -0
- package/node_modules/@eshaz/web-worker/package.json +54 -0
- package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
- package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
- package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
- package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
- package/node_modules/mpg123-decoder/README.md +265 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
- package/node_modules/mpg123-decoder/index.js +8 -0
- package/node_modules/mpg123-decoder/package.json +58 -0
- package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
- package/node_modules/mpg123-decoder/types.d.ts +30 -0
- package/node_modules/silk-wasm/LICENSE +21 -0
- package/node_modules/silk-wasm/README.md +85 -0
- package/node_modules/silk-wasm/lib/index.cjs +16 -0
- package/node_modules/silk-wasm/lib/index.d.ts +70 -0
- package/node_modules/silk-wasm/lib/index.mjs +16 -0
- package/node_modules/silk-wasm/lib/silk.wasm +0 -0
- package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
- package/node_modules/silk-wasm/package.json +39 -0
- package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
- package/node_modules/simple-yenc/.prettierignore +1 -0
- package/node_modules/simple-yenc/LICENSE +7 -0
- package/node_modules/simple-yenc/README.md +163 -0
- package/node_modules/simple-yenc/dist/esm.js +1 -0
- package/node_modules/simple-yenc/dist/index.js +1 -0
- package/node_modules/simple-yenc/package.json +50 -0
- package/node_modules/simple-yenc/rollup.config.js +27 -0
- package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +13 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/node_modules/ws/lib/receiver.js +706 -0
- package/node_modules/ws/lib/sender.js +602 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +554 -0
- package/node_modules/ws/lib/websocket.js +1393 -0
- package/node_modules/ws/package.json +69 -0
- package/node_modules/ws/wrapper.mjs +8 -0
- package/openclaw.plugin.json +17 -0
- package/package.json +67 -0
- package/preload.cjs +33 -0
- package/scripts/cleanup-legacy-plugins.sh +124 -0
- package/scripts/link-sdk-core.cjs +185 -0
- package/scripts/postinstall-link-sdk.js +113 -0
- package/scripts/proactive-api-server.ts +369 -0
- package/scripts/send-proactive.ts +293 -0
- package/scripts/set-markdown.sh +156 -0
- package/scripts/test-sendmedia.ts +116 -0
- package/scripts/upgrade-via-npm.ps1 +451 -0
- package/scripts/upgrade-via-npm.sh +528 -0
- package/scripts/upgrade-via-source.sh +916 -0
- package/skills/qqbot-channel/SKILL.md +263 -0
- package/skills/qqbot-channel/references/api_references.md +521 -0
- package/skills/qqbot-media/SKILL.md +60 -0
- package/skills/qqbot-remind/SKILL.md +149 -0
- package/src/admin-resolver.ts +181 -0
- package/src/api.ts +1138 -0
- package/src/channel.ts +477 -0
- package/src/config.ts +347 -0
- package/src/credential-backup.ts +72 -0
- package/src/deliver-debounce.ts +229 -0
- package/src/gateway.ts +2257 -0
- package/src/group-history.ts +328 -0
- package/src/image-server.ts +675 -0
- package/src/inbound-attachments.ts +321 -0
- package/src/known-users.ts +353 -0
- package/src/message-gating.ts +190 -0
- package/src/message-queue.ts +349 -0
- package/src/onboarding.ts +274 -0
- package/src/openclaw-plugin-sdk.d.ts +587 -0
- package/src/outbound-deliver.ts +473 -0
- package/src/outbound.ts +1119 -0
- package/src/proactive.ts +530 -0
- package/src/ref-index-store.ts +335 -0
- package/src/reply-dispatcher.ts +334 -0
- package/src/request-context.ts +39 -0
- package/src/runtime.ts +14 -0
- package/src/session-store.ts +303 -0
- package/src/slash-commands.ts +1615 -0
- package/src/startup-greeting.ts +120 -0
- package/src/streaming.ts +1102 -0
- package/src/stt.ts +86 -0
- package/src/tools/channel.ts +281 -0
- package/src/tools/remind.ts +300 -0
- package/src/types.ts +386 -0
- package/src/typing-keepalive.ts +59 -0
- package/src/update-checker.ts +174 -0
- package/src/utils/audio-convert.ts +859 -0
- package/src/utils/chunked-upload.ts +419 -0
- package/src/utils/file-utils.ts +193 -0
- package/src/utils/image-size.ts +266 -0
- package/src/utils/media-send.ts +585 -0
- package/src/utils/media-tags.ts +182 -0
- package/src/utils/payload.ts +265 -0
- package/src/utils/pkg-version.ts +54 -0
- package/src/utils/platform.ts +435 -0
- package/src/utils/ssrf-guard.ts +102 -0
- package/src/utils/text-parsing.ts +75 -0
- package/src/utils/upload-cache.ts +128 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type ChannelPlugin } from "openclaw/plugin-sdk/core";
|
|
2
|
+
import type { ResolvedQQBotAccount } from "./types.js";
|
|
3
|
+
/** QQ Bot 单条消息文本长度上限 */
|
|
4
|
+
export declare const TEXT_CHUNK_LIMIT = 5000;
|
|
5
|
+
/**
|
|
6
|
+
* Markdown 感知的文本分块函数
|
|
7
|
+
* 委托给 SDK 内置的 channel.text.chunkMarkdownText
|
|
8
|
+
* 支持代码块自动关闭/重开、括号感知等
|
|
9
|
+
*/
|
|
10
|
+
export declare function chunkText(text: string, limit: number): string[];
|
|
11
|
+
export declare const qqbotPlugin: ChannelPlugin<ResolvedQQBotAccount>;
|
|
12
|
+
/** 清理 @mention:替换 <@openid> 为 @用户名,去除 @机器人自身 */
|
|
13
|
+
export declare function stripMentionText(text: string, mentions?: Array<{
|
|
14
|
+
member_openid?: string;
|
|
15
|
+
id?: string;
|
|
16
|
+
user_openid?: string;
|
|
17
|
+
is_you?: boolean;
|
|
18
|
+
nickname?: string;
|
|
19
|
+
username?: string;
|
|
20
|
+
}>): string;
|
|
21
|
+
/** 检测消息是否 @了机器人(mentions > eventType > mentionPatterns) */
|
|
22
|
+
export declare function detectWasMentioned({ eventType, mentions, content, mentionPatterns }: {
|
|
23
|
+
eventType?: string;
|
|
24
|
+
mentions?: Array<{
|
|
25
|
+
is_you?: boolean;
|
|
26
|
+
}>;
|
|
27
|
+
content?: string;
|
|
28
|
+
mentionPatterns?: string[];
|
|
29
|
+
}): boolean;
|
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
import { applyAccountNameToChannelSection, deleteAccountFromConfigSection, setAccountEnabledInConfigSection, } from "openclaw/plugin-sdk/core";
|
|
2
|
+
import { DEFAULT_ACCOUNT_ID, listQQBotAccountIds, resolveQQBotAccount, applyQQBotAccountConfig, resolveDefaultQQBotAccountId, resolveRequireMention, resolveToolPolicy, resolveGroupConfig } from "./config.js";
|
|
3
|
+
import { sendText, sendMedia } from "./outbound.js";
|
|
4
|
+
import { startGateway } from "./gateway.js";
|
|
5
|
+
import { qqbotOnboardingAdapter } from "./onboarding.js";
|
|
6
|
+
import { getQQBotRuntime } from "./runtime.js";
|
|
7
|
+
import { saveCredentialBackup, loadCredentialBackup } from "./credential-backup.js";
|
|
8
|
+
import { initApiConfig } from "./api.js";
|
|
9
|
+
/** QQ Bot 单条消息文本长度上限 */
|
|
10
|
+
export const TEXT_CHUNK_LIMIT = 5000;
|
|
11
|
+
/**
|
|
12
|
+
* Markdown 感知的文本分块函数
|
|
13
|
+
* 委托给 SDK 内置的 channel.text.chunkMarkdownText
|
|
14
|
+
* 支持代码块自动关闭/重开、括号感知等
|
|
15
|
+
*/
|
|
16
|
+
export function chunkText(text, limit) {
|
|
17
|
+
const runtime = getQQBotRuntime();
|
|
18
|
+
return runtime.channel.text.chunkMarkdownText(text, limit);
|
|
19
|
+
}
|
|
20
|
+
export const qqbotPlugin = {
|
|
21
|
+
id: "qqbot",
|
|
22
|
+
meta: {
|
|
23
|
+
id: "qqbot",
|
|
24
|
+
label: "QQ Bot",
|
|
25
|
+
selectionLabel: "QQ Bot",
|
|
26
|
+
docsPath: "/docs/channels/qqbot",
|
|
27
|
+
blurb: "Connect to QQ via official QQ Bot API",
|
|
28
|
+
order: 50,
|
|
29
|
+
},
|
|
30
|
+
capabilities: {
|
|
31
|
+
chatTypes: ["direct", "group"],
|
|
32
|
+
media: true,
|
|
33
|
+
reactions: false,
|
|
34
|
+
threads: false,
|
|
35
|
+
/**
|
|
36
|
+
* blockStreaming: true 表示该 Channel 支持块流式
|
|
37
|
+
* 框架会收集流式响应,然后通过 deliver 回调发送
|
|
38
|
+
*/
|
|
39
|
+
blockStreaming: true,
|
|
40
|
+
},
|
|
41
|
+
reload: { configPrefixes: ["channels.qqbot"] },
|
|
42
|
+
// ============ 群消息策略适配器 ============
|
|
43
|
+
groups: {
|
|
44
|
+
/** 是否需要 @机器人才响应 */
|
|
45
|
+
resolveRequireMention: ({ cfg, accountId, groupId }) => {
|
|
46
|
+
if (!groupId)
|
|
47
|
+
return undefined;
|
|
48
|
+
return resolveRequireMention(cfg, groupId, accountId ?? undefined);
|
|
49
|
+
},
|
|
50
|
+
/** 群聊工具范围 */
|
|
51
|
+
resolveToolPolicy: ({ cfg, accountId, groupId }) => {
|
|
52
|
+
if (!groupId)
|
|
53
|
+
return undefined;
|
|
54
|
+
const policy = resolveToolPolicy(cfg, groupId, accountId ?? undefined);
|
|
55
|
+
// 将简单字符串策略映射为 GroupToolPolicyConfig 对象
|
|
56
|
+
if (policy === "full")
|
|
57
|
+
return undefined; // full = 默认不限制
|
|
58
|
+
if (policy === "none")
|
|
59
|
+
return { allow: [], deny: ["*"] };
|
|
60
|
+
// restricted: 默认空 allow(框架会使用内置 restricted 列表)
|
|
61
|
+
return { allow: [] };
|
|
62
|
+
},
|
|
63
|
+
/** QQ Bot 平台特有的群聊行为提示 */
|
|
64
|
+
resolveGroupIntroHint: ({ cfg, accountId, groupId }) => {
|
|
65
|
+
if (!groupId)
|
|
66
|
+
return undefined;
|
|
67
|
+
const groupCfg = resolveGroupConfig(cfg, groupId, accountId ?? undefined);
|
|
68
|
+
const hints = [];
|
|
69
|
+
if (groupCfg.name) {
|
|
70
|
+
hints.push(`当前群: ${groupCfg.name}`);
|
|
71
|
+
}
|
|
72
|
+
// bot 互聊防护、@状态行为指引在 gateway.ts 动态注入
|
|
73
|
+
return hints.join(" ") || undefined;
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
// ============ @mention 检测与清理 ============
|
|
77
|
+
mentions: {
|
|
78
|
+
/** 清理 @mention 文本(SDK ChannelMentionAdapter 接口) */
|
|
79
|
+
stripMentions: ({ text, ctx }) => {
|
|
80
|
+
const mentions = ctx?.mentions;
|
|
81
|
+
return stripMentionText(text, mentions);
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
// CLI onboarding wizard
|
|
85
|
+
// @ts-ignore onboarding removed from ChannelPlugin type in 2026.3.23 but still supported at runtime
|
|
86
|
+
onboarding: qqbotOnboardingAdapter,
|
|
87
|
+
config: {
|
|
88
|
+
listAccountIds: (cfg) => listQQBotAccountIds(cfg),
|
|
89
|
+
resolveAccount: (cfg, accountId) => resolveQQBotAccount(cfg, accountId),
|
|
90
|
+
defaultAccountId: (cfg) => resolveDefaultQQBotAccountId(cfg),
|
|
91
|
+
// 新增:设置账户启用状态
|
|
92
|
+
setAccountEnabled: ({ cfg, accountId, enabled }) => setAccountEnabledInConfigSection({
|
|
93
|
+
cfg,
|
|
94
|
+
sectionKey: "qqbot",
|
|
95
|
+
accountId,
|
|
96
|
+
enabled,
|
|
97
|
+
allowTopLevel: true,
|
|
98
|
+
}),
|
|
99
|
+
// 新增:删除账户
|
|
100
|
+
deleteAccount: ({ cfg, accountId }) => deleteAccountFromConfigSection({
|
|
101
|
+
cfg,
|
|
102
|
+
sectionKey: "qqbot",
|
|
103
|
+
accountId,
|
|
104
|
+
clearBaseFields: ["appId", "clientSecret", "clientSecretFile", "name"],
|
|
105
|
+
}),
|
|
106
|
+
isConfigured: (account) => {
|
|
107
|
+
if (account?.appId && account?.clientSecret)
|
|
108
|
+
return true;
|
|
109
|
+
// 配置为空但有凭证备份时仍返回 true,让 startAccount 有机会恢复凭证
|
|
110
|
+
const backup = loadCredentialBackup(account?.accountId);
|
|
111
|
+
return backup !== null;
|
|
112
|
+
},
|
|
113
|
+
describeAccount: (account) => ({
|
|
114
|
+
accountId: account?.accountId ?? DEFAULT_ACCOUNT_ID,
|
|
115
|
+
name: account?.name,
|
|
116
|
+
enabled: account?.enabled ?? false,
|
|
117
|
+
configured: Boolean(account?.appId && account?.clientSecret),
|
|
118
|
+
tokenSource: account?.secretSource,
|
|
119
|
+
}),
|
|
120
|
+
// 关键:解析 allowFrom 配置,用于命令授权
|
|
121
|
+
resolveAllowFrom: ({ cfg, accountId }) => {
|
|
122
|
+
const account = resolveQQBotAccount(cfg, accountId ?? undefined);
|
|
123
|
+
const allowFrom = account.config?.allowFrom ?? [];
|
|
124
|
+
console.log(`[qqbot] resolveAllowFrom: accountId=${accountId}, allowFrom=${JSON.stringify(allowFrom)}`);
|
|
125
|
+
return allowFrom.map((entry) => String(entry));
|
|
126
|
+
},
|
|
127
|
+
// 格式化 allowFrom 条目(移除 qqbot: 前缀,统一大写)
|
|
128
|
+
formatAllowFrom: ({ allowFrom }) => allowFrom
|
|
129
|
+
.map((entry) => String(entry).trim())
|
|
130
|
+
.filter(Boolean)
|
|
131
|
+
.map((entry) => entry.replace(/^qqbot:/i, ""))
|
|
132
|
+
.map((entry) => entry.toUpperCase()), // QQ openid 是大写的
|
|
133
|
+
},
|
|
134
|
+
setup: {
|
|
135
|
+
// 新增:规范化账户 ID
|
|
136
|
+
resolveAccountId: ({ accountId }) => accountId?.trim().toLowerCase() || DEFAULT_ACCOUNT_ID,
|
|
137
|
+
// 新增:应用账户名称
|
|
138
|
+
applyAccountName: ({ cfg, accountId, name }) => applyAccountNameToChannelSection({
|
|
139
|
+
cfg,
|
|
140
|
+
channelKey: "qqbot",
|
|
141
|
+
accountId,
|
|
142
|
+
name,
|
|
143
|
+
}),
|
|
144
|
+
validateInput: ({ input }) => {
|
|
145
|
+
if (!input.token && !input.tokenFile && !input.useEnv) {
|
|
146
|
+
return "QQBot requires --token (format: appId:clientSecret) or --use-env";
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
},
|
|
150
|
+
applyAccountConfig: ({ cfg, accountId, input }) => {
|
|
151
|
+
let appId = "";
|
|
152
|
+
let clientSecret = "";
|
|
153
|
+
if (input.token) {
|
|
154
|
+
const parts = input.token.split(":");
|
|
155
|
+
if (parts.length === 2) {
|
|
156
|
+
appId = parts[0];
|
|
157
|
+
clientSecret = parts[1];
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return applyQQBotAccountConfig(cfg, accountId, {
|
|
161
|
+
appId,
|
|
162
|
+
clientSecret,
|
|
163
|
+
clientSecretFile: input.tokenFile,
|
|
164
|
+
name: input.name,
|
|
165
|
+
imageServerBaseUrl: input.imageServerBaseUrl,
|
|
166
|
+
});
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
// Messaging 配置:用于解析目标地址
|
|
170
|
+
messaging: {
|
|
171
|
+
/**
|
|
172
|
+
* 规范化目标地址
|
|
173
|
+
* 支持以下格式:
|
|
174
|
+
* - qqbot:c2c:openid -> 私聊
|
|
175
|
+
* - qqbot:group:groupid -> 群聊
|
|
176
|
+
* - qqbot:channel:channelid -> 频道
|
|
177
|
+
* - c2c:openid -> 私聊
|
|
178
|
+
* - group:groupid -> 群聊
|
|
179
|
+
* - channel:channelid -> 频道
|
|
180
|
+
* - 纯 openid(32位十六进制)-> 私聊
|
|
181
|
+
*/
|
|
182
|
+
normalizeTarget: (target) => {
|
|
183
|
+
// 去掉 qqbot: 前缀(如果有)
|
|
184
|
+
const id = target.replace(/^qqbot:/i, "");
|
|
185
|
+
// 检查是否是已知格式
|
|
186
|
+
if (id.startsWith("c2c:") || id.startsWith("group:") || id.startsWith("channel:")) {
|
|
187
|
+
return `qqbot:${id}`;
|
|
188
|
+
}
|
|
189
|
+
// 检查是否是纯 openid(32位十六进制,不带连字符)
|
|
190
|
+
// QQ Bot OpenID 格式类似: 207A5B8339D01F6582911C014668B77B
|
|
191
|
+
const openIdHexPattern = /^[0-9a-fA-F]{32}$/;
|
|
192
|
+
if (openIdHexPattern.test(id)) {
|
|
193
|
+
return `qqbot:c2c:${id}`;
|
|
194
|
+
}
|
|
195
|
+
// 检查是否是 UUID 格式的 openid(带连字符)
|
|
196
|
+
const openIdUuidPattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
197
|
+
if (openIdUuidPattern.test(id)) {
|
|
198
|
+
return `qqbot:c2c:${id}`;
|
|
199
|
+
}
|
|
200
|
+
// 不认识的格式,返回 undefined 让核心使用原始值
|
|
201
|
+
return undefined;
|
|
202
|
+
},
|
|
203
|
+
/**
|
|
204
|
+
* 目标解析器配置
|
|
205
|
+
* 用于判断一个目标 ID 是否看起来像 QQ Bot 的格式
|
|
206
|
+
*/
|
|
207
|
+
targetResolver: {
|
|
208
|
+
/**
|
|
209
|
+
* 判断目标 ID 是否可能是 QQ Bot 格式
|
|
210
|
+
* 支持以下格式:
|
|
211
|
+
* - qqbot:c2c:xxx
|
|
212
|
+
* - qqbot:group:xxx
|
|
213
|
+
* - qqbot:channel:xxx
|
|
214
|
+
* - c2c:xxx
|
|
215
|
+
* - group:xxx
|
|
216
|
+
* - channel:xxx
|
|
217
|
+
* - UUID 格式的 openid
|
|
218
|
+
*/
|
|
219
|
+
looksLikeId: (id) => {
|
|
220
|
+
// 带 qqbot: 前缀的格式
|
|
221
|
+
if (/^qqbot:(c2c|group|channel):/i.test(id)) {
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
// 不带前缀但有类型标识
|
|
225
|
+
if (/^(c2c|group|channel):/i.test(id)) {
|
|
226
|
+
return true;
|
|
227
|
+
}
|
|
228
|
+
// 32位十六进制 openid(不带连字符)
|
|
229
|
+
if (/^[0-9a-fA-F]{32}$/.test(id)) {
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
// UUID 格式的 openid(带连字符)
|
|
233
|
+
const openIdPattern = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
|
|
234
|
+
return openIdPattern.test(id);
|
|
235
|
+
},
|
|
236
|
+
hint: "QQ Bot 目标格式: qqbot:c2c:openid (私聊) 或 qqbot:group:groupid (群聊)",
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
outbound: {
|
|
240
|
+
deliveryMode: "direct",
|
|
241
|
+
chunker: (text, limit) => getQQBotRuntime().channel.text.chunkMarkdownText(text, limit),
|
|
242
|
+
chunkerMode: "markdown",
|
|
243
|
+
textChunkLimit: 5000,
|
|
244
|
+
sendText: async ({ to, text, accountId, replyToId, cfg }) => {
|
|
245
|
+
console.log(`[qqbot:channel] sendText called — accountId=${accountId}, to=${to}, replyToId=${replyToId}, text.length=${text?.length ?? 0}`);
|
|
246
|
+
console.log(`[qqbot:channel] sendText text preview: ${text?.slice(0, 100)}${(text?.length ?? 0) > 100 ? "..." : ""}`);
|
|
247
|
+
const account = resolveQQBotAccount(cfg, accountId ?? undefined);
|
|
248
|
+
initApiConfig({ markdownSupport: account.markdownSupport });
|
|
249
|
+
console.log(`[qqbot:channel] sendText resolved account: id=${account.accountId}, appId=${account.appId}, enabled=${account.enabled}`);
|
|
250
|
+
const result = await sendText({ to, text, accountId, replyToId, account });
|
|
251
|
+
console.log(`[qqbot:channel] sendText result: messageId=${result.messageId}, error=${result.error ?? "none"}`);
|
|
252
|
+
if (result.error)
|
|
253
|
+
throw new Error(result.error);
|
|
254
|
+
return {
|
|
255
|
+
channel: "qqbot",
|
|
256
|
+
messageId: result.messageId ?? "",
|
|
257
|
+
};
|
|
258
|
+
},
|
|
259
|
+
sendMedia: async ({ to, text, mediaUrl, accountId, replyToId, cfg }) => {
|
|
260
|
+
console.log(`[qqbot:channel] sendMedia called — accountId=${accountId}, to=${to}, replyToId=${replyToId}, mediaUrl=${mediaUrl?.slice(0, 80)}, text.length=${text?.length ?? 0}`);
|
|
261
|
+
const account = resolveQQBotAccount(cfg, accountId ?? undefined);
|
|
262
|
+
initApiConfig({ markdownSupport: account.markdownSupport });
|
|
263
|
+
console.log(`[qqbot:channel] sendMedia resolved account: id=${account.accountId}, appId=${account.appId}, enabled=${account.enabled}`);
|
|
264
|
+
const result = await sendMedia({ to, text: text ?? "", mediaUrl: mediaUrl ?? "", accountId, replyToId, account });
|
|
265
|
+
console.log(`[qqbot:channel] sendMedia result: messageId=${result.messageId}, error=${result.error ?? "none"}`);
|
|
266
|
+
// 此 sendMedia 是框架 Channel Plugin 的标准出站接口,
|
|
267
|
+
// 用于非 gateway deliver 场景(如 API 直接发送、cron 等)。
|
|
268
|
+
// gateway 消息响应走的是 deliver 回调 → sendPlainReply,不经过此处。
|
|
269
|
+
// 框架拿到 error 后不一定会给用户发文字兜底,所以这里主动发一条。
|
|
270
|
+
if (result.error) {
|
|
271
|
+
try {
|
|
272
|
+
const fallbackResult = await sendText({ to, text: result.error, accountId, replyToId, account });
|
|
273
|
+
console.log(`[qqbot:channel] sendMedia fallback text sent: messageId=${fallbackResult.messageId}, error=${fallbackResult.error ?? "none"}`);
|
|
274
|
+
}
|
|
275
|
+
catch (fallbackErr) {
|
|
276
|
+
console.error(`[qqbot:channel] sendMedia fallback text failed: ${fallbackErr}`);
|
|
277
|
+
}
|
|
278
|
+
throw new Error(result.error);
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
channel: "qqbot",
|
|
282
|
+
messageId: result.messageId ?? "",
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
gateway: {
|
|
287
|
+
startAccount: async (ctx) => {
|
|
288
|
+
let { account } = ctx;
|
|
289
|
+
const { abortSignal, log, cfg } = ctx;
|
|
290
|
+
// 凭证恢复:如果 appId/secret 为空(热更新打断可能导致配置丢失),尝试从暂存文件恢复
|
|
291
|
+
if (!account.appId || !account.clientSecret) {
|
|
292
|
+
const backup = loadCredentialBackup(account.accountId);
|
|
293
|
+
if (backup) {
|
|
294
|
+
log?.info(`[qqbot:${account.accountId}] 配置中凭证为空,从暂存文件恢复 (appId=${backup.appId}, savedAt=${backup.savedAt})`);
|
|
295
|
+
try {
|
|
296
|
+
const runtime = getQQBotRuntime();
|
|
297
|
+
const restoredCfg = applyQQBotAccountConfig(cfg, account.accountId, {
|
|
298
|
+
appId: backup.appId,
|
|
299
|
+
clientSecret: backup.clientSecret,
|
|
300
|
+
});
|
|
301
|
+
const configApi = runtime.config;
|
|
302
|
+
await configApi.writeConfigFile(restoredCfg);
|
|
303
|
+
// 重新解析 account 以获取恢复后的值
|
|
304
|
+
account = resolveQQBotAccount(restoredCfg, account.accountId);
|
|
305
|
+
log?.info(`[qqbot:${account.accountId}] 凭证已恢复`);
|
|
306
|
+
}
|
|
307
|
+
catch (e) {
|
|
308
|
+
log?.error(`[qqbot:${account.accountId}] 凭证恢复失败: ${e}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
log?.info(`[qqbot:${account.accountId}] Starting gateway — appId=${account.appId}, enabled=${account.enabled}, name=${account.name ?? "unnamed"}`);
|
|
313
|
+
console.log(`[qqbot:channel] startAccount: accountId=${account.accountId}, appId=${account.appId}, secretSource=${account.secretSource}`);
|
|
314
|
+
await startGateway({
|
|
315
|
+
account,
|
|
316
|
+
abortSignal,
|
|
317
|
+
cfg,
|
|
318
|
+
log,
|
|
319
|
+
onReady: () => {
|
|
320
|
+
log?.info(`[qqbot:${account.accountId}] Gateway ready`);
|
|
321
|
+
// 启动成功,保存凭证快照供后续恢复使用
|
|
322
|
+
saveCredentialBackup(account.accountId, account.appId, account.clientSecret);
|
|
323
|
+
ctx.setStatus({
|
|
324
|
+
...ctx.getStatus(),
|
|
325
|
+
running: true,
|
|
326
|
+
connected: true,
|
|
327
|
+
lastConnectedAt: Date.now(),
|
|
328
|
+
});
|
|
329
|
+
},
|
|
330
|
+
onError: (error) => {
|
|
331
|
+
log?.error(`[qqbot:${account.accountId}] Gateway error: ${error.message}`);
|
|
332
|
+
ctx.setStatus({
|
|
333
|
+
...ctx.getStatus(),
|
|
334
|
+
lastError: error.message,
|
|
335
|
+
});
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
},
|
|
339
|
+
// 新增:登出账户(清除配置中的凭证)
|
|
340
|
+
logoutAccount: async ({ accountId, cfg }) => {
|
|
341
|
+
const nextCfg = { ...cfg };
|
|
342
|
+
const nextQQBot = cfg.channels?.qqbot ? { ...cfg.channels.qqbot } : undefined;
|
|
343
|
+
let cleared = false;
|
|
344
|
+
let changed = false;
|
|
345
|
+
if (nextQQBot) {
|
|
346
|
+
const qqbot = nextQQBot;
|
|
347
|
+
if (accountId === DEFAULT_ACCOUNT_ID && qqbot.clientSecret) {
|
|
348
|
+
delete qqbot.clientSecret;
|
|
349
|
+
cleared = true;
|
|
350
|
+
changed = true;
|
|
351
|
+
}
|
|
352
|
+
const accounts = qqbot.accounts;
|
|
353
|
+
if (accounts && accountId in accounts) {
|
|
354
|
+
const entry = accounts[accountId];
|
|
355
|
+
if (entry && "clientSecret" in entry) {
|
|
356
|
+
delete entry.clientSecret;
|
|
357
|
+
cleared = true;
|
|
358
|
+
changed = true;
|
|
359
|
+
}
|
|
360
|
+
if (entry && Object.keys(entry).length === 0) {
|
|
361
|
+
delete accounts[accountId];
|
|
362
|
+
changed = true;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
if (changed && nextQQBot) {
|
|
367
|
+
nextCfg.channels = { ...nextCfg.channels, qqbot: nextQQBot };
|
|
368
|
+
const runtime = getQQBotRuntime();
|
|
369
|
+
const configApi = runtime.config;
|
|
370
|
+
await configApi.writeConfigFile(nextCfg);
|
|
371
|
+
}
|
|
372
|
+
const resolved = resolveQQBotAccount(changed ? nextCfg : cfg, accountId);
|
|
373
|
+
const loggedOut = resolved.secretSource === "none";
|
|
374
|
+
const envToken = Boolean(process.env.QQBOT_CLIENT_SECRET);
|
|
375
|
+
return { ok: true, cleared, envToken, loggedOut };
|
|
376
|
+
},
|
|
377
|
+
},
|
|
378
|
+
status: {
|
|
379
|
+
defaultRuntime: {
|
|
380
|
+
accountId: DEFAULT_ACCOUNT_ID,
|
|
381
|
+
running: false,
|
|
382
|
+
connected: false,
|
|
383
|
+
lastConnectedAt: null,
|
|
384
|
+
lastError: null,
|
|
385
|
+
lastInboundAt: null,
|
|
386
|
+
lastOutboundAt: null,
|
|
387
|
+
},
|
|
388
|
+
// 新增:构建通道摘要
|
|
389
|
+
buildChannelSummary: ({ snapshot }) => ({
|
|
390
|
+
configured: snapshot.configured ?? false,
|
|
391
|
+
tokenSource: snapshot.tokenSource ?? "none",
|
|
392
|
+
running: snapshot.running ?? false,
|
|
393
|
+
connected: snapshot.connected ?? false,
|
|
394
|
+
lastConnectedAt: snapshot.lastConnectedAt ?? null,
|
|
395
|
+
lastError: snapshot.lastError ?? null,
|
|
396
|
+
}),
|
|
397
|
+
buildAccountSnapshot: ({ account, runtime }) => ({
|
|
398
|
+
accountId: account?.accountId ?? DEFAULT_ACCOUNT_ID,
|
|
399
|
+
name: account?.name,
|
|
400
|
+
enabled: account?.enabled ?? false,
|
|
401
|
+
configured: Boolean(account?.appId && account?.clientSecret),
|
|
402
|
+
tokenSource: account?.secretSource,
|
|
403
|
+
running: Boolean(runtime?.running ?? false),
|
|
404
|
+
connected: Boolean(runtime?.connected ?? false),
|
|
405
|
+
lastConnectedAt: runtime?.lastConnectedAt ?? null,
|
|
406
|
+
lastError: runtime?.lastError ?? null,
|
|
407
|
+
lastInboundAt: runtime?.lastInboundAt ?? null,
|
|
408
|
+
lastOutboundAt: runtime?.lastOutboundAt ?? null,
|
|
409
|
+
}),
|
|
410
|
+
},
|
|
411
|
+
};
|
|
412
|
+
// ============ 独立的 mention 工具函数(供 gateway.ts 等直接调用) ============
|
|
413
|
+
/** 清理 @mention:替换 <@openid> 为 @用户名,去除 @机器人自身 */
|
|
414
|
+
export function stripMentionText(text, mentions) {
|
|
415
|
+
if (!text || !mentions?.length)
|
|
416
|
+
return text;
|
|
417
|
+
let cleaned = text;
|
|
418
|
+
for (const m of mentions) {
|
|
419
|
+
const openid = m.member_openid ?? m.id ?? m.user_openid;
|
|
420
|
+
if (!openid)
|
|
421
|
+
continue;
|
|
422
|
+
if (m.is_you) {
|
|
423
|
+
cleaned = cleaned.replace(new RegExp(`<@!?${openid}>`, "g"), "").trim();
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
const displayName = m.nickname ?? m.username;
|
|
427
|
+
if (displayName) {
|
|
428
|
+
cleaned = cleaned.replace(new RegExp(`<@!?${openid}>`, "g"), `@${displayName}`);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return cleaned;
|
|
433
|
+
}
|
|
434
|
+
/** 检测消息是否 @了机器人(mentions > eventType > mentionPatterns) */
|
|
435
|
+
export function detectWasMentioned({ eventType, mentions, content, mentionPatterns }) {
|
|
436
|
+
if (mentions?.some((m) => m.is_you))
|
|
437
|
+
return true;
|
|
438
|
+
if (eventType === "GROUP_AT_MESSAGE_CREATE")
|
|
439
|
+
return true;
|
|
440
|
+
if (mentionPatterns?.length && content) {
|
|
441
|
+
for (const pattern of mentionPatterns) {
|
|
442
|
+
try {
|
|
443
|
+
if (new RegExp(pattern, "i").test(content))
|
|
444
|
+
return true;
|
|
445
|
+
}
|
|
446
|
+
catch {
|
|
447
|
+
// 无效正则,跳过
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { ResolvedQQBotAccount, ToolPolicy, GroupConfig } from "./types.js";
|
|
2
|
+
import type { OpenClawConfig, GroupPolicy } from "openclaw/plugin-sdk";
|
|
3
|
+
/**
|
|
4
|
+
* 解析 mentionPatterns(agent → global → 空数组)
|
|
5
|
+
*
|
|
6
|
+
* 优先级:
|
|
7
|
+
* 1. agents.list[agentId].groupChat.mentionPatterns
|
|
8
|
+
* 2. messages.groupChat.mentionPatterns
|
|
9
|
+
* 3. []
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveMentionPatterns(cfg: OpenClawConfig, agentId?: string): string[];
|
|
12
|
+
export declare const DEFAULT_ACCOUNT_ID = "default";
|
|
13
|
+
/** 解析群消息策略 */
|
|
14
|
+
export declare function resolveGroupPolicy(cfg: OpenClawConfig, accountId?: string): GroupPolicy;
|
|
15
|
+
/** 解析群白名单(统一转大写) */
|
|
16
|
+
export declare function resolveGroupAllowFrom(cfg: OpenClawConfig, accountId?: string): string[];
|
|
17
|
+
/** 检查指定群是否被允许(使用标准策略引擎) */
|
|
18
|
+
export declare function isGroupAllowed(cfg: OpenClawConfig, groupOpenid: string, accountId?: string): boolean;
|
|
19
|
+
type ResolvedGroupConfig = Omit<Required<GroupConfig>, "prompt"> & Pick<GroupConfig, "prompt">;
|
|
20
|
+
/** 解析指定群配置(具体 groupOpenid > 通配符 "*" > 默认值) */
|
|
21
|
+
export declare function resolveGroupConfig(cfg: OpenClawConfig, groupOpenid: string, accountId?: string): ResolvedGroupConfig;
|
|
22
|
+
/** 解析群历史消息缓存条数 */
|
|
23
|
+
export declare function resolveHistoryLimit(cfg: OpenClawConfig, groupOpenid: string, accountId?: string): number;
|
|
24
|
+
/** 解析群行为 PE(具体群 > "*" > 默认值) */
|
|
25
|
+
export declare function resolveGroupPrompt(cfg: OpenClawConfig, groupOpenid: string, accountId?: string): string;
|
|
26
|
+
/** 解析群是否需要 @机器人才响应 */
|
|
27
|
+
export declare function resolveRequireMention(cfg: OpenClawConfig, groupOpenid: string, accountId?: string): boolean;
|
|
28
|
+
/** 解析群是否忽略 @了其他人(非 bot)的消息 */
|
|
29
|
+
export declare function resolveIgnoreOtherMentions(cfg: OpenClawConfig, groupOpenid: string, accountId?: string): boolean;
|
|
30
|
+
/** 解析群工具策略 */
|
|
31
|
+
export declare function resolveToolPolicy(cfg: OpenClawConfig, groupOpenid: string, accountId?: string): ToolPolicy;
|
|
32
|
+
/** 解析群名称(优先配置,fallback 为 openid 前 8 位) */
|
|
33
|
+
export declare function resolveGroupName(cfg: OpenClawConfig, groupOpenid: string, accountId?: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* 列出所有 QQBot 账户 ID
|
|
36
|
+
*/
|
|
37
|
+
export declare function listQQBotAccountIds(cfg: OpenClawConfig): string[];
|
|
38
|
+
/**
|
|
39
|
+
* 获取默认账户 ID
|
|
40
|
+
*/
|
|
41
|
+
export declare function resolveDefaultQQBotAccountId(cfg: OpenClawConfig): string;
|
|
42
|
+
/**
|
|
43
|
+
* 解析 QQBot 账户配置
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveQQBotAccount(cfg: OpenClawConfig, accountId?: string | null): ResolvedQQBotAccount;
|
|
46
|
+
/**
|
|
47
|
+
* 应用账户配置
|
|
48
|
+
*/
|
|
49
|
+
export declare function applyQQBotAccountConfig(cfg: OpenClawConfig, accountId: string, input: {
|
|
50
|
+
appId?: string;
|
|
51
|
+
clientSecret?: string;
|
|
52
|
+
clientSecretFile?: string;
|
|
53
|
+
name?: string;
|
|
54
|
+
imageServerBaseUrl?: string;
|
|
55
|
+
}): OpenClawConfig;
|
|
56
|
+
export {};
|