mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-21 13:44:03 +00:00
* feat: add QQ Bot channel extension * fix(qqbot): add setupWizard to runtime plugin for onboard re-entry * fix: fix review * fix: fix review * chore: sync lockfile and config-docs baseline for qqbot extension * refactor: 移除图床服务器相关代码 * fix * docs: 新增 QQ Bot 插件文档并修正链接路径 * refactor: remove credential backup functionality and update setup logic - Deleted the credential backup module to streamline the codebase. - Updated the setup surface to handle client secrets more robustly, allowing for configured secret inputs. - Simplified slash commands by removing unused hot upgrade compatibility checks and related functions. - Adjusted types to use SecretInput for client secrets in QQBot configuration. - Modified bundled plugin metadata to allow additional properties in the config schema. * feat: 添加本地媒体路径解析功能,修正 QQBot 媒体路径处理 * feat: 添加本地媒体路径解析功能,修正 QQBot 媒体路径处理 * feat: remove qqbot-media and qqbot-remind skills, add tests for config and setup - Deleted the qqbot-media and qqbot-remind skills documentation files. - Added unit tests for qqbot configuration and setup processes, ensuring proper handling of SecretRef-backed credentials and account configurations. - Implemented tests for local media path remapping, verifying correct resolution of media file paths. - Removed obsolete channel and remind tools, streamlining the codebase. * feat: 更新 QQBot 配置模式,添加音频格式和账户定义 * feat: 添加 QQBot 频道管理和定时提醒技能,更新媒体路径解析功能 * fix * feat: 添加 /bot-upgrade 指令以查看 QQBot 插件升级指引 * feat: update reminder and qq channel skills * feat: 更新remind工具投递目标地址格式 * feat: Refactor QQBot payload handling and improve code documentation - Simplified and clarified the structure of payload interfaces for Cron reminders and media messages. - Enhanced the parsing function to provide clearer error messages and improved validation. - Updated platform utility functions for better cross-platform compatibility and clearer documentation. - Improved text parsing utilities for better readability and consistency in emoji representation. - Optimized upload cache management with clearer comments and reduced redundancy. - Integrated QQBot plugin into the bundled channel plugins and updated metadata for installation. * OK apps/macos/Sources/OpenClaw/HostEnvSecurityPolicy.generated.swift > openclaw@2026.3.26 check:bundled-channel-config-metadata /Users/yuehuali/code/PR/openclaw > node --import tsx scripts/generate-bundled-channel-config-metadata.ts --check [bundled-channel-config-metadata] stale generated output at src/config/bundled-channel-config-metadata.generated.ts ELIFECYCLE Command failed with exit code 1. ELIFECYCLE Command failed with exit code 1. * feat: 添加 QQBot 渠道配置及相关账户设置 * fix(qqbot): resolve 14 high-priority bugs from PR #52986 review DM routing (7 fixes): - #1: DM slash-command replies use sendDmMessage(guildId) instead of sendC2CMessage(senderId) - #2: DM qualifiedTarget uses qqbot:dm:${guildId} instead of qqbot:c2c:${senderId} - #3: sendTextChunks adds DM branch - #4: sendMarkdownReply adds DM branch for text and Base64 images - #5: parseAndSendMediaTags maps DM to targetType:dm + guildId - #6: sendTextToTarget DM branch uses sendDmMessage; MessageTarget adds guildId field - #7: handleImage/Audio/Video/FilePayload add DM branches Other high-priority fixes: - #8: Fix sendC2CVoiceMessage/sendGroupVoiceMessage parameter misalignment - #9: broadcastMessage uses groupOpenid instead of member_openid for group users - #10: Unify KnownUser storage - proactive.ts delegates to known-users.ts - #11: Remove invalid recordKnownUser calls for guild/DM users - #12: sendGroupMessage uses sendAndNotify to trigger onMessageSent hook - #13: sendPhoto channel unsupported returns error field - #14: sendTextAfterMedia adds channel and dm branches Type fixes: - DeliverEventContext adds guildId field - MediaTargetContext.targetType adds dm variant - sendPlainTextReply imgMediaTarget adds DM branch * fix(qqbot): resolve 2 blockers + 7 medium-priority bugs from PR #52986 review Blocker-1: Remove unused dmPolicy config knob - dmPolicy was declared in schema/types/plugin.json but never consumed at runtime - Removed from config-schema.ts, types.ts, and openclaw.plugin.json - allowFrom remains active (already wired into framework command-auth) Blocker-2: Gate sensitive slash commands with allowFrom authorization - SlashCommand interface adds requireAuth?: boolean - SlashCommandContext adds commandAuthorized: boolean - /bot-logs set to requireAuth: true (reads local log files) - matchSlashCommand rejects unauthorized senders for requireAuth commands - trySlashCommandOrEnqueue computes commandAuthorized from allowFrom config Medium-priority fixes: - #15: Strip non-HTTP/non-local markdown image tags to prevent path leakage - #16: applyQQBotAccountConfig clears clientSecret when setting clientSecretFile and vice versa - #17: getAdminMarkerFile sanitizes accountId to prevent path traversal - #18: URGENT_COMMANDS uses exact match instead of startsWith prefix match - #19: isCronExpression validates each token starts with a cron-valid character - #20: --token format validation rejects malformed input without colon separator - #21: resolveDefaultQQBotAccountId checks QQBOT_APP_ID environment variable * test(qqbot): add focused tests for slash command authorization path - Unauthorized sender rejected for /bot-logs (requireAuth: true) - Authorized sender allowed for /bot-logs - Non-requireAuth commands (/bot-ping, /bot-help, /bot-version) work for all senders - Unknown slash commands return null (passthrough) - Non-slash messages return null - Usage query (/bot-logs ?) also gated by auth check * fix(qqbot): align global TTS fallback with framework config resolution - Extract isGlobalTTSAvailable to utils/audio-convert.ts, mirroring core resolveTtsConfig logic: check auto !== 'off', fall back to legacy enabled boolean, default to off when neither is set. - Add pre-check in reply-dispatcher before calling globalTextToSpeech to avoid unnecessary TTS calls and noisy error logs when TTS is not configured. - Remove inline as any casts; use OpenClawConfig type throughout. - Refactor handleAudioPayload into flat early-return structure with unified send path (plugin TTS → global fallback → send). * fix(qqbot): break ESM circular dependency causing multi-account startup crash The bundled gateway chunk had a circular static import on the channel chunk (gateway -> outbound-deliver -> channel, while channel dynamically imports gateway). When two accounts start concurrently via Promise.all, the first dynamic import triggers module graph evaluation; the circular reference causes api exports (including runDiagnostics) to resolve as undefined before the module finishes evaluating. Fix: extract chunkText and TEXT_CHUNK_LIMIT from channel.ts into a new text-utils.ts leaf module. outbound-deliver.ts now imports from text-utils.ts, breaking the cycle. channel.ts re-exports for backward compatibility. * fix(qqbot): serialize gateway module import to prevent multi-account startup race When multiple accounts start concurrently via Promise.all, each calls await import('./gateway.js') independently. Due to ESM circular dependencies in the bundled output, the first import can resolve transitive exports as undefined before module evaluation completes. Fix: cache the dynamic import promise in a module-level variable so all concurrent startAccount calls share the same import, ensuring the gateway module is fully evaluated before any account uses it. * refactor(qqbot): remove startup greeting logic Remove getStartupGreetingPlan and related startup greeting delivery: - Delete startup-greeting.ts (greeting plan, marker persistence) - Delete admin-resolver.ts (admin resolution, greeting dispatch) - Remove startup greeting calls from gateway READY/RESUMED handlers - Remove isFirstReadyGlobal flag and adminCtx * fix(qqbot): skip octal escape decoding for Windows local paths Windows paths like C:\Users\1\file.txt contain backslash-digit sequences that were incorrectly matched as octal escape sequences and decoded, corrupting the file path. Detect Windows local paths (drive letter or UNC prefix) and skip the octal decoding step for them. * fix bot issue * feat: 支持 TTS 自动开关并清理配置中的 clientSecretFile * docs: 添加 QQBot 配置和消息处理的设计说明 * rebase * fix(qqbot): align slash-command auth with shared command-auth model Route requireAuth:true slash commands (e.g. /bot-logs) through the framework's api.registerCommand() so resolveCommandAuthorization() applies commands.allowFrom.qqbot precedence and qqbot: prefix normalization before any handler runs. - slash-commands.ts: registerCommand() now auto-routes by requireAuth into two maps (commands / frameworkCommands); getFrameworkCommands() exports the auth-required set for framework registration; bot-help lists both maps - index.ts: registerFull() iterates getFrameworkCommands() and calls api.registerCommand() for each; handler derives msgType from ctx.from, sends file attachments via sendDocument, supports multi-account via ctx.accountId - gateway.ts (inbound): replace raw allowFrom string comparison with qqbotPlugin.config.formatAllowFrom() to strip qqbot: prefix and uppercase before matching event.senderId - gateway.ts (pre-dispatch): remove stale auth computation; commandAuthorized is true (requireAuth:true commands never reach matchSlashCommand) - command-auth.test.ts: add regression tests for qqbot: prefix normalization in the inbound commandAuthorized computation - slash-commands.test.ts: update /bot-logs tests to expect null (command routed to framework, not in local registry) * rebase and solve conflict * fix(qqbot): preserve mixed env setup credentials --------- Co-authored-by: yuehuali <yuehuali@tencent.com> Co-authored-by: walli <walli@tencent.com> Co-authored-by: WideLee <limkuan24@gmail.com> Co-authored-by: Frank Yang <frank.ekn@gmail.com>
588 lines
20 KiB
TypeScript
588 lines
20 KiB
TypeScript
import path from "node:path";
|
|
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
|
import { textToSpeech as globalTextToSpeech } from "openclaw/plugin-sdk/speech-runtime";
|
|
import {
|
|
getAccessToken,
|
|
sendC2CMessage,
|
|
sendChannelMessage,
|
|
sendDmMessage,
|
|
sendGroupMessage,
|
|
clearTokenCache,
|
|
sendC2CImageMessage,
|
|
sendGroupImageMessage,
|
|
sendC2CVoiceMessage,
|
|
sendGroupVoiceMessage,
|
|
sendC2CVideoMessage,
|
|
sendGroupVideoMessage,
|
|
sendC2CFileMessage,
|
|
sendGroupFileMessage,
|
|
} from "./api.js";
|
|
import type { ResolvedQQBotAccount } from "./types.js";
|
|
import {
|
|
isGlobalTTSAvailable,
|
|
resolveTTSConfig,
|
|
textToSilk,
|
|
audioFileToSilkBase64,
|
|
formatDuration,
|
|
} from "./utils/audio-convert.js";
|
|
import {
|
|
checkFileSize,
|
|
readFileAsync,
|
|
fileExistsAsync,
|
|
formatFileSize,
|
|
} from "./utils/file-utils.js";
|
|
import {
|
|
parseQQBotPayload,
|
|
encodePayloadForCron,
|
|
isCronReminderPayload,
|
|
isMediaPayload,
|
|
type MediaPayload,
|
|
} from "./utils/payload.js";
|
|
import {
|
|
getQQBotDataDir,
|
|
normalizePath,
|
|
resolveQQBotLocalMediaPath,
|
|
sanitizeFileName,
|
|
} from "./utils/platform.js";
|
|
|
|
export interface MessageTarget {
|
|
type: "c2c" | "guild" | "dm" | "group";
|
|
senderId: string;
|
|
messageId: string;
|
|
channelId?: string;
|
|
guildId?: string;
|
|
groupOpenid?: string;
|
|
}
|
|
|
|
export interface ReplyContext {
|
|
target: MessageTarget;
|
|
account: ResolvedQQBotAccount;
|
|
cfg: unknown;
|
|
log?: {
|
|
info: (msg: string) => void;
|
|
error: (msg: string) => void;
|
|
debug?: (msg: string) => void;
|
|
};
|
|
}
|
|
|
|
/** Send a message and retry once if the token appears to have expired. */
|
|
export async function sendWithTokenRetry<T>(
|
|
appId: string,
|
|
clientSecret: string,
|
|
sendFn: (token: string) => Promise<T>,
|
|
log?: ReplyContext["log"],
|
|
accountId?: string,
|
|
): Promise<T> {
|
|
try {
|
|
const token = await getAccessToken(appId, clientSecret);
|
|
return await sendFn(token);
|
|
} catch (err) {
|
|
const errMsg = String(err);
|
|
if (errMsg.includes("401") || errMsg.includes("token") || errMsg.includes("access_token")) {
|
|
log?.info(`[qqbot:${accountId}] Token may be expired, refreshing...`);
|
|
clearTokenCache(appId);
|
|
const newToken = await getAccessToken(appId, clientSecret);
|
|
return await sendFn(newToken);
|
|
} else {
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
|
|
/** Route a text message to the correct QQ target type. */
|
|
export async function sendTextToTarget(
|
|
ctx: ReplyContext,
|
|
text: string,
|
|
refIdx?: string,
|
|
): Promise<void> {
|
|
const { target, account } = ctx;
|
|
await sendWithTokenRetry(
|
|
account.appId,
|
|
account.clientSecret,
|
|
async (token) => {
|
|
if (target.type === "c2c") {
|
|
await sendC2CMessage(account.appId, token, target.senderId, text, target.messageId, refIdx);
|
|
} else if (target.type === "group" && target.groupOpenid) {
|
|
await sendGroupMessage(account.appId, token, target.groupOpenid, text, target.messageId);
|
|
} else if (target.channelId) {
|
|
await sendChannelMessage(token, target.channelId, text, target.messageId);
|
|
} else if (target.type === "dm" && target.guildId) {
|
|
await sendDmMessage(token, target.guildId, text, target.messageId);
|
|
}
|
|
},
|
|
ctx.log,
|
|
account.accountId,
|
|
);
|
|
}
|
|
|
|
/** Best-effort delivery for error text back to the user. */
|
|
export async function sendErrorToTarget(ctx: ReplyContext, errorText: string): Promise<void> {
|
|
try {
|
|
await sendTextToTarget(ctx, errorText);
|
|
} catch (sendErr) {
|
|
ctx.log?.error(`[qqbot:${ctx.account.accountId}] Failed to send error message: ${sendErr}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Handle a structured payload prefixed with `QQBOT_PAYLOAD:`.
|
|
* Returns true when the reply was handled here, otherwise false.
|
|
*/
|
|
export async function handleStructuredPayload(
|
|
ctx: ReplyContext,
|
|
replyText: string,
|
|
recordActivity: () => void,
|
|
): Promise<boolean> {
|
|
const { target, account, cfg, log } = ctx;
|
|
const payloadResult = parseQQBotPayload(replyText);
|
|
|
|
if (!payloadResult.isPayload) return false;
|
|
|
|
if (payloadResult.error) {
|
|
log?.error(`[qqbot:${account.accountId}] Payload parse error: ${payloadResult.error}`);
|
|
return true;
|
|
}
|
|
|
|
if (!payloadResult.payload) return true;
|
|
|
|
const parsedPayload = payloadResult.payload;
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] Detected structured payload, type: ${parsedPayload.type}`,
|
|
);
|
|
|
|
if (isCronReminderPayload(parsedPayload)) {
|
|
log?.info(`[qqbot:${account.accountId}] Processing cron_reminder payload`);
|
|
const cronMessage = encodePayloadForCron(parsedPayload);
|
|
const confirmText = `⏰ Reminder scheduled. It will be sent at the configured time: "${parsedPayload.content}"`;
|
|
try {
|
|
await sendTextToTarget(ctx, confirmText);
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] Cron reminder confirmation sent, cronMessage: ${cronMessage}`,
|
|
);
|
|
} catch (err) {
|
|
log?.error(`[qqbot:${account.accountId}] Failed to send cron confirmation: ${err}`);
|
|
}
|
|
recordActivity();
|
|
return true;
|
|
}
|
|
|
|
if (isMediaPayload(parsedPayload)) {
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] Processing media payload, mediaType: ${parsedPayload.mediaType}`,
|
|
);
|
|
|
|
if (parsedPayload.mediaType === "image") {
|
|
await handleImagePayload(ctx, parsedPayload);
|
|
} else if (parsedPayload.mediaType === "audio") {
|
|
await handleAudioPayload(ctx, parsedPayload);
|
|
} else if (parsedPayload.mediaType === "video") {
|
|
await handleVideoPayload(ctx, parsedPayload);
|
|
} else if (parsedPayload.mediaType === "file") {
|
|
await handleFilePayload(ctx, parsedPayload);
|
|
} else {
|
|
log?.error(
|
|
`[qqbot:${account.accountId}] Unknown media type: ${(parsedPayload as MediaPayload).mediaType}`,
|
|
);
|
|
}
|
|
recordActivity();
|
|
return true;
|
|
}
|
|
|
|
log?.error(`[qqbot:${account.accountId}] Unknown payload type: ${(parsedPayload as any).type}`);
|
|
return true;
|
|
}
|
|
|
|
// Media payload handlers.
|
|
|
|
async function handleImagePayload(ctx: ReplyContext, payload: MediaPayload): Promise<void> {
|
|
const { target, account, log } = ctx;
|
|
let imageUrl = resolveQQBotLocalMediaPath(normalizePath(payload.path));
|
|
const originalImagePath = payload.source === "file" ? imageUrl : undefined;
|
|
|
|
if (payload.source === "file") {
|
|
try {
|
|
if (!(await fileExistsAsync(imageUrl))) {
|
|
log?.error(`[qqbot:${account.accountId}] Image not found: ${imageUrl}`);
|
|
return;
|
|
}
|
|
const imgSzCheck = checkFileSize(imageUrl);
|
|
if (!imgSzCheck.ok) {
|
|
log?.error(`[qqbot:${account.accountId}] Image size check failed: ${imgSzCheck.error}`);
|
|
return;
|
|
}
|
|
const fileBuffer = await readFileAsync(imageUrl);
|
|
const base64Data = fileBuffer.toString("base64");
|
|
const ext = path.extname(imageUrl).toLowerCase();
|
|
const mimeTypes: Record<string, string> = {
|
|
".jpg": "image/jpeg",
|
|
".jpeg": "image/jpeg",
|
|
".png": "image/png",
|
|
".gif": "image/gif",
|
|
".webp": "image/webp",
|
|
".bmp": "image/bmp",
|
|
};
|
|
const mimeType = mimeTypes[ext];
|
|
if (!mimeType) {
|
|
log?.error(`[qqbot:${account.accountId}] Unsupported image format: ${ext}`);
|
|
return;
|
|
}
|
|
imageUrl = `data:${mimeType};base64,${base64Data}`;
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] Converted local image to Base64 (size: ${formatFileSize(fileBuffer.length)})`,
|
|
);
|
|
} catch (readErr) {
|
|
log?.error(`[qqbot:${account.accountId}] Failed to read local image: ${readErr}`);
|
|
return;
|
|
}
|
|
}
|
|
|
|
try {
|
|
await sendWithTokenRetry(
|
|
account.appId,
|
|
account.clientSecret,
|
|
async (token) => {
|
|
if (target.type === "c2c") {
|
|
await sendC2CImageMessage(
|
|
account.appId,
|
|
token,
|
|
target.senderId,
|
|
imageUrl,
|
|
target.messageId,
|
|
undefined,
|
|
originalImagePath,
|
|
);
|
|
} else if (target.type === "group" && target.groupOpenid) {
|
|
await sendGroupImageMessage(
|
|
account.appId,
|
|
token,
|
|
target.groupOpenid,
|
|
imageUrl,
|
|
target.messageId,
|
|
);
|
|
} else if (target.type === "dm" && target.guildId) {
|
|
// By design: DM only supports text/markdown; use markdown image syntax with the
|
|
// original path so the QQ client can attempt to render it.
|
|
await sendDmMessage(token, target.guildId, ``, target.messageId);
|
|
} else if (target.channelId) {
|
|
// By design: channel messages only support text/markdown, same approach as DM above.
|
|
await sendChannelMessage(
|
|
token,
|
|
target.channelId,
|
|
``,
|
|
target.messageId,
|
|
);
|
|
}
|
|
},
|
|
log,
|
|
account.accountId,
|
|
);
|
|
log?.info(`[qqbot:${account.accountId}] Sent image via media payload`);
|
|
|
|
if (payload.caption) {
|
|
await sendTextToTarget(ctx, payload.caption);
|
|
}
|
|
} catch (err) {
|
|
log?.error(`[qqbot:${account.accountId}] Failed to send image: ${err}`);
|
|
}
|
|
}
|
|
|
|
async function handleAudioPayload(ctx: ReplyContext, payload: MediaPayload): Promise<void> {
|
|
const { target, account, cfg, log } = ctx;
|
|
try {
|
|
const ttsText = payload.caption || payload.path;
|
|
if (!ttsText?.trim()) {
|
|
log?.error(`[qqbot:${account.accountId}] Voice missing text`);
|
|
return;
|
|
}
|
|
|
|
let silkBase64: string | undefined;
|
|
let silkPath: string | undefined;
|
|
let duration: number | undefined;
|
|
let providerLabel: string | undefined;
|
|
|
|
// Strategy 1: Plugin-specific TTS (OpenAI-compatible /audio/speech API).
|
|
const ttsCfg = resolveTTSConfig(cfg as Record<string, unknown>);
|
|
if (ttsCfg) {
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] TTS (plugin): "${ttsText.slice(0, 50)}..." via ${ttsCfg.model}`,
|
|
);
|
|
const ttsDir = getQQBotDataDir("tts");
|
|
const result = await textToSilk(ttsText, ttsCfg, ttsDir);
|
|
silkBase64 = result.silkBase64;
|
|
silkPath = result.silkPath;
|
|
duration = result.duration;
|
|
providerLabel = ttsCfg.model;
|
|
} else {
|
|
// Strategy 2: Fall back to global TTS provider registry (e.g. Edge TTS).
|
|
if (!isGlobalTTSAvailable(cfg as OpenClawConfig)) {
|
|
log?.error(
|
|
`[qqbot:${account.accountId}] TTS not configured (neither plugin channels.qqbot.tts nor global messages.tts)`,
|
|
);
|
|
return;
|
|
}
|
|
log?.info(`[qqbot:${account.accountId}] TTS (global fallback): "${ttsText.slice(0, 50)}..."`);
|
|
const globalResult = await globalTextToSpeech({
|
|
text: ttsText,
|
|
cfg: cfg as OpenClawConfig,
|
|
channel: "qqbot",
|
|
});
|
|
if (!globalResult.success || !globalResult.audioPath) {
|
|
log?.error(
|
|
`[qqbot:${account.accountId}] Global TTS failed: ${globalResult.error ?? "unknown"}`,
|
|
);
|
|
return;
|
|
}
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] Global TTS returned: provider=${globalResult.provider}, format=${globalResult.outputFormat}, path=${globalResult.audioPath}`,
|
|
);
|
|
providerLabel = globalResult.provider ?? "global";
|
|
|
|
// Convert the global TTS audio file to SILK for QQ upload.
|
|
const base64 = await audioFileToSilkBase64(globalResult.audioPath);
|
|
if (!base64) {
|
|
log?.error(`[qqbot:${account.accountId}] Failed to convert global TTS audio to SILK`);
|
|
return;
|
|
}
|
|
silkBase64 = base64;
|
|
silkPath = globalResult.audioPath;
|
|
duration = 0; // Duration unknown from global TTS; use 0 as fallback.
|
|
}
|
|
|
|
if (!silkBase64) {
|
|
log?.error(`[qqbot:${account.accountId}] TTS produced no audio output`);
|
|
return;
|
|
}
|
|
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] TTS done (${providerLabel}): ${duration ? formatDuration(duration) : "N/A"}, file: ${silkPath ?? "N/A"}`,
|
|
);
|
|
|
|
await sendWithTokenRetry(
|
|
account.appId,
|
|
account.clientSecret,
|
|
async (token) => {
|
|
if (target.type === "c2c") {
|
|
await sendC2CVoiceMessage(
|
|
account.appId,
|
|
token,
|
|
target.senderId,
|
|
silkBase64!,
|
|
undefined,
|
|
target.messageId,
|
|
ttsText,
|
|
silkPath,
|
|
);
|
|
} else if (target.type === "group" && target.groupOpenid) {
|
|
await sendGroupVoiceMessage(
|
|
account.appId,
|
|
token,
|
|
target.groupOpenid,
|
|
silkBase64!,
|
|
undefined,
|
|
target.messageId,
|
|
);
|
|
} else if (target.type === "dm" && target.guildId) {
|
|
log?.error(
|
|
`[qqbot:${account.accountId}] Voice not supported in DM, sending text fallback`,
|
|
);
|
|
await sendDmMessage(token, target.guildId, ttsText, target.messageId);
|
|
} else if (target.channelId) {
|
|
log?.error(
|
|
`[qqbot:${account.accountId}] Voice not supported in channel, sending text fallback`,
|
|
);
|
|
await sendChannelMessage(token, target.channelId, ttsText, target.messageId);
|
|
}
|
|
},
|
|
log,
|
|
account.accountId,
|
|
);
|
|
log?.info(`[qqbot:${account.accountId}] Voice message sent`);
|
|
} catch (err) {
|
|
log?.error(`[qqbot:${account.accountId}] TTS/voice send failed: ${err}`);
|
|
}
|
|
}
|
|
|
|
async function handleVideoPayload(ctx: ReplyContext, payload: MediaPayload): Promise<void> {
|
|
const { target, account, log } = ctx;
|
|
try {
|
|
const videoPath = resolveQQBotLocalMediaPath(normalizePath(payload.path ?? ""));
|
|
if (!videoPath?.trim()) {
|
|
log?.error(`[qqbot:${account.accountId}] Video missing path`);
|
|
} else {
|
|
const isHttpUrl = videoPath.startsWith("http://") || videoPath.startsWith("https://");
|
|
log?.info(`[qqbot:${account.accountId}] Video send: "${videoPath.slice(0, 60)}..."`);
|
|
|
|
await sendWithTokenRetry(
|
|
account.appId,
|
|
account.clientSecret,
|
|
async (token) => {
|
|
if (isHttpUrl) {
|
|
if (target.type === "c2c") {
|
|
await sendC2CVideoMessage(
|
|
account.appId,
|
|
token,
|
|
target.senderId,
|
|
videoPath,
|
|
undefined,
|
|
target.messageId,
|
|
);
|
|
} else if (target.type === "group" && target.groupOpenid) {
|
|
await sendGroupVideoMessage(
|
|
account.appId,
|
|
token,
|
|
target.groupOpenid,
|
|
videoPath,
|
|
undefined,
|
|
target.messageId,
|
|
);
|
|
} else if (target.type === "dm") {
|
|
log?.error(`[qqbot:${account.accountId}] Video not supported in DM`);
|
|
} else if (target.channelId) {
|
|
log?.error(`[qqbot:${account.accountId}] Video not supported in channel`);
|
|
}
|
|
} else {
|
|
if (!(await fileExistsAsync(videoPath))) {
|
|
throw new Error(`Video file does not exist: ${videoPath}`);
|
|
}
|
|
const vPaySzCheck = checkFileSize(videoPath);
|
|
if (!vPaySzCheck.ok) {
|
|
throw new Error(vPaySzCheck.error!);
|
|
}
|
|
const fileBuffer = await readFileAsync(videoPath);
|
|
const videoBase64 = fileBuffer.toString("base64");
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] Read local video (${formatFileSize(fileBuffer.length)}): ${videoPath}`,
|
|
);
|
|
|
|
if (target.type === "c2c") {
|
|
await sendC2CVideoMessage(
|
|
account.appId,
|
|
token,
|
|
target.senderId,
|
|
undefined,
|
|
videoBase64,
|
|
target.messageId,
|
|
undefined,
|
|
videoPath,
|
|
);
|
|
} else if (target.type === "group" && target.groupOpenid) {
|
|
await sendGroupVideoMessage(
|
|
account.appId,
|
|
token,
|
|
target.groupOpenid,
|
|
undefined,
|
|
videoBase64,
|
|
target.messageId,
|
|
);
|
|
} else if (target.type === "dm") {
|
|
log?.error(`[qqbot:${account.accountId}] Video not supported in DM`);
|
|
} else if (target.channelId) {
|
|
log?.error(`[qqbot:${account.accountId}] Video not supported in channel`);
|
|
}
|
|
}
|
|
},
|
|
log,
|
|
account.accountId,
|
|
);
|
|
log?.info(`[qqbot:${account.accountId}] Video message sent`);
|
|
|
|
if (payload.caption) {
|
|
await sendTextToTarget(ctx, payload.caption);
|
|
}
|
|
}
|
|
} catch (err) {
|
|
log?.error(`[qqbot:${account.accountId}] Video send failed: ${err}`);
|
|
}
|
|
}
|
|
|
|
async function handleFilePayload(ctx: ReplyContext, payload: MediaPayload): Promise<void> {
|
|
const { target, account, log } = ctx;
|
|
try {
|
|
const filePath = resolveQQBotLocalMediaPath(normalizePath(payload.path ?? ""));
|
|
if (!filePath?.trim()) {
|
|
log?.error(`[qqbot:${account.accountId}] File missing path`);
|
|
} else {
|
|
const isHttpUrl = filePath.startsWith("http://") || filePath.startsWith("https://");
|
|
const fileName = sanitizeFileName(path.basename(filePath));
|
|
log?.info(
|
|
`[qqbot:${account.accountId}] File send: "${filePath.slice(0, 60)}..." (${isHttpUrl ? "URL" : "local"})`,
|
|
);
|
|
|
|
await sendWithTokenRetry(
|
|
account.appId,
|
|
account.clientSecret,
|
|
async (token) => {
|
|
if (isHttpUrl) {
|
|
if (target.type === "c2c") {
|
|
await sendC2CFileMessage(
|
|
account.appId,
|
|
token,
|
|
target.senderId,
|
|
undefined,
|
|
filePath,
|
|
target.messageId,
|
|
fileName,
|
|
);
|
|
} else if (target.type === "group" && target.groupOpenid) {
|
|
await sendGroupFileMessage(
|
|
account.appId,
|
|
token,
|
|
target.groupOpenid,
|
|
undefined,
|
|
filePath,
|
|
target.messageId,
|
|
fileName,
|
|
);
|
|
} else if (target.type === "dm") {
|
|
log?.error(`[qqbot:${account.accountId}] File not supported in DM`);
|
|
} else if (target.channelId) {
|
|
log?.error(`[qqbot:${account.accountId}] File not supported in channel`);
|
|
}
|
|
} else {
|
|
if (!(await fileExistsAsync(filePath))) {
|
|
throw new Error(`File does not exist: ${filePath}`);
|
|
}
|
|
const fPaySzCheck = checkFileSize(filePath);
|
|
if (!fPaySzCheck.ok) {
|
|
throw new Error(fPaySzCheck.error!);
|
|
}
|
|
const fileBuffer = await readFileAsync(filePath);
|
|
const fileBase64 = fileBuffer.toString("base64");
|
|
if (target.type === "c2c") {
|
|
await sendC2CFileMessage(
|
|
account.appId,
|
|
token,
|
|
target.senderId,
|
|
fileBase64,
|
|
undefined,
|
|
target.messageId,
|
|
fileName,
|
|
filePath,
|
|
);
|
|
} else if (target.type === "group" && target.groupOpenid) {
|
|
await sendGroupFileMessage(
|
|
account.appId,
|
|
token,
|
|
target.groupOpenid,
|
|
fileBase64,
|
|
undefined,
|
|
target.messageId,
|
|
fileName,
|
|
);
|
|
} else if (target.type === "dm") {
|
|
log?.error(`[qqbot:${account.accountId}] File not supported in DM`);
|
|
} else if (target.channelId) {
|
|
log?.error(`[qqbot:${account.accountId}] File not supported in channel`);
|
|
}
|
|
}
|
|
},
|
|
log,
|
|
account.accountId,
|
|
);
|
|
log?.info(`[qqbot:${account.accountId}] File message sent`);
|
|
}
|
|
} catch (err) {
|
|
log?.error(`[qqbot:${account.accountId}] File send failed: ${err}`);
|
|
}
|
|
}
|