feat: 支持本地图片上传和多媒体字段处理
This commit is contained in:
@@ -379,25 +379,52 @@ MEDIA:/绝对路径/图片文件.png
|
|||||||
cfg,
|
cfg,
|
||||||
dispatcherOptions: {
|
dispatcherOptions: {
|
||||||
responsePrefix: messagesConfig.responsePrefix,
|
responsePrefix: messagesConfig.responsePrefix,
|
||||||
deliver: async (payload: { text?: string }) => {
|
deliver: async (payload: { text?: string; mediaUrls?: string[]; mediaUrl?: string }) => {
|
||||||
hasResponse = true;
|
hasResponse = true;
|
||||||
if (timeoutId) {
|
if (timeoutId) {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
timeoutId = null;
|
timeoutId = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
log?.info(`[qqbot:${account.accountId}] deliver called, payload: ${JSON.stringify(payload).slice(0, 200)}`);
|
log?.info(`[qqbot:${account.accountId}] deliver called, payload keys: ${Object.keys(payload).join(", ")}`);
|
||||||
|
|
||||||
let replyText = payload.text ?? "";
|
let replyText = payload.text ?? "";
|
||||||
if (!replyText.trim()) {
|
|
||||||
log?.info(`[qqbot:${account.accountId}] Empty reply text, skipping`);
|
// 收集所有图片路径
|
||||||
|
const imageUrls: string[] = [];
|
||||||
|
|
||||||
|
// 处理 mediaUrls 和 mediaUrl 字段(本地文件路径)
|
||||||
|
const mediaPaths: string[] = [];
|
||||||
|
if (payload.mediaUrls?.length) {
|
||||||
|
mediaPaths.push(...payload.mediaUrls);
|
||||||
|
}
|
||||||
|
if (payload.mediaUrl && !mediaPaths.includes(payload.mediaUrl)) {
|
||||||
|
mediaPaths.push(payload.mediaUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const localPath of mediaPaths) {
|
||||||
|
if (localPath && imageServerBaseUrl) {
|
||||||
|
try {
|
||||||
|
const savedUrl = saveImageFromPath(localPath);
|
||||||
|
if (savedUrl) {
|
||||||
|
imageUrls.push(savedUrl);
|
||||||
|
log?.info(`[qqbot:${account.accountId}] Saved media to server: ${localPath}`);
|
||||||
|
} else {
|
||||||
|
log?.error(`[qqbot:${account.accountId}] Failed to save media (not found or not image): ${localPath}`);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
log?.error(`[qqbot:${account.accountId}] Failed to save media: ${err}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有文本也没有图片,跳过
|
||||||
|
if (!replyText.trim() && imageUrls.length === 0) {
|
||||||
|
log?.info(`[qqbot:${account.accountId}] Empty reply, skipping`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 提取回复中的图片
|
// 0. 提取 MEDIA: 前缀的本地文件路径(从文本中)
|
||||||
const imageUrls: string[] = [];
|
|
||||||
|
|
||||||
// 0. 提取 MEDIA: 前缀的本地文件路径
|
|
||||||
const mediaPathRegex = /MEDIA:([^\s\n]+)/gi;
|
const mediaPathRegex = /MEDIA:([^\s\n]+)/gi;
|
||||||
const mediaMatches = [...replyText.matchAll(mediaPathRegex)];
|
const mediaMatches = [...replyText.matchAll(mediaPathRegex)];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user