diff --git a/packages/onebot/src/message-parser.ts b/packages/onebot/src/message-parser.ts index 52fe2b47..5dd7ae32 100644 --- a/packages/onebot/src/message-parser.ts +++ b/packages/onebot/src/message-parser.ts @@ -54,6 +54,32 @@ export function parseCQParams(raw: string): Record { return params; } +/** + * Pick the best loadable source from a media segment's `file` / `url` / `path` + * / `media` fields. + * + * `file` is normally the canonical OneBot field and wins, but it can also be a + * QQ-internal media id (e.g. `.png`) that this process cannot resolve to a + * local path. When a bot framework echoes a received image back (Yunzai et al. + * resend the original `file=.ext` together with the download `url`), using + * `file` makes the send path `statSync` the id as a bogus local path and throw + * `ENOENT`. So: keep `file` when it is a directly loadable source (inline + * bytes, a remote url, or a filesystem path with a separator); otherwise, if a + * real http(s) `url` accompanies it, prefer that. (issue #155) + */ +function pickMediaSource(data: Record): string { + const file = String(data.file ?? '').trim(); + const url = String(data.url ?? '').trim(); + const fallback = file || url || String(data.path ?? '').trim() || String(data.media ?? '').trim(); + if (!file) return fallback; + // `file` is itself loadable: inline bytes, a remote url, or a path (anything + // carrying a `/` or `\` separator, incl. file:// and absolute/relative paths). + if (/^(base64:\/\/|data:|https?:\/\/|file:\/\/)/i.test(file) || /[\\/]/.test(file)) return file; + // `file` is a bare token (QQ-internal id) — fall back to a real url if present. + if (/^https?:\/\//i.test(url)) return url; + return fallback; +} + // --- JSON segment parsing --- interface MessageSegment { @@ -136,7 +162,7 @@ export async function segmentToElement(type: string, data: Record { expect(result[0].url).toBe('https://example.com/img.png'); }); + it('prefers a real url when file is a QQ-internal id (issue #155)', async () => { + // Yunzai et al. echo a received image by resending the original + // `file=.ext` together with the download `url`. `file` is not a + // local path, so picking it would statSync → ENOENT on send. + const result = await parseMessage( + [{ + type: 'image', + data: { + file: '35246A5B5C287F680C90839829FD7620.png', + url: 'https://multimedia.nt.qq.com.cn/download?fileid=abc&rkey=xyz', + sub_type: 1, + summary: '[动画表情]', + }, + }] as any, + true, + ); + expect(result[0].type).toBe('image'); + expect(result[0].url).toBe('https://multimedia.nt.qq.com.cn/download?fileid=abc&rkey=xyz'); + }); + + it('keeps file when it is itself loadable even if a url is also present', async () => { + // A path (has a separator) or inline bytes are the actual content and + // must win over any sibling url. + const viaPath = await parseMessage( + [{ type: 'image', data: { file: '/tmp/local.png', url: 'https://example.com/other.png' } }] as any, + false, + ); + expect(viaPath[0].url).toBe('/tmp/local.png'); + + const viaBase64 = await parseMessage( + [{ type: 'image', data: { file: 'base64://AAAA', url: 'https://example.com/other.png' } }] as any, + false, + ); + expect(viaBase64[0].url).toBe('base64://AAAA'); + }); + + it('falls back a QQ-internal id record/video to the sibling url (issue #155)', async () => { + const rec = await parseMessage( + [{ type: 'record', data: { file: 'ABCD1234.amr', url: 'https://example.com/v.amr' } }] as any, + false, + ); + expect(rec).toEqual([{ type: 'record', url: 'https://example.com/v.amr' }]); + + const vid = await parseMessage( + [{ type: 'video', data: { file: 'EF567890.mp4', url: 'https://example.com/v.mp4' } }] as any, + false, + ); + expect(vid).toEqual([{ type: 'video', url: 'https://example.com/v.mp4', thumbUrl: undefined }]); + }); + it('parses record segment from data.file', async () => { const result = await parseMessage( [{ type: 'record', data: { file: 'file:///tmp/voice.amr' } }] as any,