fix(channels): forward line and zalo URL attachments
This commit is contained in:
@@ -74,6 +74,56 @@ describe('LineAdapter', () => {
|
|||||||
expect(mockFetch.mock.calls[0]?.[0]).toBe('https://api.line.me/v2/bot/message/push');
|
expect(mockFetch.mock.calls[0]?.[0]).toBe('https://api.line.me/v2/bot/message/push');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('send emits URL attachments and warns for binary attachments', async () => {
|
||||||
|
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||||
|
const adapter = new LineAdapter({
|
||||||
|
channelAccessToken: 'token',
|
||||||
|
channelSecret: 'secret',
|
||||||
|
});
|
||||||
|
await adapter.connect();
|
||||||
|
mockFetch.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
text: async () => '',
|
||||||
|
} as Response);
|
||||||
|
|
||||||
|
await adapter.send('U123', {
|
||||||
|
text: 'hello line',
|
||||||
|
attachments: [
|
||||||
|
{ mimeType: 'text/plain', url: 'https://example.com/file.txt', filename: 'file.txt' },
|
||||||
|
{ mimeType: 'image/png', data: 'aGVsbG8=' },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||||
|
const secondBody = JSON.parse(String(mockFetch.mock.calls[1]?.[1]?.body ?? '{}'));
|
||||||
|
expect(secondBody.messages?.[0]?.text).toBe('file.txt: https://example.com/file.txt');
|
||||||
|
expect(warnSpy).toHaveBeenCalledWith('LINE: skipping attachment data (image/png) — upload not implemented');
|
||||||
|
warnSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('send delivers URL attachment even when text is empty', async () => {
|
||||||
|
const adapter = new LineAdapter({
|
||||||
|
channelAccessToken: 'token',
|
||||||
|
channelSecret: 'secret',
|
||||||
|
});
|
||||||
|
await adapter.connect();
|
||||||
|
mockFetch.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
text: async () => '',
|
||||||
|
} as Response);
|
||||||
|
|
||||||
|
await adapter.send('U123', {
|
||||||
|
text: ' ',
|
||||||
|
attachments: [{ mimeType: 'text/plain', url: 'https://example.com/file.txt', filename: 'file.txt' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(1);
|
||||||
|
const body = JSON.parse(String(mockFetch.mock.calls[0]?.[1]?.body ?? '{}'));
|
||||||
|
expect(body.messages?.[0]?.text).toBe('file.txt: https://example.com/file.txt');
|
||||||
|
});
|
||||||
|
|
||||||
it('handleRequest validates signature and dispatches text event', async () => {
|
it('handleRequest validates signature and dispatches text event', async () => {
|
||||||
const adapter = new LineAdapter({
|
const adapter = new LineAdapter({
|
||||||
channelAccessToken: 'token',
|
channelAccessToken: 'token',
|
||||||
|
|||||||
@@ -70,13 +70,27 @@ export class LineAdapter implements ChannelAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const text = message.text.trim();
|
const text = message.text.trim();
|
||||||
if (!text) {
|
const attachments = message.attachments ?? [];
|
||||||
|
if (!text && attachments.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunks = text.length > MAX_MESSAGE_LENGTH ? splitMessage(text, MAX_MESSAGE_LENGTH) : [text];
|
if (text) {
|
||||||
for (const chunk of chunks) {
|
const chunks = text.length > MAX_MESSAGE_LENGTH ? splitMessage(text, MAX_MESSAGE_LENGTH) : [text];
|
||||||
await this.sendPush(peerId, chunk);
|
for (const chunk of chunks) {
|
||||||
|
await this.sendPush(peerId, chunk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const attachment of attachments) {
|
||||||
|
if (attachment.url) {
|
||||||
|
const line = attachment.filename ? `${attachment.filename}: ${attachment.url}` : attachment.url;
|
||||||
|
await this.sendPush(peerId, line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (attachment.data) {
|
||||||
|
console.warn(`LINE: skipping attachment data (${attachment.mimeType}) — upload not implemented`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -63,6 +63,50 @@ describe('ZaloAdapter', () => {
|
|||||||
expect(String(mockFetch.mock.calls[0]?.[0])).toContain('/v3.0/oa/message/cs');
|
expect(String(mockFetch.mock.calls[0]?.[0])).toContain('/v3.0/oa/message/cs');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('send emits URL attachments and warns for binary attachments', async () => {
|
||||||
|
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
||||||
|
const adapter = new ZaloAdapter({ oaAccessToken: 'token' });
|
||||||
|
await adapter.connect();
|
||||||
|
mockFetch.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
text: async () => '',
|
||||||
|
} as Response);
|
||||||
|
|
||||||
|
await adapter.send('uid-1', {
|
||||||
|
text: 'hello zalo',
|
||||||
|
attachments: [
|
||||||
|
{ mimeType: 'text/plain', url: 'https://example.com/file.txt', filename: 'file.txt' },
|
||||||
|
{ mimeType: 'application/pdf', data: 'aGVsbG8=' },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(2);
|
||||||
|
const secondBody = JSON.parse(String(mockFetch.mock.calls[1]?.[1]?.body ?? '{}'));
|
||||||
|
expect(secondBody.message?.text).toBe('file.txt: https://example.com/file.txt');
|
||||||
|
expect(warnSpy).toHaveBeenCalledWith('Zalo: skipping attachment data (application/pdf) — upload not implemented');
|
||||||
|
warnSpy.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('send delivers URL attachment even when text is empty', async () => {
|
||||||
|
const adapter = new ZaloAdapter({ oaAccessToken: 'token' });
|
||||||
|
await adapter.connect();
|
||||||
|
mockFetch.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
status: 200,
|
||||||
|
text: async () => '',
|
||||||
|
} as Response);
|
||||||
|
|
||||||
|
await adapter.send('uid-1', {
|
||||||
|
text: ' ',
|
||||||
|
attachments: [{ mimeType: 'text/plain', url: 'https://example.com/file.txt', filename: 'file.txt' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockFetch).toHaveBeenCalledTimes(1);
|
||||||
|
const body = JSON.parse(String(mockFetch.mock.calls[0]?.[1]?.body ?? '{}'));
|
||||||
|
expect(body.message?.text).toBe('file.txt: https://example.com/file.txt');
|
||||||
|
});
|
||||||
|
|
||||||
it('handleEvent emits inbound message', async () => {
|
it('handleEvent emits inbound message', async () => {
|
||||||
const adapter = new ZaloAdapter({ oaAccessToken: 'token', requireMention: false });
|
const adapter = new ZaloAdapter({ oaAccessToken: 'token', requireMention: false });
|
||||||
const inbound: Array<{ channel: string; senderId: string; text: string }> = [];
|
const inbound: Array<{ channel: string; senderId: string; text: string }> = [];
|
||||||
|
|||||||
@@ -60,13 +60,27 @@ export class ZaloAdapter implements ChannelAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const text = message.text.trim();
|
const text = message.text.trim();
|
||||||
if (!text) {
|
const attachments = message.attachments ?? [];
|
||||||
|
if (!text && attachments.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunks = text.length > MAX_MESSAGE_LENGTH ? splitMessage(text, MAX_MESSAGE_LENGTH) : [text];
|
if (text) {
|
||||||
for (const chunk of chunks) {
|
const chunks = text.length > MAX_MESSAGE_LENGTH ? splitMessage(text, MAX_MESSAGE_LENGTH) : [text];
|
||||||
await this.sendText(peerId, chunk);
|
for (const chunk of chunks) {
|
||||||
|
await this.sendText(peerId, chunk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const attachment of attachments) {
|
||||||
|
if (attachment.url) {
|
||||||
|
const line = attachment.filename ? `${attachment.filename}: ${attachment.url}` : attachment.url;
|
||||||
|
await this.sendText(peerId, line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (attachment.data) {
|
||||||
|
console.warn(`Zalo: skipping attachment data (${attachment.mimeType}) — upload not implemented`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user