@ai-devkit/channel-connector 0.9.0 โ 0.10.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/dist/__tests__/adapters/TelegramAdapter.test.js +92 -1
- package/dist/__tests__/adapters/TelegramAdapter.test.js.map +1 -1
- package/dist/__tests__/utils/telegramHtml.test.js +6 -0
- package/dist/__tests__/utils/telegramHtml.test.js.map +1 -1
- package/dist/adapters/TelegramAdapter.d.ts +2 -3
- package/dist/adapters/TelegramAdapter.d.ts.map +1 -1
- package/dist/adapters/TelegramAdapter.js +237 -7
- package/dist/adapters/TelegramAdapter.js.map +1 -1
- package/dist/utils/telegramHtml.js +1 -1
- package/dist/utils/telegramHtml.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as telegrafModule from 'telegraf';
|
|
2
|
+
import * as telegramHtml from '../../utils/telegramHtml.js';
|
|
2
3
|
import { TelegramAdapter } from '../../adapters/TelegramAdapter.js';
|
|
3
4
|
// Mock telegraf
|
|
4
5
|
vi.mock('telegraf', ()=>{
|
|
@@ -46,6 +47,12 @@ vi.mock('telegraf', ()=>{
|
|
|
46
47
|
function getMockBot() {
|
|
47
48
|
return telegrafModule.__mockBot;
|
|
48
49
|
}
|
|
50
|
+
function expectHtmlChunkWithinLimit(chunk) {
|
|
51
|
+
expect(chunk.length).toBeLessThanOrEqual(4096);
|
|
52
|
+
expect(chunk).not.toMatch(/&(?:l|lt|g|gt|a|am|amp|q|qu|quo|quot)$/);
|
|
53
|
+
expect((chunk.match(/<pre>/g) ?? []).length).toBe((chunk.match(/<\/pre>/g) ?? []).length);
|
|
54
|
+
expect((chunk.match(/<code(?: class="language-[^"]+")?>/g) ?? []).length).toBe((chunk.match(/<\/code>/g) ?? []).length);
|
|
55
|
+
}
|
|
49
56
|
describe('TelegramAdapter', ()=>{
|
|
50
57
|
let adapter;
|
|
51
58
|
beforeEach(()=>{
|
|
@@ -147,6 +154,72 @@ describe('TelegramAdapter', ()=>{
|
|
|
147
154
|
});
|
|
148
155
|
}
|
|
149
156
|
});
|
|
157
|
+
it('should split long fenced code into independently valid HTML code blocks', async ()=>{
|
|
158
|
+
const bot = getMockBot();
|
|
159
|
+
const codeLine = 'const sample = "<code>tag</code>" && value < limit;\n';
|
|
160
|
+
const longMessage = `Before\n\n\`\`\`ts\n${codeLine.repeat(100)}\`\`\`\n\nAfter`;
|
|
161
|
+
await adapter.sendMessage('12345', longMessage);
|
|
162
|
+
expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);
|
|
163
|
+
for (const call of bot.telegram.sendMessage.mock.calls){
|
|
164
|
+
const chunk = call[1];
|
|
165
|
+
expect(call[2]).toEqual({
|
|
166
|
+
parse_mode: 'HTML'
|
|
167
|
+
});
|
|
168
|
+
expectHtmlChunkWithinLimit(chunk);
|
|
169
|
+
if (chunk.includes('<pre>')) {
|
|
170
|
+
expect(chunk).toContain('<code class="language-ts">');
|
|
171
|
+
expect(chunk).toContain('<code>tag</code>');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
it('should split nested list fenced code without sending partial HTML tags', async ()=>{
|
|
176
|
+
const bot = getMockBot();
|
|
177
|
+
const codeLine = ' return "<code>tag</code>" && input < output;\n';
|
|
178
|
+
const longMessage = [
|
|
179
|
+
'- parent item',
|
|
180
|
+
' - child item with code',
|
|
181
|
+
' ```js',
|
|
182
|
+
codeLine.repeat(100).trimEnd(),
|
|
183
|
+
' ```',
|
|
184
|
+
'- final item'
|
|
185
|
+
].join('\n');
|
|
186
|
+
await adapter.sendMessage('12345', longMessage);
|
|
187
|
+
expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);
|
|
188
|
+
for (const call of bot.telegram.sendMessage.mock.calls){
|
|
189
|
+
const chunk = call[1];
|
|
190
|
+
expect(call[2]).toEqual({
|
|
191
|
+
parse_mode: 'HTML'
|
|
192
|
+
});
|
|
193
|
+
expectHtmlChunkWithinLimit(chunk);
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
it('should split long paragraphs at readable markdown boundaries', async ()=>{
|
|
197
|
+
const bot = getMockBot();
|
|
198
|
+
const sentence = 'This is a long sentence with enough words to make chunking choose sentence boundaries. ';
|
|
199
|
+
const longMessage = sentence.repeat(80);
|
|
200
|
+
await adapter.sendMessage('12345', longMessage);
|
|
201
|
+
expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);
|
|
202
|
+
for (const call of bot.telegram.sendMessage.mock.calls){
|
|
203
|
+
const chunk = call[1];
|
|
204
|
+
expect(call[2]).toEqual({
|
|
205
|
+
parse_mode: 'HTML'
|
|
206
|
+
});
|
|
207
|
+
expectHtmlChunkWithinLimit(chunk);
|
|
208
|
+
expect(chunk.endsWith('.') || chunk.endsWith('.\n\n') || chunk.endsWith(' ')).toBe(true);
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
it('should keep emoji chunks within the configured JavaScript string length limit', async ()=>{
|
|
212
|
+
const bot = getMockBot();
|
|
213
|
+
const longMessage = `Status ${'๐งช'.repeat(2500)}`;
|
|
214
|
+
await adapter.sendMessage('12345', longMessage);
|
|
215
|
+
expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);
|
|
216
|
+
for (const call of bot.telegram.sendMessage.mock.calls){
|
|
217
|
+
expect(call[1].length).toBeLessThanOrEqual(4096);
|
|
218
|
+
expect(call[2]).toEqual({
|
|
219
|
+
parse_mode: 'HTML'
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
});
|
|
150
223
|
it('should hard split at 4096 when no newlines available', async ()=>{
|
|
151
224
|
const bot = getMockBot();
|
|
152
225
|
const longMessage = 'A'.repeat(5000);
|
|
@@ -163,7 +236,7 @@ describe('TelegramAdapter', ()=>{
|
|
|
163
236
|
await adapter.sendMessage('12345', message);
|
|
164
237
|
// First chunk should end at a \n\n boundary, not mid-paragraph
|
|
165
238
|
const firstChunk = bot.telegram.sendMessage.mock.calls[0][1];
|
|
166
|
-
expect(firstChunk.
|
|
239
|
+
expect(firstChunk).toBe(`${paragraph}\n\n${paragraph}`);
|
|
167
240
|
});
|
|
168
241
|
it('should send short messages in a single call', async ()=>{
|
|
169
242
|
const bot = getMockBot();
|
|
@@ -210,6 +283,24 @@ describe('TelegramAdapter', ()=>{
|
|
|
210
283
|
expect(plainChunk).not.toContain('<');
|
|
211
284
|
expect(plainChunk).not.toContain('&');
|
|
212
285
|
});
|
|
286
|
+
it('should deliver nested lists without throwing', async ()=>{
|
|
287
|
+
const bot = getMockBot();
|
|
288
|
+
await adapter.sendMessage('12345', '- agent\n - Status');
|
|
289
|
+
expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);
|
|
290
|
+
expect(bot.telegram.sendMessage.mock.calls[0][1]).toContain('Status');
|
|
291
|
+
});
|
|
292
|
+
it('should send plain text when markdown formatting throws', async ()=>{
|
|
293
|
+
const bot = getMockBot();
|
|
294
|
+
const formatter = vi.spyOn(telegramHtml, 'markdownToTelegramHtml').mockImplementationOnce(()=>{
|
|
295
|
+
throw new Error('formatter failed');
|
|
296
|
+
});
|
|
297
|
+
await adapter.sendMessage('12345', '- agent\n - Status');
|
|
298
|
+
expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);
|
|
299
|
+
expect(bot.telegram.sendMessage.mock.calls[0][0]).toBe('12345');
|
|
300
|
+
expect(bot.telegram.sendMessage.mock.calls[0][1]).toBe('- agent\n - Status');
|
|
301
|
+
expect(bot.telegram.sendMessage.mock.calls[0][2]).toBeUndefined();
|
|
302
|
+
formatter.mockRestore();
|
|
303
|
+
});
|
|
213
304
|
it('should propagate non-parse-entities errors without falling back', async ()=>{
|
|
214
305
|
const bot = getMockBot();
|
|
215
306
|
const otherError = Object.assign(new Error('403'), {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/__tests__/adapters/TelegramAdapter.test.ts"],"sourcesContent":["import * as telegrafModule from 'telegraf';\nimport { TelegramAdapter } from '../../adapters/TelegramAdapter.js';\nimport type { IncomingMessage } from '../../types.js';\n\n// Mock telegraf\nvi.mock('telegraf', () => {\n const handlers: Record<string, (...args: any[]) => any> = {};\n const mockBot = {\n launch: vi.fn().mockResolvedValue(undefined),\n stop: vi.fn().mockResolvedValue(undefined),\n on: vi.fn((event: string, handler: (...args: any[]) => any) => {\n handlers[event] = handler;\n }),\n telegram: {\n sendMessage: vi.fn().mockResolvedValue(undefined),\n getMe: vi.fn().mockResolvedValue({ username: 'test_bot' }),\n },\n _handlers: handlers,\n _triggerText: async (chatId: number, userId: number, text: string) => {\n const ctx = {\n message: {\n chat: { id: chatId },\n from: { id: userId },\n text,\n date: Math.floor(Date.now() / 1000),\n },\n reply: vi.fn().mockResolvedValue(undefined),\n };\n if (handlers['text']) {\n await handlers['text'](ctx);\n }\n return ctx;\n },\n };\n return {\n Telegraf: vi.fn(function () { return mockBot; }),\n __mockBot: mockBot,\n };\n});\n\nfunction getMockBot() {\n return (telegrafModule as unknown as { __mockBot: ReturnType<typeof vi.fn>['mock'] & Record<string, unknown> }).__mockBot;\n}\n\ndescribe('TelegramAdapter', () => {\n let adapter: TelegramAdapter;\n\n beforeEach(() => {\n vi.clearAllMocks();\n adapter = new TelegramAdapter({ botToken: 'test-token-123' });\n });\n\n describe('type', () => {\n it('should return \"telegram\"', () => {\n expect(adapter.type).toBe('telegram');\n });\n });\n\n describe('start', () => {\n it('should launch the telegraf bot', async () => {\n const bot = getMockBot();\n await adapter.start();\n expect(bot.launch).toHaveBeenCalled();\n });\n });\n\n describe('stop', () => {\n it('should stop the telegraf bot', async () => {\n const bot = getMockBot();\n await adapter.start();\n await adapter.stop();\n expect(bot.stop).toHaveBeenCalled();\n });\n });\n\n describe('onMessage', () => {\n it('should silently ignore messages when no handler is registered', async () => {\n // Don't register a handler\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n // Should not throw or reply\n expect(ctx.reply).not.toHaveBeenCalled();\n });\n\n it('should handle non-Error thrown by handler', async () => {\n const handler = vi.fn().mockRejectedValue('string error');\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n expect(ctx.reply).toHaveBeenCalledWith(\n 'Error processing message: Unknown error'\n );\n });\n\n it('should call handler with IncomingMessage on incoming text', async () => {\n const handler = vi.fn().mockResolvedValue(undefined);\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n await bot._triggerText(12345, 67890, 'hello agent');\n\n expect(handler).toHaveBeenCalledTimes(1);\n const msg: IncomingMessage = handler.mock.calls[0][0];\n expect(msg.channelType).toBe('telegram');\n expect(msg.chatId).toBe('12345');\n expect(msg.userId).toBe('67890');\n expect(msg.text).toBe('hello agent');\n expect(msg.timestamp).toBeInstanceOf(Date);\n });\n\n it('should handle handler errors gracefully', async () => {\n const handler = vi.fn().mockRejectedValue(new Error('handler failed'));\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n // Should not throw, and should reply with error\n expect(ctx.reply).toHaveBeenCalledWith(\n expect.stringContaining('Error processing message')\n );\n });\n });\n\n describe('sendMessage', () => {\n it('should send plain text with parse_mode HTML', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', 'hello from bot');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledWith(\n '12345',\n 'hello from bot',\n { parse_mode: 'HTML' }\n );\n });\n\n it('should render markdown as Telegram HTML', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', '**bold** and *italic* and `code`');\n\n const sent = bot.telegram.sendMessage.mock.calls[0][1];\n expect(sent).toContain('<b>bold</b>');\n expect(sent).toContain('<i>italic</i>');\n expect(sent).toContain('<code>code</code>');\n expect(bot.telegram.sendMessage.mock.calls[0][2]).toEqual({\n parse_mode: 'HTML',\n });\n });\n\n it('should chunk messages exceeding 4096 chars', async () => {\n const bot = getMockBot();\n const line = 'A'.repeat(100) + '\\n';\n const longMessage = line.repeat(50); // 5050 chars\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);\n for (const call of bot.telegram.sendMessage.mock.calls) {\n expect(call[1].length).toBeLessThanOrEqual(4096);\n expect(call[2]).toEqual({ parse_mode: 'HTML' });\n }\n });\n\n it('should hard split at 4096 when no newlines available', async () => {\n const bot = getMockBot();\n const longMessage = 'A'.repeat(5000);\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBe(2);\n expect(bot.telegram.sendMessage.mock.calls[0][1].length).toBe(4096);\n expect(bot.telegram.sendMessage.mock.calls[1][1].length).toBe(904);\n });\n\n it('should prefer paragraph (\\\\n\\\\n) over single \\\\n when chunking', async () => {\n const bot = getMockBot();\n // 4 paragraphs of ~1500 chars each, total > 4096\n const paragraph = 'A'.repeat(1500);\n const message = `${paragraph}\\n\\n${paragraph}\\n\\n${paragraph}\\n\\n${paragraph}`;\n\n await adapter.sendMessage('12345', message);\n\n // First chunk should end at a \\n\\n boundary, not mid-paragraph\n const firstChunk = bot.telegram.sendMessage.mock.calls[0][1];\n expect(firstChunk.endsWith('\\n\\n')).toBe(true);\n });\n\n it('should send short messages in a single call', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', 'short message');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);\n });\n\n it('should retry as plain text when Telegram rejects HTML with parse-entities error', async () => {\n const bot = getMockBot();\n const parseError = Object.assign(new Error('400: Bad Request'), {\n description: \"Bad Request: can't parse entities: Unsupported start tag \\\"foo\\\"\",\n });\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n await adapter.sendMessage('12345', '**hello**');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(2);\n\n // First call: rendered HTML with parse_mode\n const [, htmlChunk, htmlOpts] = bot.telegram.sendMessage.mock.calls[0];\n expect(htmlChunk).toContain('<b>hello</b>');\n expect(htmlOpts).toEqual({ parse_mode: 'HTML' });\n\n // Second call: same content, plain text (tags stripped, no parse_mode)\n const [, plainChunk, plainOpts] = bot.telegram.sendMessage.mock.calls[1];\n expect(plainChunk).toBe('hello');\n expect(plainOpts).toBeUndefined();\n });\n\n it('should detect parse-entities error from \"message\" field too', async () => {\n const bot = getMockBot();\n // Some error shapes carry the marker on `message` rather than `description`\n const parseError = new Error(\"can't parse entities\");\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n await adapter.sendMessage('12345', '**hi**');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(2);\n });\n\n it('should decode HTML entities when falling back to plain text', async () => {\n const bot = getMockBot();\n const parseError = Object.assign(new Error('400'), {\n description: \"Bad Request: can't parse entities\",\n });\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n // Source has chars that escapeHtml encodes; fallback should decode them\n await adapter.sendMessage('12345', 'a < b && c > d');\n\n const [, plainChunk] = bot.telegram.sendMessage.mock.calls[1];\n expect(plainChunk).toContain('a < b && c > d');\n expect(plainChunk).not.toContain('<');\n expect(plainChunk).not.toContain('&');\n });\n\n it('should propagate non-parse-entities errors without falling back', async () => {\n const bot = getMockBot();\n const otherError = Object.assign(new Error('403'), {\n description: 'Forbidden: bot was blocked by the user',\n });\n bot.telegram.sendMessage.mockRejectedValueOnce(otherError);\n\n await expect(adapter.sendMessage('12345', 'hi')).rejects.toBe(otherError);\n\n // Only the HTML attempt should have happened โ no fallback retry\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);\n });\n });\n\n describe('isHealthy', () => {\n it('should return true after start', async () => {\n await adapter.start();\n expect(await adapter.isHealthy()).toBe(true);\n });\n\n it('should return false before start', async () => {\n expect(await adapter.isHealthy()).toBe(false);\n });\n\n it('should return false after stop', async () => {\n await adapter.start();\n await adapter.stop();\n expect(await adapter.isHealthy()).toBe(false);\n });\n });\n});\n"],"names":["telegrafModule","TelegramAdapter","vi","mock","handlers","mockBot","launch","fn","mockResolvedValue","undefined","stop","on","event","handler","telegram","sendMessage","getMe","username","_handlers","_triggerText","chatId","userId","text","ctx","message","chat","id","from","date","Math","floor","Date","now","reply","Telegraf","__mockBot","getMockBot","describe","adapter","beforeEach","clearAllMocks","botToken","it","expect","type","toBe","bot","start","toHaveBeenCalled","not","mockRejectedValue","onMessage","toHaveBeenCalledWith","toHaveBeenCalledTimes","msg","calls","channelType","timestamp","toBeInstanceOf","Error","stringContaining","parse_mode","sent","toContain","toEqual","line","repeat","longMessage","length","toBeGreaterThan","call","toBeLessThanOrEqual","paragraph","firstChunk","endsWith","parseError","Object","assign","description","mockRejectedValueOnce","mockResolvedValueOnce","htmlChunk","htmlOpts","plainChunk","plainOpts","toBeUndefined","otherError","rejects","isHealthy"],"mappings":"AAAA,YAAYA,oBAAoB,WAAW;AAC3C,SAASC,eAAe,QAAQ,oCAAoC;AAGpE,gBAAgB;AAChBC,GAAGC,IAAI,CAAC,YAAY;IAChB,MAAMC,WAAoD,CAAC;IAC3D,MAAMC,UAAU;QACZC,QAAQJ,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;QAClCC,MAAMR,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;QAChCE,IAAIT,GAAGK,EAAE,CAAC,CAACK,OAAeC;YACtBT,QAAQ,CAACQ,MAAM,GAAGC;QACtB;QACAC,UAAU;YACNC,aAAab,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;YACvCO,OAAOd,GAAGK,EAAE,GAAGC,iBAAiB,CAAC;gBAAES,UAAU;YAAW;QAC5D;QACAC,WAAWd;QACXe,cAAc,OAAOC,QAAgBC,QAAgBC;YACjD,MAAMC,MAAM;gBACRC,SAAS;oBACLC,MAAM;wBAAEC,IAAIN;oBAAO;oBACnBO,MAAM;wBAAED,IAAIL;oBAAO;oBACnBC;oB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YAClE,MAAMI,MAAMV;YACZ,MAAMkD,aAAaV,OAAOC,MAAM,CAAC,IAAIlB,MAAM,QAAQ;gBAC/CmB,aAAa;YACjB;YACAhC,IAAIhC,QAAQ,CAACC,WAAW,CAACgE,qBAAqB,CAACO;YAE/C,MAAM3C,OAAOL,QAAQvB,WAAW,CAAC,SAAS,OAAOwE,OAAO,CAAC1C,IAAI,CAACyC;YAE9D,iEAAiE;YACjE3C,OAAOG,IAAIhC,QAAQ,CAACC,WAAW,EAAEsC,qBAAqB,CAAC;QAC3D;IACJ;IAEAhB,SAAS,aAAa;QAClBK,GAAG,kCAAkC;YACjC,MAAMJ,QAAQS,KAAK;YACnBJ,OAAO,MAAML,QAAQkD,SAAS,IAAI3C,IAAI,CAAC;QAC3C;QAEAH,GAAG,oCAAoC;YACnCC,OAAO,MAAML,QAAQkD,SAAS,IAAI3C,IAAI,CAAC;QAC3C;QAEAH,GAAG,kCAAkC;YACjC,MAAMJ,QAAQS,KAAK;YACnB,MAAMT,QAAQ5B,IAAI;YAClBiC,OAAO,MAAML,QAAQkD,SAAS,IAAI3C,IAAI,CAAC;QAC3C;IACJ;AACJ"}
|
|
1
|
+
{"version":3,"sources":["../../../src/__tests__/adapters/TelegramAdapter.test.ts"],"sourcesContent":["import * as telegrafModule from 'telegraf';\nimport * as telegramHtml from '../../utils/telegramHtml.js';\nimport { TelegramAdapter } from '../../adapters/TelegramAdapter.js';\nimport type { IncomingMessage } from '../../types.js';\n\n// Mock telegraf\nvi.mock('telegraf', () => {\n const handlers: Record<string, (...args: any[]) => any> = {};\n const mockBot = {\n launch: vi.fn().mockResolvedValue(undefined),\n stop: vi.fn().mockResolvedValue(undefined),\n on: vi.fn((event: string, handler: (...args: any[]) => any) => {\n handlers[event] = handler;\n }),\n telegram: {\n sendMessage: vi.fn().mockResolvedValue(undefined),\n getMe: vi.fn().mockResolvedValue({ username: 'test_bot' }),\n },\n _handlers: handlers,\n _triggerText: async (chatId: number, userId: number, text: string) => {\n const ctx = {\n message: {\n chat: { id: chatId },\n from: { id: userId },\n text,\n date: Math.floor(Date.now() / 1000),\n },\n reply: vi.fn().mockResolvedValue(undefined),\n };\n if (handlers['text']) {\n await handlers['text'](ctx);\n }\n return ctx;\n },\n };\n return {\n Telegraf: vi.fn(function () { return mockBot; }),\n __mockBot: mockBot,\n };\n});\n\nfunction getMockBot() {\n return (telegrafModule as unknown as { __mockBot: ReturnType<typeof vi.fn>['mock'] & Record<string, unknown> }).__mockBot;\n}\n\nfunction expectHtmlChunkWithinLimit(chunk: string): void {\n expect(chunk.length).toBeLessThanOrEqual(4096);\n expect(chunk).not.toMatch(/&(?:l|lt|g|gt|a|am|amp|q|qu|quo|quot)$/);\n expect((chunk.match(/<pre>/g) ?? []).length).toBe((chunk.match(/<\\/pre>/g) ?? []).length);\n expect((chunk.match(/<code(?: class=\"language-[^\"]+\")?>/g) ?? []).length).toBe((chunk.match(/<\\/code>/g) ?? []).length);\n}\n\ndescribe('TelegramAdapter', () => {\n let adapter: TelegramAdapter;\n\n beforeEach(() => {\n vi.clearAllMocks();\n adapter = new TelegramAdapter({ botToken: 'test-token-123' });\n });\n\n describe('type', () => {\n it('should return \"telegram\"', () => {\n expect(adapter.type).toBe('telegram');\n });\n });\n\n describe('start', () => {\n it('should launch the telegraf bot', async () => {\n const bot = getMockBot();\n await adapter.start();\n expect(bot.launch).toHaveBeenCalled();\n });\n });\n\n describe('stop', () => {\n it('should stop the telegraf bot', async () => {\n const bot = getMockBot();\n await adapter.start();\n await adapter.stop();\n expect(bot.stop).toHaveBeenCalled();\n });\n });\n\n describe('onMessage', () => {\n it('should silently ignore messages when no handler is registered', async () => {\n // Don't register a handler\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n // Should not throw or reply\n expect(ctx.reply).not.toHaveBeenCalled();\n });\n\n it('should handle non-Error thrown by handler', async () => {\n const handler = vi.fn().mockRejectedValue('string error');\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n expect(ctx.reply).toHaveBeenCalledWith(\n 'Error processing message: Unknown error'\n );\n });\n\n it('should call handler with IncomingMessage on incoming text', async () => {\n const handler = vi.fn().mockResolvedValue(undefined);\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n await bot._triggerText(12345, 67890, 'hello agent');\n\n expect(handler).toHaveBeenCalledTimes(1);\n const msg: IncomingMessage = handler.mock.calls[0][0];\n expect(msg.channelType).toBe('telegram');\n expect(msg.chatId).toBe('12345');\n expect(msg.userId).toBe('67890');\n expect(msg.text).toBe('hello agent');\n expect(msg.timestamp).toBeInstanceOf(Date);\n });\n\n it('should handle handler errors gracefully', async () => {\n const handler = vi.fn().mockRejectedValue(new Error('handler failed'));\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n // Should not throw, and should reply with error\n expect(ctx.reply).toHaveBeenCalledWith(\n expect.stringContaining('Error processing message')\n );\n });\n });\n\n describe('sendMessage', () => {\n it('should send plain text with parse_mode HTML', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', 'hello from bot');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledWith(\n '12345',\n 'hello from bot',\n { parse_mode: 'HTML' }\n );\n });\n\n it('should render markdown as Telegram HTML', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', '**bold** and *italic* and `code`');\n\n const sent = bot.telegram.sendMessage.mock.calls[0][1];\n expect(sent).toContain('<b>bold</b>');\n expect(sent).toContain('<i>italic</i>');\n expect(sent).toContain('<code>code</code>');\n expect(bot.telegram.sendMessage.mock.calls[0][2]).toEqual({\n parse_mode: 'HTML',\n });\n });\n\n it('should chunk messages exceeding 4096 chars', async () => {\n const bot = getMockBot();\n const line = 'A'.repeat(100) + '\\n';\n const longMessage = line.repeat(50); // 5050 chars\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);\n for (const call of bot.telegram.sendMessage.mock.calls) {\n expect(call[1].length).toBeLessThanOrEqual(4096);\n expect(call[2]).toEqual({ parse_mode: 'HTML' });\n }\n });\n\n it('should split long fenced code into independently valid HTML code blocks', async () => {\n const bot = getMockBot();\n const codeLine = 'const sample = \"<code>tag</code>\" && value < limit;\\n';\n const longMessage = `Before\\n\\n\\`\\`\\`ts\\n${codeLine.repeat(100)}\\`\\`\\`\\n\\nAfter`;\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);\n for (const call of bot.telegram.sendMessage.mock.calls) {\n const chunk = call[1];\n expect(call[2]).toEqual({ parse_mode: 'HTML' });\n expectHtmlChunkWithinLimit(chunk);\n if (chunk.includes('<pre>')) {\n expect(chunk).toContain('<code class=\"language-ts\">');\n expect(chunk).toContain('<code>tag</code>');\n }\n }\n });\n\n it('should split nested list fenced code without sending partial HTML tags', async () => {\n const bot = getMockBot();\n const codeLine = ' return \"<code>tag</code>\" && input < output;\\n';\n const longMessage = [\n '- parent item',\n ' - child item with code',\n ' ```js',\n codeLine.repeat(100).trimEnd(),\n ' ```',\n '- final item',\n ].join('\\n');\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);\n for (const call of bot.telegram.sendMessage.mock.calls) {\n const chunk = call[1];\n expect(call[2]).toEqual({ parse_mode: 'HTML' });\n expectHtmlChunkWithinLimit(chunk);\n }\n });\n\n it('should split long paragraphs at readable markdown boundaries', async () => {\n const bot = getMockBot();\n const sentence = 'This is a long sentence with enough words to make chunking choose sentence boundaries. ';\n const longMessage = sentence.repeat(80);\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);\n for (const call of bot.telegram.sendMessage.mock.calls) {\n const chunk = call[1];\n expect(call[2]).toEqual({ parse_mode: 'HTML' });\n expectHtmlChunkWithinLimit(chunk);\n expect(chunk.endsWith('.') || chunk.endsWith('.\\n\\n') || chunk.endsWith(' ')).toBe(true);\n }\n });\n\n it('should keep emoji chunks within the configured JavaScript string length limit', async () => {\n const bot = getMockBot();\n const longMessage = `Status ${'๐งช'.repeat(2500)}`;\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);\n for (const call of bot.telegram.sendMessage.mock.calls) {\n expect(call[1].length).toBeLessThanOrEqual(4096);\n expect(call[2]).toEqual({ parse_mode: 'HTML' });\n }\n });\n\n it('should hard split at 4096 when no newlines available', async () => {\n const bot = getMockBot();\n const longMessage = 'A'.repeat(5000);\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBe(2);\n expect(bot.telegram.sendMessage.mock.calls[0][1].length).toBe(4096);\n expect(bot.telegram.sendMessage.mock.calls[1][1].length).toBe(904);\n });\n\n it('should prefer paragraph (\\\\n\\\\n) over single \\\\n when chunking', async () => {\n const bot = getMockBot();\n // 4 paragraphs of ~1500 chars each, total > 4096\n const paragraph = 'A'.repeat(1500);\n const message = `${paragraph}\\n\\n${paragraph}\\n\\n${paragraph}\\n\\n${paragraph}`;\n\n await adapter.sendMessage('12345', message);\n\n // First chunk should end at a \\n\\n boundary, not mid-paragraph\n const firstChunk = bot.telegram.sendMessage.mock.calls[0][1];\n expect(firstChunk).toBe(`${paragraph}\\n\\n${paragraph}`);\n });\n\n it('should send short messages in a single call', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', 'short message');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);\n });\n\n it('should retry as plain text when Telegram rejects HTML with parse-entities error', async () => {\n const bot = getMockBot();\n const parseError = Object.assign(new Error('400: Bad Request'), {\n description: \"Bad Request: can't parse entities: Unsupported start tag \\\"foo\\\"\",\n });\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n await adapter.sendMessage('12345', '**hello**');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(2);\n\n // First call: rendered HTML with parse_mode\n const [, htmlChunk, htmlOpts] = bot.telegram.sendMessage.mock.calls[0];\n expect(htmlChunk).toContain('<b>hello</b>');\n expect(htmlOpts).toEqual({ parse_mode: 'HTML' });\n\n // Second call: same content, plain text (tags stripped, no parse_mode)\n const [, plainChunk, plainOpts] = bot.telegram.sendMessage.mock.calls[1];\n expect(plainChunk).toBe('hello');\n expect(plainOpts).toBeUndefined();\n });\n\n it('should detect parse-entities error from \"message\" field too', async () => {\n const bot = getMockBot();\n // Some error shapes carry the marker on `message` rather than `description`\n const parseError = new Error(\"can't parse entities\");\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n await adapter.sendMessage('12345', '**hi**');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(2);\n });\n\n it('should decode HTML entities when falling back to plain text', async () => {\n const bot = getMockBot();\n const parseError = Object.assign(new Error('400'), {\n description: \"Bad Request: can't parse entities\",\n });\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n // Source has chars that escapeHtml encodes; fallback should decode them\n await adapter.sendMessage('12345', 'a < b && c > d');\n\n const [, plainChunk] = bot.telegram.sendMessage.mock.calls[1];\n expect(plainChunk).toContain('a < b && c > d');\n expect(plainChunk).not.toContain('<');\n expect(plainChunk).not.toContain('&');\n });\n\n it('should deliver nested lists without throwing', async () => {\n const bot = getMockBot();\n\n await adapter.sendMessage('12345', '- agent\\n - Status');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);\n expect(bot.telegram.sendMessage.mock.calls[0][1]).toContain('Status');\n });\n\n it('should send plain text when markdown formatting throws', async () => {\n const bot = getMockBot();\n const formatter = vi.spyOn(telegramHtml, 'markdownToTelegramHtml')\n .mockImplementationOnce(() => {\n throw new Error('formatter failed');\n });\n\n await adapter.sendMessage('12345', '- agent\\n - Status');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);\n expect(bot.telegram.sendMessage.mock.calls[0][0]).toBe('12345');\n expect(bot.telegram.sendMessage.mock.calls[0][1]).toBe('- agent\\n - Status');\n expect(bot.telegram.sendMessage.mock.calls[0][2]).toBeUndefined();\n\n formatter.mockRestore();\n });\n\n it('should propagate non-parse-entities errors without falling back', async () => {\n const bot = getMockBot();\n const otherError = Object.assign(new Error('403'), {\n description: 'Forbidden: bot was blocked by the user',\n });\n bot.telegram.sendMessage.mockRejectedValueOnce(otherError);\n\n await expect(adapter.sendMessage('12345', 'hi')).rejects.toBe(otherError);\n\n // Only the HTML attempt should have happened โ no fallback retry\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);\n });\n });\n\n describe('isHealthy', () => {\n it('should return true after start', async () => {\n await adapter.start();\n expect(await adapter.isHealthy()).toBe(true);\n });\n\n it('should return false before start', async () => {\n expect(await adapter.isHealthy()).toBe(false);\n });\n\n it('should return false after stop', async () => {\n await adapter.start();\n await adapter.stop();\n expect(await adapter.isHealthy()).toBe(false);\n });\n });\n});\n"],"names":["telegrafModule","telegramHtml","TelegramAdapter","vi","mock","handlers","mockBot","launch","fn","mockResolvedValue","undefined","stop","on","event","handler","telegram","sendMessage","getMe","username","_handlers","_triggerText","chatId","userId","text","ctx","message","chat","id","from","date","Math","floor","Date","now","reply","Telegraf","__mockBot","getMockBot","expectHtmlChunkWithinLimit","chunk","expect","length","toBeLessThanOrEqual","not","toMatch","match","toBe","describe","adapter","beforeEach","clearAllMocks","botToken","it","type","bot","start","toHaveBeenCalled","mockRejectedValue","onMessage","toHaveBeenCalledWith","toHaveBeenCalledTimes","msg","calls","channelType","timestamp","toBeInstanceOf","Error","stringContaining","parse_mode","sent","toContain","toEqual","line","repeat","longMessage","toBeGreaterThan","call","codeLine","includes","trimEnd","join","sentence","endsWith","paragraph","firstChunk","parseError","Object","assign","description","mockRejectedValueOnce","mockResolvedValueOnce","htmlChunk","htmlOpts","plainChunk","plainOpts","toBeUndefined","formatter","spyOn","mockImplementationOnce","mockRestore","otherError","rejects","isHealthy"],"mappings":"AAAA,YAAYA,oBAAoB,WAAW;AAC3C,YAAYC,kBAAkB,8BAA8B;AAC5D,SAASC,eAAe,QAAQ,oCAAoC;AAGpE,gBAAgB;AAChBC,GAAGC,IAAI,CAAC,YAAY;IAChB,MAAMC,WAAoD,CAAC;IAC3D,MAAMC,UAAU;QACZC,QAAQJ,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;QAClCC,MAAMR,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;QAChCE,IAAIT,GAAGK,EAAE,CAAC,CAACK,OAAeC;YACtBT,QAAQ,CAACQ,MAAM,GAAGC;QACtB;QACAC,UAAU;YACNC,aAAab,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;YACvCO,OAAOd,GAAGK,EAAE,GAAGC,iBAAiB,CAAC;gBAAES,UAAU;YAAW;QAC5D;QACAC,WAAWd;QACXe,cAAc,OAAOC,QAAgBC,QAAgBC;YACjD,MAAMC,MAAM;gBACRC,SAAS;oBACLC,MAAM;wBAAEC,IAAIN;oBAAO;oBACnBO,MAAM;wBAAED,IAAIL;oBAAO;oBACnBC;oBACAM,MAAMC,KAAKC,KAAK,CAACC,KAAKC,GAAG,KAAK;gBAClC;gBACAC,OAAO/B,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;YACrC;YACA,IAAIL,QAAQ,CAAC,OAAO,EAAE;gBAClB,MAAMA,QAAQ,CAAC,OAAO,CAACmB;YAC3B;YACA,OAAOA;QACX;IACJ;IACA,OAAO;QACHW,UAAUhC,GAAGK,EAAE,CAAC;YAAc,OAAOF;QAAS;QAC9C8B,WAAW9B;IACf;AACJ;AAEA,SAAS+B;IACL,OAAO,AAACrC,eAAwGoC,SAAS;AAC7H;AAEA,SAASE,2BAA2BC,KAAa;IAC7CC,OAAOD,MAAME,MAAM,EAAEC,mBAAmB,C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kC;YACjC,MAAMJ,QAAQO,KAAK;YACnBf,OAAO,MAAMQ,QAAQsD,SAAS,IAAIxD,IAAI,CAAC;QAC3C;QAEAM,GAAG,oCAAoC;YACnCZ,OAAO,MAAMQ,QAAQsD,SAAS,IAAIxD,IAAI,CAAC;QAC3C;QAEAM,GAAG,kCAAkC;YACjC,MAAMJ,QAAQO,KAAK;YACnB,MAAMP,QAAQrC,IAAI;YAClB6B,OAAO,MAAMQ,QAAQsD,SAAS,IAAIxD,IAAI,CAAC;QAC3C;IACJ;AACJ"}
|
|
@@ -33,6 +33,12 @@ describe('markdownToTelegramHtml', ()=>{
|
|
|
33
33
|
expect(out).toContain('โข three');
|
|
34
34
|
expect(out).not.toContain('<ul>');
|
|
35
35
|
});
|
|
36
|
+
it('renders nested lists without throwing', ()=>{
|
|
37
|
+
const out = markdownToTelegramHtml('- agent\n - Status');
|
|
38
|
+
expect(out).toContain('โข agent');
|
|
39
|
+
expect(out).toContain('โข Status');
|
|
40
|
+
expect(out).not.toContain('<ul>');
|
|
41
|
+
});
|
|
36
42
|
it('renders ordered lists with numbers', ()=>{
|
|
37
43
|
const out = markdownToTelegramHtml('1. one\n2. two');
|
|
38
44
|
expect(out).toContain('1. one');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/__tests__/utils/telegramHtml.test.ts"],"sourcesContent":["import { markdownToTelegramHtml } from '../../utils/telegramHtml.js';\n\ndescribe('markdownToTelegramHtml', () => {\n it('renders bold, italic, strikethrough', () => {\n const out = markdownToTelegramHtml('**b** _i_ ~~s~~');\n expect(out).toContain('<b>b</b>');\n expect(out).toContain('<i>i</i>');\n expect(out).toContain('<s>s</s>');\n });\n\n it('renders inline code and fenced code with language', () => {\n const md = 'Run `npm test`\\n\\n```ts\\nconst x = 1;\\n```';\n const out = markdownToTelegramHtml(md);\n expect(out).toContain('<code>npm test</code>');\n expect(out).toContain('<pre><code class=\"language-ts\">const x = 1;</code></pre>');\n });\n\n it('renders links and converts headings to bold', () => {\n const md = '# Title\\n\\nSee [docs](https://x.com).';\n const out = markdownToTelegramHtml(md);\n expect(out).toContain('<b>Title</b>');\n expect(out).toContain('<a href=\"https://x.com\">docs</a>');\n });\n\n it('renders images as alt-text links', () => {\n const out = markdownToTelegramHtml('');\n expect(out).toBe('<a href=\"https://x.com/d.png\">diagram</a>');\n });\n\n it('falls back to URL when image has no alt text', () => {\n const out = markdownToTelegramHtml('');\n expect(out).toContain('<a href=\"https://x.com/d.png\">https://x.com/d.png</a>');\n });\n\n it('renders unordered lists with bullets', () => {\n const out = markdownToTelegramHtml('- one\\n- two\\n- three');\n expect(out).toContain('โข one');\n expect(out).toContain('โข two');\n expect(out).toContain('โข three');\n expect(out).not.toContain('<ul>');\n });\n\n it('renders ordered lists with numbers', () => {\n const out = markdownToTelegramHtml('1. one\\n2. two');\n expect(out).toContain('1. one');\n expect(out).toContain('2. two');\n expect(out).not.toContain('<ol>');\n });\n\n it('renders tables as ASCII inside <pre>', () => {\n const md = '| a | b |\\n|---|---|\\n| 1 | 2 |';\n const out = markdownToTelegramHtml(md);\n expect(out.startsWith('<pre>')).toBe(true);\n expect(out).toContain('a');\n expect(out).toContain('b');\n expect(out).toContain('1');\n expect(out).toContain('2');\n expect(out).not.toContain('<table>');\n });\n\n it('uses <blockquote> for quotes', () => {\n const out = markdownToTelegramHtml('> quoted');\n expect(out).toContain('<blockquote>');\n expect(out).toContain('quoted');\n expect(out).toContain('</blockquote>');\n });\n\n it('escapes HTML special chars in plain text', () => {\n const out = markdownToTelegramHtml('a < b && c > d');\n expect(out).toContain('<');\n expect(out).toContain('&');\n expect(out).toContain('>');\n expect(out).not.toContain(' < ');\n });\n\n it('escapes HTML special chars inside code', () => {\n const out = markdownToTelegramHtml('`<script>`');\n expect(out).toContain('<code><script></code>');\n });\n\n it('strips raw HTML blocks', () => {\n const out = markdownToTelegramHtml('hello\\n\\n<div>raw</div>\\n\\nworld');\n expect(out).not.toContain('<div>');\n expect(out).toContain('hello');\n expect(out).toContain('world');\n });\n\n it('renders horizontal rule as a divider', () => {\n const out = markdownToTelegramHtml('above\\n\\n---\\n\\nbelow');\n expect(out).toContain('above');\n expect(out).toContain('โโโ');\n expect(out).toContain('below');\n });\n\n it('passes plain text through unchanged', () => {\n expect(markdownToTelegramHtml('hello world')).toBe('hello world');\n });\n});\n"],"names":["markdownToTelegramHtml","describe","it","out","expect","toContain","md","toBe","not","startsWith"],"mappings":"AAAA,SAASA,sBAAsB,QAAQ,8BAA8B;AAErEC,SAAS,0BAA0B;IAC/BC,GAAG,uCAAuC;QACtC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,qDAAqD;QACpD,MAAMI,KAAK;QACX,MAAMH,MAAMH,uBAAuBM;QACnCF,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,+CAA+C;QAC9C,MAAMI,KAAK;QACX,MAAMH,MAAMH,uBAAuBM;QACnCF,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,oCAAoC;QACnC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKI,IAAI,CAAC;IACrB;IAEAL,GAAG,gDAAgD;QAC/C,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,wCAAwC;QACvC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKK,GAAG,CAACH,SAAS,CAAC;IAC9B;IAEAH,GAAG,sCAAsC;QACrC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,K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OAAOJ,uBAAuB,gBAAgBO,IAAI,CAAC;IACvD;AACJ"}
|
|
1
|
+
{"version":3,"sources":["../../../src/__tests__/utils/telegramHtml.test.ts"],"sourcesContent":["import { markdownToTelegramHtml } from '../../utils/telegramHtml.js';\n\ndescribe('markdownToTelegramHtml', () => {\n it('renders bold, italic, strikethrough', () => {\n const out = markdownToTelegramHtml('**b** _i_ ~~s~~');\n expect(out).toContain('<b>b</b>');\n expect(out).toContain('<i>i</i>');\n expect(out).toContain('<s>s</s>');\n });\n\n it('renders inline code and fenced code with language', () => {\n const md = 'Run `npm test`\\n\\n```ts\\nconst x = 1;\\n```';\n const out = markdownToTelegramHtml(md);\n expect(out).toContain('<code>npm test</code>');\n expect(out).toContain('<pre><code class=\"language-ts\">const x = 1;</code></pre>');\n });\n\n it('renders links and converts headings to bold', () => {\n const md = '# Title\\n\\nSee [docs](https://x.com).';\n const out = markdownToTelegramHtml(md);\n expect(out).toContain('<b>Title</b>');\n expect(out).toContain('<a href=\"https://x.com\">docs</a>');\n });\n\n it('renders images as alt-text links', () => {\n const out = markdownToTelegramHtml('');\n expect(out).toBe('<a href=\"https://x.com/d.png\">diagram</a>');\n });\n\n it('falls back to URL when image has no alt text', () => {\n const out = markdownToTelegramHtml('');\n expect(out).toContain('<a href=\"https://x.com/d.png\">https://x.com/d.png</a>');\n });\n\n it('renders unordered lists with bullets', () => {\n const out = markdownToTelegramHtml('- one\\n- two\\n- three');\n expect(out).toContain('โข one');\n expect(out).toContain('โข two');\n expect(out).toContain('โข three');\n expect(out).not.toContain('<ul>');\n });\n\n it('renders nested lists without throwing', () => {\n const out = markdownToTelegramHtml('- agent\\n - Status');\n\n expect(out).toContain('โข agent');\n expect(out).toContain('โข Status');\n expect(out).not.toContain('<ul>');\n });\n\n it('renders ordered lists with numbers', () => {\n const out = markdownToTelegramHtml('1. one\\n2. two');\n expect(out).toContain('1. one');\n expect(out).toContain('2. two');\n expect(out).not.toContain('<ol>');\n });\n\n it('renders tables as ASCII inside <pre>', () => {\n const md = '| a | b |\\n|---|---|\\n| 1 | 2 |';\n const out = markdownToTelegramHtml(md);\n expect(out.startsWith('<pre>')).toBe(true);\n expect(out).toContain('a');\n expect(out).toContain('b');\n expect(out).toContain('1');\n expect(out).toContain('2');\n expect(out).not.toContain('<table>');\n });\n\n it('uses <blockquote> for quotes', () => {\n const out = markdownToTelegramHtml('> quoted');\n expect(out).toContain('<blockquote>');\n expect(out).toContain('quoted');\n expect(out).toContain('</blockquote>');\n });\n\n it('escapes HTML special chars in plain text', () => {\n const out = markdownToTelegramHtml('a < b && c > d');\n expect(out).toContain('<');\n expect(out).toContain('&');\n expect(out).toContain('>');\n expect(out).not.toContain(' < ');\n });\n\n it('escapes HTML special chars inside code', () => {\n const out = markdownToTelegramHtml('`<script>`');\n expect(out).toContain('<code><script></code>');\n });\n\n it('strips raw HTML blocks', () => {\n const out = markdownToTelegramHtml('hello\\n\\n<div>raw</div>\\n\\nworld');\n expect(out).not.toContain('<div>');\n expect(out).toContain('hello');\n expect(out).toContain('world');\n });\n\n it('renders horizontal rule as a divider', () => {\n const out = markdownToTelegramHtml('above\\n\\n---\\n\\nbelow');\n expect(out).toContain('above');\n expect(out).toContain('โโโ');\n expect(out).toContain('below');\n });\n\n it('passes plain text through unchanged', () => {\n expect(markdownToTelegramHtml('hello world')).toBe('hello world');\n });\n});\n"],"names":["markdownToTelegramHtml","describe","it","out","expect","toContain","md","toBe","not","startsWith"],"mappings":"AAAA,SAASA,sBAAsB,QAAQ,8BAA8B;AAErEC,SAAS,0BAA0B;IAC/BC,GAAG,uCAAuC;QACtC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,qDAAqD;QACpD,MAAMI,KAAK;QACX,MAAMH,MAAMH,uBAAuBM;QACnCF,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAA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AAgC;QAC/B,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,4CAA4C;QAC3C,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKK,GAAG,CAACH,SAAS,CAAC;IAC9B;IAEAH,GAAG,0CAA0C;QACzC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,0BAA0B;QACzB,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKK,GAAG,CAACH,SAAS,CAAC;QAC1BD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,wCAAwC;QACvC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,uCAAuC;QACtCE,OAAOJ,uBAAuB,gBAAgBO,IAAI,CAAC;IACvD;AACJ"}
|
|
@@ -18,9 +18,8 @@ export declare class TelegramAdapter implements ChannelAdapter {
|
|
|
18
18
|
stop(): Promise<void>;
|
|
19
19
|
/**
|
|
20
20
|
* Input is treated as markdown and rendered as Telegram-compatible HTML.
|
|
21
|
-
* Long messages are chunked
|
|
22
|
-
*
|
|
23
|
-
* mid-tag and produce a partial render in the second chunk.
|
|
21
|
+
* Long messages are chunked as markdown source before rendering so each
|
|
22
|
+
* Telegram HTML payload is independently valid.
|
|
24
23
|
*/
|
|
25
24
|
sendMessage(chatId: string, text: string): Promise<void>;
|
|
26
25
|
onMessage(handler: (msg: IncomingMessage) => Promise<void>): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TelegramAdapter.d.ts","sourceRoot":"","sources":["../../src/adapters/TelegramAdapter.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"TelegramAdapter.d.ts","sourceRoot":"","sources":["../../src/adapters/TelegramAdapter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnD,eAAO,MAAM,qBAAqB,aAAa,CAAC;AAChD,eAAO,MAAM,2BAA2B,OAAO,CAAC;AAShD,MAAM,WAAW,sBAAsB;IACnC,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,qBAAa,eAAgB,YAAW,cAAc;IAClD,QAAQ,CAAC,IAAI,cAAyB;IAEtC,OAAO,CAAC,GAAG,CAAW;IACtB,OAAO,CAAC,cAAc,CAA0D;IAChF,OAAO,CAAC,OAAO,CAAS;gBAEZ,OAAO,EAAE,sBAAsB;IAIrC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAwBtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAK3B;;;;OAIG;IACG,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA4B9D,SAAS,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI;IAI3D,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;CAGtC"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Telegraf } from 'telegraf';
|
|
2
|
+
import { Marked } from 'marked';
|
|
2
3
|
import { markdownToTelegramHtml } from '../utils/telegramHtml.js';
|
|
3
4
|
export const TELEGRAM_CHANNEL_TYPE = 'telegram';
|
|
4
5
|
export const TELEGRAM_MAX_MESSAGE_LENGTH = 4096;
|
|
5
6
|
const TELEGRAM_PARSE_MODE = 'HTML';
|
|
7
|
+
const markdownLexer = new Marked();
|
|
6
8
|
/**
|
|
7
9
|
* Telegram Bot API adapter using telegraf with long polling.
|
|
8
10
|
*/ export class TelegramAdapter {
|
|
@@ -39,22 +41,32 @@ const TELEGRAM_PARSE_MODE = 'HTML';
|
|
|
39
41
|
}
|
|
40
42
|
/**
|
|
41
43
|
* Input is treated as markdown and rendered as Telegram-compatible HTML.
|
|
42
|
-
* Long messages are chunked
|
|
43
|
-
*
|
|
44
|
-
* mid-tag and produce a partial render in the second chunk.
|
|
44
|
+
* Long messages are chunked as markdown source before rendering so each
|
|
45
|
+
* Telegram HTML payload is independently valid.
|
|
45
46
|
*/ async sendMessage(chatId, text) {
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
let chunks;
|
|
48
|
+
try {
|
|
49
|
+
chunks = chunkMarkdownForTelegram(text, TELEGRAM_MAX_MESSAGE_LENGTH);
|
|
50
|
+
} catch {
|
|
51
|
+
for (const chunk of chunkMessage(text, TELEGRAM_MAX_MESSAGE_LENGTH)){
|
|
52
|
+
await this.bot.telegram.sendMessage(chatId, chunk);
|
|
53
|
+
}
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
48
56
|
for (const chunk of chunks){
|
|
57
|
+
if (!chunk.html) {
|
|
58
|
+
await this.bot.telegram.sendMessage(chatId, chunk.text);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
49
61
|
try {
|
|
50
|
-
await this.bot.telegram.sendMessage(chatId, chunk, {
|
|
62
|
+
await this.bot.telegram.sendMessage(chatId, chunk.text, {
|
|
51
63
|
parse_mode: TELEGRAM_PARSE_MODE
|
|
52
64
|
});
|
|
53
65
|
} catch (error) {
|
|
54
66
|
if (!isParseEntitiesError(error)) throw error;
|
|
55
67
|
// Telegram rejected the rendered HTML โ fall back to plain text
|
|
56
68
|
// so the user still gets the content (just unformatted).
|
|
57
|
-
await this.bot.telegram.sendMessage(chatId, htmlToPlainText(chunk));
|
|
69
|
+
await this.bot.telegram.sendMessage(chatId, htmlToPlainText(chunk.text));
|
|
58
70
|
}
|
|
59
71
|
}
|
|
60
72
|
}
|
|
@@ -74,6 +86,224 @@ function isParseEntitiesError(error) {
|
|
|
74
86
|
function htmlToPlainText(html) {
|
|
75
87
|
return html.replace(/<[^>]+>/g, '').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/&/g, '&');
|
|
76
88
|
}
|
|
89
|
+
function chunkMarkdownForTelegram(markdown, maxLen) {
|
|
90
|
+
const markdownChunks = splitMarkdownSource(markdown, maxLen);
|
|
91
|
+
const chunks = [];
|
|
92
|
+
for (const markdownChunk of markdownChunks){
|
|
93
|
+
const html = markdownToTelegramHtml(markdownChunk);
|
|
94
|
+
if (html.length <= maxLen) {
|
|
95
|
+
if (html.length > 0) chunks.push({
|
|
96
|
+
text: html,
|
|
97
|
+
html: true
|
|
98
|
+
});
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
for (const plainChunk of chunkMessage(markdownChunk, maxLen)){
|
|
102
|
+
if (plainChunk.length > 0) chunks.push({
|
|
103
|
+
text: plainChunk,
|
|
104
|
+
html: false
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return chunks;
|
|
109
|
+
}
|
|
110
|
+
function splitMarkdownSource(markdown, maxLen, depth = 0) {
|
|
111
|
+
if (markdown.length === 0) return [];
|
|
112
|
+
if (renderedLengthFits(markdown, maxLen)) return [
|
|
113
|
+
markdown
|
|
114
|
+
];
|
|
115
|
+
if (depth > 6) return splitPlainMarkdownText(markdown, maxLen);
|
|
116
|
+
const chunks = [];
|
|
117
|
+
let current = '';
|
|
118
|
+
const tokens = markdownLexer.lexer(markdown);
|
|
119
|
+
for (const token of tokens){
|
|
120
|
+
const raw = token.raw ?? '';
|
|
121
|
+
if (raw.length === 0) continue;
|
|
122
|
+
const candidate = current + raw;
|
|
123
|
+
if (candidate.length > 0 && renderedLengthFits(candidate, maxLen)) {
|
|
124
|
+
current = candidate;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (current.length > 0) {
|
|
128
|
+
chunks.push(current);
|
|
129
|
+
current = '';
|
|
130
|
+
}
|
|
131
|
+
if (renderedLengthFits(raw, maxLen)) {
|
|
132
|
+
current = raw;
|
|
133
|
+
} else {
|
|
134
|
+
chunks.push(...splitOversizedToken(token, maxLen, depth + 1));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (current.length > 0) chunks.push(current);
|
|
138
|
+
return chunks.flatMap((chunk)=>renderedLengthFits(chunk, maxLen) ? [
|
|
139
|
+
chunk
|
|
140
|
+
] : splitPlainMarkdownText(chunk, maxLen));
|
|
141
|
+
}
|
|
142
|
+
function splitOversizedToken(token, maxLen, depth) {
|
|
143
|
+
switch(token.type){
|
|
144
|
+
case 'code':
|
|
145
|
+
return splitCodeToken(token, maxLen);
|
|
146
|
+
case 'list':
|
|
147
|
+
return splitListToken(token, maxLen, depth);
|
|
148
|
+
case 'paragraph':
|
|
149
|
+
case 'text':
|
|
150
|
+
return splitPlainMarkdownText(token.raw, maxLen);
|
|
151
|
+
default:
|
|
152
|
+
if ('tokens' in token && Array.isArray(token.tokens) && token.raw !== undefined) {
|
|
153
|
+
return splitMarkdownSource(token.raw, maxLen, depth);
|
|
154
|
+
}
|
|
155
|
+
return splitPlainMarkdownText(token.raw ?? '', maxLen);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function splitListToken(token, maxLen, depth) {
|
|
159
|
+
const chunks = [];
|
|
160
|
+
let current = '';
|
|
161
|
+
for (const item of token.items){
|
|
162
|
+
const raw = item.raw;
|
|
163
|
+
const candidate = current + raw;
|
|
164
|
+
if (candidate.length > 0 && renderedLengthFits(candidate, maxLen)) {
|
|
165
|
+
current = candidate;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (current.length > 0) {
|
|
169
|
+
chunks.push(current);
|
|
170
|
+
current = '';
|
|
171
|
+
}
|
|
172
|
+
if (renderedLengthFits(raw, maxLen)) {
|
|
173
|
+
current = raw;
|
|
174
|
+
} else {
|
|
175
|
+
chunks.push(...splitMarkdownSource(raw, maxLen, depth + 1));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (current.length > 0) chunks.push(current);
|
|
179
|
+
return chunks;
|
|
180
|
+
}
|
|
181
|
+
function splitCodeToken(token, maxLen) {
|
|
182
|
+
const fence = token.raw.startsWith('~~~') ? '~~~' : '```';
|
|
183
|
+
const lang = token.lang ? token.lang.split(/\s/)[0] : '';
|
|
184
|
+
const lines = token.text.split('\n');
|
|
185
|
+
const chunks = [];
|
|
186
|
+
let currentLines = [];
|
|
187
|
+
const renderFence = (codeLines)=>`${fence}${lang}\n${codeLines.join('\n')}\n${fence}\n\n`;
|
|
188
|
+
for (const line of lines){
|
|
189
|
+
const candidateLines = [
|
|
190
|
+
...currentLines,
|
|
191
|
+
line
|
|
192
|
+
];
|
|
193
|
+
if (renderedLengthFits(renderFence(candidateLines), maxLen)) {
|
|
194
|
+
currentLines = candidateLines;
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (currentLines.length > 0) {
|
|
198
|
+
chunks.push(renderFence(currentLines));
|
|
199
|
+
currentLines = [];
|
|
200
|
+
}
|
|
201
|
+
if (renderedLengthFits(renderFence([
|
|
202
|
+
line
|
|
203
|
+
]), maxLen)) {
|
|
204
|
+
currentLines = [
|
|
205
|
+
line
|
|
206
|
+
];
|
|
207
|
+
} else {
|
|
208
|
+
for (const segment of splitCodeLine(line, fence, lang, maxLen)){
|
|
209
|
+
chunks.push(renderFence([
|
|
210
|
+
segment
|
|
211
|
+
]));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (currentLines.length > 0) chunks.push(renderFence(currentLines));
|
|
216
|
+
return chunks;
|
|
217
|
+
}
|
|
218
|
+
function splitCodeLine(line, fence, lang, maxLen) {
|
|
219
|
+
const segments = [];
|
|
220
|
+
let current = '';
|
|
221
|
+
const renderFence = (value)=>`${fence}${lang}\n${value}\n${fence}\n\n`;
|
|
222
|
+
for (const char of Array.from(line)){
|
|
223
|
+
const candidate = current + char;
|
|
224
|
+
if (renderedLengthFits(renderFence(candidate), maxLen)) {
|
|
225
|
+
current = candidate;
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (current.length > 0) {
|
|
229
|
+
segments.push(current);
|
|
230
|
+
current = '';
|
|
231
|
+
}
|
|
232
|
+
if (renderedLengthFits(renderFence(char), maxLen)) {
|
|
233
|
+
current = char;
|
|
234
|
+
} else {
|
|
235
|
+
segments.push(char);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (current.length > 0) segments.push(current);
|
|
239
|
+
return segments;
|
|
240
|
+
}
|
|
241
|
+
function splitPlainMarkdownText(markdown, maxLen) {
|
|
242
|
+
if (markdown.length === 0) return [];
|
|
243
|
+
if (renderedLengthFits(markdown, maxLen)) return [
|
|
244
|
+
markdown
|
|
245
|
+
];
|
|
246
|
+
const newlineUnits = markdown.split(/(?<=\n)/u);
|
|
247
|
+
if (newlineUnits.length > 1) {
|
|
248
|
+
return packMarkdownUnits(newlineUnits, maxLen, splitPlainMarkdownText);
|
|
249
|
+
}
|
|
250
|
+
const sentenceUnits = markdown.match(/[^.!?]+[.!?]+(?:\s+|$)|[^.!?]+$/gu);
|
|
251
|
+
if (sentenceUnits && sentenceUnits.length > 1) {
|
|
252
|
+
return packMarkdownUnits(sentenceUnits, maxLen, splitPlainMarkdownText);
|
|
253
|
+
}
|
|
254
|
+
const wordUnits = markdown.match(/\S+\s*/gu);
|
|
255
|
+
if (wordUnits && wordUnits.length > 1) {
|
|
256
|
+
return packMarkdownUnits(wordUnits, maxLen, splitPlainMarkdownText);
|
|
257
|
+
}
|
|
258
|
+
return splitByCodePoint(markdown, maxLen);
|
|
259
|
+
}
|
|
260
|
+
function packMarkdownUnits(units, maxLen, splitOversized) {
|
|
261
|
+
const chunks = [];
|
|
262
|
+
let current = '';
|
|
263
|
+
for (const unit of units){
|
|
264
|
+
const candidate = current + unit;
|
|
265
|
+
if (renderedLengthFits(candidate, maxLen)) {
|
|
266
|
+
current = candidate;
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (current.length > 0) {
|
|
270
|
+
chunks.push(current);
|
|
271
|
+
current = '';
|
|
272
|
+
}
|
|
273
|
+
if (renderedLengthFits(unit, maxLen)) {
|
|
274
|
+
current = unit;
|
|
275
|
+
} else {
|
|
276
|
+
chunks.push(...splitOversized(unit, maxLen));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (current.length > 0) chunks.push(current);
|
|
280
|
+
return chunks;
|
|
281
|
+
}
|
|
282
|
+
function splitByCodePoint(text, maxLen) {
|
|
283
|
+
const chunks = [];
|
|
284
|
+
let current = '';
|
|
285
|
+
for (const char of Array.from(text)){
|
|
286
|
+
const candidate = current + char;
|
|
287
|
+
if (renderedLengthFits(candidate, maxLen)) {
|
|
288
|
+
current = candidate;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (current.length > 0) {
|
|
292
|
+
chunks.push(current);
|
|
293
|
+
current = '';
|
|
294
|
+
}
|
|
295
|
+
if (renderedLengthFits(char, maxLen)) {
|
|
296
|
+
current = char;
|
|
297
|
+
} else {
|
|
298
|
+
chunks.push(char);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (current.length > 0) chunks.push(current);
|
|
302
|
+
return chunks;
|
|
303
|
+
}
|
|
304
|
+
function renderedLengthFits(markdown, maxLen) {
|
|
305
|
+
return markdownToTelegramHtml(markdown).length <= maxLen;
|
|
306
|
+
}
|
|
77
307
|
/**
|
|
78
308
|
* Split text into chunks of maxLen or fewer characters. Prefers paragraph
|
|
79
309
|
* boundaries (\n\n), then single newlines (\n), then hard-splits at maxLen.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/adapters/TelegramAdapter.ts"],"sourcesContent":["import { Telegraf } from 'telegraf';\nimport type { ChannelAdapter } from './ChannelAdapter.js';\nimport { markdownToTelegramHtml } from '../utils/telegramHtml.js';\nimport type { IncomingMessage } from '../types.js';\n\nexport const TELEGRAM_CHANNEL_TYPE = 'telegram';\nexport const TELEGRAM_MAX_MESSAGE_LENGTH = 4096;\nconst TELEGRAM_PARSE_MODE = 'HTML' as const;\n\nexport interface TelegramAdapterOptions {\n botToken: string;\n}\n\n/**\n * Telegram Bot API adapter using telegraf with long polling.\n */\nexport class TelegramAdapter implements ChannelAdapter {\n readonly type = TELEGRAM_CHANNEL_TYPE;\n\n private bot: Telegraf;\n private messageHandler: ((msg: IncomingMessage) => Promise<void>) | null = null;\n private running = false;\n\n constructor(options: TelegramAdapterOptions) {\n this.bot = new Telegraf(options.botToken);\n }\n\n async start(): Promise<void> {\n this.bot.on('text', async (ctx) => {\n if (!this.messageHandler) return;\n\n const msg: IncomingMessage = {\n channelType: TELEGRAM_CHANNEL_TYPE,\n chatId: String(ctx.message.chat.id),\n userId: String(ctx.message.from.id),\n text: ctx.message.text,\n timestamp: new Date(ctx.message.date * 1000),\n };\n\n try {\n await this.messageHandler(msg);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error';\n await ctx.reply(`Error processing message: ${errorMessage}`);\n }\n });\n\n await this.bot.launch();\n this.running = true;\n }\n\n async stop(): Promise<void> {\n this.running = false;\n await this.bot.stop();\n }\n\n /**\n * Input is treated as markdown and rendered as Telegram-compatible HTML.\n * Long messages are chunked at paragraph boundaries when possible; very\n * long single blocks (e.g. a `<pre>` over 4096 chars) may still split\n * mid-tag and produce a partial render in the second chunk.\n */\n async sendMessage(chatId: string, text: string): Promise<void> {\n const html = markdownToTelegramHtml(text);\n const chunks = chunkMessage(html, TELEGRAM_MAX_MESSAGE_LENGTH);\n for (const chunk of chunks) {\n try {\n await this.bot.telegram.sendMessage(chatId, chunk, { parse_mode: TELEGRAM_PARSE_MODE });\n } catch (error) {\n if (!isParseEntitiesError(error)) throw error;\n // Telegram rejected the rendered HTML โ fall back to plain text\n // so the user still gets the content (just unformatted).\n await this.bot.telegram.sendMessage(chatId, htmlToPlainText(chunk));\n }\n }\n }\n\n onMessage(handler: (msg: IncomingMessage) => Promise<void>): void {\n this.messageHandler = handler;\n }\n\n async isHealthy(): Promise<boolean> {\n return this.running;\n }\n}\n\nfunction isParseEntitiesError(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false;\n const description = (error as { description?: string }).description;\n const message = (error as { message?: string }).message;\n return ((description ?? '') + (message ?? '')).includes(\"can't parse entities\");\n}\n\nfunction htmlToPlainText(html: string): string {\n return html\n .replace(/<[^>]+>/g, '')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/"/g, '\"')\n .replace(/&/g, '&');\n}\n\n/**\n * Split text into chunks of maxLen or fewer characters. Prefers paragraph\n * boundaries (\\n\\n), then single newlines (\\n), then hard-splits at maxLen.\n */\nfunction chunkMessage(text: string, maxLen: number): string[] {\n const chunks: string[] = [];\n let remaining = text;\n\n while (remaining.length > 0) {\n if (remaining.length <= maxLen) {\n chunks.push(remaining);\n break;\n }\n\n const lastParagraph = remaining.lastIndexOf('\\n\\n', maxLen - 2);\n const lastNewline = remaining.lastIndexOf('\\n', maxLen - 1);\n\n let splitAt: number;\n if (lastParagraph > 0) {\n splitAt = lastParagraph + 2;\n } else if (lastNewline > 0) {\n splitAt = lastNewline + 1;\n } else {\n splitAt = maxLen;\n }\n\n chunks.push(remaining.slice(0, splitAt));\n remaining = remaining.slice(splitAt);\n }\n\n return chunks;\n}\n"],"names":["Telegraf","markdownToTelegramHtml","TELEGRAM_CHANNEL_TYPE","TELEGRAM_MAX_MESSAGE_LENGTH","TELEGRAM_PARSE_MODE","TelegramAdapter","type","bot","messageHandler","running","options","botToken","start","on","ctx","msg","channelType","chatId","String","message","chat","id","userId","from","text","timestamp","Date","date","error","errorMessage","Error","reply","launch","stop","sendMessage","html","chunks","chunkMessage","chunk","telegram","parse_mode","isParseEntitiesError","htmlToPlainText","onMessage","handler","isHealthy","description","includes","replace","maxLen","remaining","length","push","lastParagraph","lastIndexOf","lastNewline","splitAt","slice"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,WAAW;AAEpC,SAASC,sBAAsB,QAAQ,2BAA2B;AAGlE,OAAO,MAAMC,wBAAwB,WAAW;AAChD,OAAO,MAAMC,8BAA8B,KAAK;AAChD,MAAMC,sBAAsB;AAM5B;;CAEC,GACD,OAAO,MAAMC;IACAC,OAAOJ,sBAAsB;IAE9BK,IAAc;IACdC,iBAAmE,KAAK;IACxEC,UAAU,MAAM;IAExB,YAAYC,OAA+B,CAAE;QACzC,IAAI,CAACH,GAAG,GAAG,IAAIP,SAASU,QAAQC,QAAQ;IAC5C;IAEA,MAAMC,QAAuB;QACzB,IAAI,CAACL,GAAG,CAACM,EAAE,CAAC,QAAQ,OAAOC;YACvB,IAAI,CAAC,IAAI,CAACN,cAAc,EAAE;YAE1B,MAAMO,MAAuB;gBACzBC,aAAad;gBACbe,QAAQC,OAAOJ,IAAIK,OAAO,CAACC,IAAI,CAACC,EAAE;gBAClCC,QAAQJ,OAAOJ,IAAIK,OAAO,CAACI,IAAI,CAACF,EAAE;gBAClCG,MAAMV,IAAIK,OAAO,CAACK,IAAI;gBACtBC,WAAW,IAAIC,KAAKZ,IAAIK,OAAO,CAACQ,IAAI,GAAG;YAC3C;YAEA,IAAI;gBACA,MAAM,IAAI,CAACnB,cAAc,CAACO;YAC9B,EAAE,OAAOa,OAAO;gBACZ,MAAMC,eAAeD,iBAAiBE,QAAQF,MAAMT,OAAO,GAAG;gBAC9D,MAAML,IAAIiB,KAAK,CAAC,CAAC,0BAA0B,EAAEF,cAAc;YAC/D;QACJ;QAEA,MAAM,IAAI,CAACtB,GAAG,CAACyB,MAAM;QACrB,IAAI,CAACvB,OAAO,GAAG;IACnB;IAEA,MAAMwB,OAAsB;QACxB,IAAI,CAACxB,OAAO,GAAG;QACf,MAAM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AAUC,MAAM,GAAG,EAAG;QACzB,IAAID,UAAUC,MAAM,IAAIF,QAAQ;YAC5Bb,OAAOgB,IAAI,CAACF;YACZ;QACJ;QAEA,MAAMG,gBAAgBH,UAAUI,WAAW,CAAC,QAAQL,SAAS;QAC7D,MAAMM,cAAcL,UAAUI,WAAW,CAAC,MAAML,SAAS;QAEzD,IAAIO;QACJ,IAAIH,gBAAgB,GAAG;YACnBG,UAAUH,gBAAgB;QAC9B,OAAO,IAAIE,cAAc,GAAG;YACxBC,UAAUD,cAAc;QAC5B,OAAO;YACHC,UAAUP;QACd;QAEAb,OAAOgB,IAAI,CAACF,UAAUO,KAAK,CAAC,GAAGD;QAC/BN,YAAYA,UAAUO,KAAK,CAACD;IAChC;IAEA,OAAOpB;AACX"}
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/TelegramAdapter.ts"],"sourcesContent":["import { Telegraf } from 'telegraf';\nimport { Marked, type Token, type Tokens } from 'marked';\nimport type { ChannelAdapter } from './ChannelAdapter.js';\nimport { markdownToTelegramHtml } from '../utils/telegramHtml.js';\nimport type { IncomingMessage } from '../types.js';\n\nexport const TELEGRAM_CHANNEL_TYPE = 'telegram';\nexport const TELEGRAM_MAX_MESSAGE_LENGTH = 4096;\nconst TELEGRAM_PARSE_MODE = 'HTML' as const;\nconst markdownLexer = new Marked();\n\ntype TelegramMessageChunk = {\n text: string;\n html: boolean;\n};\n\nexport interface TelegramAdapterOptions {\n botToken: string;\n}\n\n/**\n * Telegram Bot API adapter using telegraf with long polling.\n */\nexport class TelegramAdapter implements ChannelAdapter {\n readonly type = TELEGRAM_CHANNEL_TYPE;\n\n private bot: Telegraf;\n private messageHandler: ((msg: IncomingMessage) => Promise<void>) | null = null;\n private running = false;\n\n constructor(options: TelegramAdapterOptions) {\n this.bot = new Telegraf(options.botToken);\n }\n\n async start(): Promise<void> {\n this.bot.on('text', async (ctx) => {\n if (!this.messageHandler) return;\n\n const msg: IncomingMessage = {\n channelType: TELEGRAM_CHANNEL_TYPE,\n chatId: String(ctx.message.chat.id),\n userId: String(ctx.message.from.id),\n text: ctx.message.text,\n timestamp: new Date(ctx.message.date * 1000),\n };\n\n try {\n await this.messageHandler(msg);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error';\n await ctx.reply(`Error processing message: ${errorMessage}`);\n }\n });\n\n await this.bot.launch();\n this.running = true;\n }\n\n async stop(): Promise<void> {\n this.running = false;\n await this.bot.stop();\n }\n\n /**\n * Input is treated as markdown and rendered as Telegram-compatible HTML.\n * Long messages are chunked as markdown source before rendering so each\n * Telegram HTML payload is independently valid.\n */\n async sendMessage(chatId: string, text: string): Promise<void> {\n let chunks: TelegramMessageChunk[];\n try {\n chunks = chunkMarkdownForTelegram(text, TELEGRAM_MAX_MESSAGE_LENGTH);\n } catch {\n for (const chunk of chunkMessage(text, TELEGRAM_MAX_MESSAGE_LENGTH)) {\n await this.bot.telegram.sendMessage(chatId, chunk);\n }\n return;\n }\n\n for (const chunk of chunks) {\n if (!chunk.html) {\n await this.bot.telegram.sendMessage(chatId, chunk.text);\n continue;\n }\n\n try {\n await this.bot.telegram.sendMessage(chatId, chunk.text, { parse_mode: TELEGRAM_PARSE_MODE });\n } catch (error) {\n if (!isParseEntitiesError(error)) throw error;\n // Telegram rejected the rendered HTML โ fall back to plain text\n // so the user still gets the content (just unformatted).\n await this.bot.telegram.sendMessage(chatId, htmlToPlainText(chunk.text));\n }\n }\n }\n\n onMessage(handler: (msg: IncomingMessage) => Promise<void>): void {\n this.messageHandler = handler;\n }\n\n async isHealthy(): Promise<boolean> {\n return this.running;\n }\n}\n\nfunction isParseEntitiesError(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false;\n const description = (error as { description?: string }).description;\n const message = (error as { message?: string }).message;\n return ((description ?? '') + (message ?? '')).includes(\"can't parse entities\");\n}\n\nfunction htmlToPlainText(html: string): string {\n return html\n .replace(/<[^>]+>/g, '')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/"/g, '\"')\n .replace(/&/g, '&');\n}\n\nfunction chunkMarkdownForTelegram(markdown: string, maxLen: number): TelegramMessageChunk[] {\n const markdownChunks = splitMarkdownSource(markdown, maxLen);\n const chunks: TelegramMessageChunk[] = [];\n\n for (const markdownChunk of markdownChunks) {\n const html = markdownToTelegramHtml(markdownChunk);\n if (html.length <= maxLen) {\n if (html.length > 0) chunks.push({ text: html, html: true });\n continue;\n }\n\n for (const plainChunk of chunkMessage(markdownChunk, maxLen)) {\n if (plainChunk.length > 0) chunks.push({ text: plainChunk, html: false });\n }\n }\n\n return chunks;\n}\n\nfunction splitMarkdownSource(markdown: string, maxLen: number, depth = 0): string[] {\n if (markdown.length === 0) return [];\n if (renderedLengthFits(markdown, maxLen)) return [markdown];\n if (depth > 6) return splitPlainMarkdownText(markdown, maxLen);\n\n const chunks: string[] = [];\n let current = '';\n const tokens = markdownLexer.lexer(markdown);\n\n for (const token of tokens) {\n const raw = token.raw ?? '';\n if (raw.length === 0) continue;\n\n const candidate = current + raw;\n if (candidate.length > 0 && renderedLengthFits(candidate, maxLen)) {\n current = candidate;\n continue;\n }\n\n if (current.length > 0) {\n chunks.push(current);\n current = '';\n }\n\n if (renderedLengthFits(raw, maxLen)) {\n current = raw;\n } else {\n chunks.push(...splitOversizedToken(token, maxLen, depth + 1));\n }\n }\n\n if (current.length > 0) chunks.push(current);\n return chunks.flatMap((chunk) => renderedLengthFits(chunk, maxLen) ? [chunk] : splitPlainMarkdownText(chunk, maxLen));\n}\n\nfunction splitOversizedToken(token: Token, maxLen: number, depth: number): string[] {\n switch (token.type) {\n case 'code':\n return splitCodeToken(token as Tokens.Code, maxLen);\n case 'list':\n return splitListToken(token as Tokens.List, maxLen, depth);\n case 'paragraph':\n case 'text':\n return splitPlainMarkdownText(token.raw, maxLen);\n default:\n if ('tokens' in token && Array.isArray(token.tokens) && token.raw !== undefined) {\n return splitMarkdownSource(token.raw, maxLen, depth);\n }\n return splitPlainMarkdownText(token.raw ?? '', maxLen);\n }\n}\n\nfunction splitListToken(token: Tokens.List, maxLen: number, depth: number): string[] {\n const chunks: string[] = [];\n let current = '';\n\n for (const item of token.items) {\n const raw = item.raw;\n const candidate = current + raw;\n if (candidate.length > 0 && renderedLengthFits(candidate, maxLen)) {\n current = candidate;\n continue;\n }\n\n if (current.length > 0) {\n chunks.push(current);\n current = '';\n }\n\n if (renderedLengthFits(raw, maxLen)) {\n current = raw;\n } else {\n chunks.push(...splitMarkdownSource(raw, maxLen, depth + 1));\n }\n }\n\n if (current.length > 0) chunks.push(current);\n return chunks;\n}\n\nfunction splitCodeToken(token: Tokens.Code, maxLen: number): string[] {\n const fence = token.raw.startsWith('~~~') ? '~~~' : '```';\n const lang = token.lang ? token.lang.split(/\\s/)[0] : '';\n const lines = token.text.split('\\n');\n const chunks: string[] = [];\n let currentLines: string[] = [];\n\n const renderFence = (codeLines: string[]): string =>\n `${fence}${lang}\\n${codeLines.join('\\n')}\\n${fence}\\n\\n`;\n\n for (const line of lines) {\n const candidateLines = [...currentLines, line];\n if (renderedLengthFits(renderFence(candidateLines), maxLen)) {\n currentLines = candidateLines;\n continue;\n }\n\n if (currentLines.length > 0) {\n chunks.push(renderFence(currentLines));\n currentLines = [];\n }\n\n if (renderedLengthFits(renderFence([line]), maxLen)) {\n currentLines = [line];\n } else {\n for (const segment of splitCodeLine(line, fence, lang, maxLen)) {\n chunks.push(renderFence([segment]));\n }\n }\n }\n\n if (currentLines.length > 0) chunks.push(renderFence(currentLines));\n return chunks;\n}\n\nfunction splitCodeLine(line: string, fence: string, lang: string, maxLen: number): string[] {\n const segments: string[] = [];\n let current = '';\n const renderFence = (value: string): string => `${fence}${lang}\\n${value}\\n${fence}\\n\\n`;\n\n for (const char of Array.from(line)) {\n const candidate = current + char;\n if (renderedLengthFits(renderFence(candidate), maxLen)) {\n current = candidate;\n continue;\n }\n\n if (current.length > 0) {\n segments.push(current);\n current = '';\n }\n\n if (renderedLengthFits(renderFence(char), maxLen)) {\n current = char;\n } else {\n segments.push(char);\n }\n }\n\n if (current.length > 0) segments.push(current);\n return segments;\n}\n\nfunction splitPlainMarkdownText(markdown: string, maxLen: number): string[] {\n if (markdown.length === 0) return [];\n if (renderedLengthFits(markdown, maxLen)) return [markdown];\n\n const newlineUnits = markdown.split(/(?<=\\n)/u);\n if (newlineUnits.length > 1) {\n return packMarkdownUnits(newlineUnits, maxLen, splitPlainMarkdownText);\n }\n\n const sentenceUnits = markdown.match(/[^.!?]+[.!?]+(?:\\s+|$)|[^.!?]+$/gu);\n if (sentenceUnits && sentenceUnits.length > 1) {\n return packMarkdownUnits(sentenceUnits, maxLen, splitPlainMarkdownText);\n }\n\n const wordUnits = markdown.match(/\\S+\\s*/gu);\n if (wordUnits && wordUnits.length > 1) {\n return packMarkdownUnits(wordUnits, maxLen, splitPlainMarkdownText);\n }\n\n return splitByCodePoint(markdown, maxLen);\n}\n\nfunction packMarkdownUnits(\n units: string[],\n maxLen: number,\n splitOversized: (unit: string, maxLen: number) => string[],\n): string[] {\n const chunks: string[] = [];\n let current = '';\n\n for (const unit of units) {\n const candidate = current + unit;\n if (renderedLengthFits(candidate, maxLen)) {\n current = candidate;\n continue;\n }\n\n if (current.length > 0) {\n chunks.push(current);\n current = '';\n }\n\n if (renderedLengthFits(unit, maxLen)) {\n current = unit;\n } else {\n chunks.push(...splitOversized(unit, maxLen));\n }\n }\n\n if (current.length > 0) chunks.push(current);\n return chunks;\n}\n\nfunction splitByCodePoint(text: string, maxLen: number): string[] {\n const chunks: string[] = [];\n let current = '';\n\n for (const char of Array.from(text)) {\n const candidate = current + char;\n if (renderedLengthFits(candidate, maxLen)) {\n current = candidate;\n continue;\n }\n\n if (current.length > 0) {\n chunks.push(current);\n current = '';\n }\n\n if (renderedLengthFits(char, maxLen)) {\n current = char;\n } else {\n chunks.push(char);\n }\n }\n\n if (current.length > 0) chunks.push(current);\n return chunks;\n}\n\nfunction renderedLengthFits(markdown: string, maxLen: number): boolean {\n return markdownToTelegramHtml(markdown).length <= maxLen;\n}\n\n/**\n * Split text into chunks of maxLen or fewer characters. Prefers paragraph\n * boundaries (\\n\\n), then single newlines (\\n), then hard-splits at maxLen.\n */\nfunction chunkMessage(text: string, maxLen: number): string[] {\n const chunks: string[] = [];\n let remaining = text;\n\n while (remaining.length > 0) {\n if (remaining.length <= maxLen) {\n chunks.push(remaining);\n break;\n }\n\n const lastParagraph = remaining.lastIndexOf('\\n\\n', maxLen - 2);\n const lastNewline = remaining.lastIndexOf('\\n', maxLen - 1);\n\n let splitAt: number;\n if (lastParagraph > 0) {\n splitAt = lastParagraph + 2;\n } else if (lastNewline > 0) {\n splitAt = lastNewline + 1;\n } else {\n splitAt = maxLen;\n }\n\n chunks.push(remaining.slice(0, splitAt));\n remaining = remaining.slice(splitAt);\n }\n\n return chunks;\n}\n"],"names":["Telegraf","Marked","markdownToTelegramHtml","TELEGRAM_CHANNEL_TYPE","TELEGRAM_MAX_MESSAGE_LENGTH","TELEGRAM_PARSE_MODE","markdownLexer","TelegramAdapter","type","bot","messageHandler","running","options","botToken","start","on","ctx","msg","channelType","chatId","String","message","chat","id","userId","from","text","timestamp","Date","date","error","errorMessage","Error","reply","launch","stop","sendMessage","chunks","chunkMarkdownForTelegram","chunk","chunkMessage","telegram","html","parse_mode","isParseEntitiesError","htmlToPlainText","onMessage","handler","isHealthy","description","includes","replace","markdown","maxLen","markdownChunks","splitMarkdownSource","markdownChunk","length","push","plainChunk","depth","renderedLengthFits","splitPlainMarkdownText","current","tokens","lexer","token","raw","candidate","splitOversizedToken","flatMap","splitCodeToken","splitListToken","Array","isArray","undefined","item","items","fence","startsWith","lang","split","lines","currentLines","renderFence","codeLines","join","line","candidateLines","segment","splitCodeLine","segments","value","char","newlineUnits","packMarkdownUnits","sentenceUnits","match","wordUnits","splitByCodePoint","units","splitOversized","unit","remaining","lastParagraph","lastIndexOf","lastNewline","splitAt","slice"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,WAAW;AACpC,SAASC,MAAM,QAAiC,SAAS;AAEzD,SAASC,sBA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OAAOqB,IAAI,CAAC6C;YACZ;QACJ;QAEA,MAAMC,gBAAgBD,UAAUE,WAAW,CAAC,QAAQpD,SAAS;QAC7D,MAAMqD,cAAcH,UAAUE,WAAW,CAAC,MAAMpD,SAAS;QAEzD,IAAIsD;QACJ,IAAIH,gBAAgB,GAAG;YACnBG,UAAUH,gBAAgB;QAC9B,OAAO,IAAIE,cAAc,GAAG;YACxBC,UAAUD,cAAc;QAC5B,OAAO;YACHC,UAAUtD;QACd;QAEAhB,OAAOqB,IAAI,CAAC6C,UAAUK,KAAK,CAAC,GAAGD;QAC/BJ,YAAYA,UAAUK,KAAK,CAACD;IAChC;IAEA,OAAOtE;AACX"}
|
|
@@ -35,7 +35,7 @@ const renderer = {
|
|
|
35
35
|
const startNum = typeof start === 'number' ? start : 1;
|
|
36
36
|
const lines = items.map((item, i)=>{
|
|
37
37
|
const marker = ordered ? `${startNum + i}.` : 'โข';
|
|
38
|
-
const inner = this.parser.
|
|
38
|
+
const inner = this.parser.parse(item.tokens).trimEnd();
|
|
39
39
|
return `${marker} ${inner}`;
|
|
40
40
|
});
|
|
41
41
|
return `${lines.join('\n')}\n\n`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/telegramHtml.ts"],"sourcesContent":["import { Marked, type RendererObject, type Tokens } from 'marked';\n\nconst escapeHtml = (s: string): string =>\n s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');\n\nconst escapeUrl = (s: string): string =>\n s.replace(/&/g, '&').replace(/\"/g, '"');\n\nconst renderer: RendererObject = {\n heading({ tokens }: Tokens.Heading): string {\n return `<b>${this.parser.parseInline(tokens)}</b>\\n\\n`;\n },\n paragraph({ tokens }: Tokens.Paragraph): string {\n return `${this.parser.parseInline(tokens)}\\n\\n`;\n },\n strong({ tokens }: Tokens.Strong): string {\n return `<b>${this.parser.parseInline(tokens)}</b>`;\n },\n em({ tokens }: Tokens.Em): string {\n return `<i>${this.parser.parseInline(tokens)}</i>`;\n },\n del({ tokens }: Tokens.Del): string {\n return `<s>${this.parser.parseInline(tokens)}</s>`;\n },\n codespan({ text }: Tokens.Codespan): string {\n return `<code>${escapeHtml(text)}</code>`;\n },\n code({ text, lang }: Tokens.Code): string {\n const langAttr = lang ? ` class=\"language-${escapeHtml(lang.split(/\\s/)[0])}\"` : '';\n return `<pre><code${langAttr}>${escapeHtml(text)}</code></pre>\\n\\n`;\n },\n link({ href, tokens }: Tokens.Link): string {\n return `<a href=\"${escapeUrl(href)}\">${this.parser.parseInline(tokens)}</a>`;\n },\n image({ href, text }: Tokens.Image): string {\n const label = text || href;\n return `<a href=\"${escapeUrl(href)}\">${escapeHtml(label)}</a>`;\n },\n list({ items, ordered, start }: Tokens.List): string {\n const startNum = typeof start === 'number' ? start : 1;\n const lines = items.map((item, i) => {\n const marker = ordered ? `${startNum + i}.` : 'โข';\n const inner = this.parser.
|
|
1
|
+
{"version":3,"sources":["../../src/utils/telegramHtml.ts"],"sourcesContent":["import { Marked, type RendererObject, type Tokens } from 'marked';\n\nconst escapeHtml = (s: string): string =>\n s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');\n\nconst escapeUrl = (s: string): string =>\n s.replace(/&/g, '&').replace(/\"/g, '"');\n\nconst renderer: RendererObject = {\n heading({ tokens }: Tokens.Heading): string {\n return `<b>${this.parser.parseInline(tokens)}</b>\\n\\n`;\n },\n paragraph({ tokens }: Tokens.Paragraph): string {\n return `${this.parser.parseInline(tokens)}\\n\\n`;\n },\n strong({ tokens }: Tokens.Strong): string {\n return `<b>${this.parser.parseInline(tokens)}</b>`;\n },\n em({ tokens }: Tokens.Em): string {\n return `<i>${this.parser.parseInline(tokens)}</i>`;\n },\n del({ tokens }: Tokens.Del): string {\n return `<s>${this.parser.parseInline(tokens)}</s>`;\n },\n codespan({ text }: Tokens.Codespan): string {\n return `<code>${escapeHtml(text)}</code>`;\n },\n code({ text, lang }: Tokens.Code): string {\n const langAttr = lang ? ` class=\"language-${escapeHtml(lang.split(/\\s/)[0])}\"` : '';\n return `<pre><code${langAttr}>${escapeHtml(text)}</code></pre>\\n\\n`;\n },\n link({ href, tokens }: Tokens.Link): string {\n return `<a href=\"${escapeUrl(href)}\">${this.parser.parseInline(tokens)}</a>`;\n },\n image({ href, text }: Tokens.Image): string {\n const label = text || href;\n return `<a href=\"${escapeUrl(href)}\">${escapeHtml(label)}</a>`;\n },\n list({ items, ordered, start }: Tokens.List): string {\n const startNum = typeof start === 'number' ? start : 1;\n const lines = items.map((item, i) => {\n const marker = ordered ? `${startNum + i}.` : 'โข';\n const inner = this.parser.parse(item.tokens).trimEnd();\n return `${marker} ${inner}`;\n });\n return `${lines.join('\\n')}\\n\\n`;\n },\n listitem(item: Tokens.ListItem): string {\n return this.parser.parseInline(item.tokens);\n },\n blockquote({ tokens }: Tokens.Blockquote): string {\n return `<blockquote>${this.parser.parse(tokens).trimEnd()}</blockquote>\\n\\n`;\n },\n table({ header, rows }: Tokens.Table): string {\n const headerCells = header.map((c) => c.text);\n const bodyRows = rows.map((row) => row.map((c) => c.text));\n const widths = headerCells.map((h, i) =>\n Math.max(h.length, ...bodyRows.map((row) => (row[i] ?? '').length))\n );\n const pad = (cells: string[]) =>\n cells.map((c, i) => c.padEnd(widths[i] ?? 0)).join(' | ');\n\n const lines = [\n pad(headerCells),\n widths.map((w) => '-'.repeat(w)).join('-+-'),\n ...bodyRows.map(pad),\n ];\n return `<pre>${escapeHtml(lines.join('\\n'))}</pre>\\n\\n`;\n },\n hr(): string {\n return 'โโโ\\n\\n';\n },\n br(): string {\n return '\\n';\n },\n html(): string {\n return '';\n },\n text(token: Tokens.Text | Tokens.Escape): string {\n if ('tokens' in token && token.tokens) {\n return this.parser.parseInline(token.tokens);\n }\n return escapeHtml(token.text);\n },\n};\n\nconst marked = new Marked({ renderer });\n\n/**\n * Convert markdown to Telegram-compatible HTML (uses only the tag subset\n * Telegram's Bot API accepts for parse_mode: 'HTML').\n */\nexport function markdownToTelegramHtml(markdown: string): string {\n return (marked.parse(markdown, { async: false }) as string).trimEnd();\n}\n"],"names":["Marked","escapeHtml","s","replace","escapeUrl","renderer","heading","tokens","parser","parseInline","paragraph","strong","em","del","codespan","text","code","lang","langAttr","split","link","href","image","label","list","items","ordered","start","startNum","lines","map","item","i","marker","inner","parse","trimEnd","join","listitem","blockquote","table","header","rows","headerCells","c","bodyRows","row","widths","h","Math","max","length","pad","cells","padEnd","w","repeat","hr","br","html","token","marked","markdownToTelegramHtml","markdown","async"],"mappings":"AAAA,SAASA,MAAM,QAA0C,SAAS;AAElE,MAAMC,aAAa,CAACC,IAChBA,EAAEC,OAAO,CAAC,MAAM,SAASA,OAAO,CAAC,MAAM,QAAQA,OAAO,CAAC,MAAM;AAEjE,MAAMC,YAAY,CAACF,IACfA,EAAEC,OAAO,CAAC,MAAM,SAASA,OAAO,CAAC,MAAM;AAE3C,MAAME,WAA2B;IAC7BC,SAAQ,EAAEC,MAAM,EAAkB;QAC9B,OAAO,CAAC,GAAG,EAAE,IAAI,CAACC,MAAM,CAACC,WAAW,CAACF,QAAQ,QAAQ,CAAC;IAC1D;IACAG,WAAU,EAAEH,MAAM,EAAoB;QAClC,OAAO,GAAG,IAAI,CAACC,MAAM,CAACC,WAAW,CAACF,QAAQ,IAAI,CAAC;IACnD;IACAI,QAAO,EAAEJ,MAAM,EAAiB;QAC5B,OAAO,CAAC,GAAG,EAAE,IAAI,CAACC,MAAM,CAACC,WAAW,CAACF,QAAQ,IAAI,CAAC;IACtD;IACAK,IAAG,EAAEL,MAAM,EAAa;QACpB,OAAO,CAAC,GAAG,EAAE,IAAI,CAACC,MAAM,CAACC,WAAW,CAACF,QAAQ,IAAI,CAAC;IACtD;IACAM,KAAI,EAAEN,MAAM,EAAc;QACtB,OAAO,CAAC,GAAG,EAAE,IAAI,CAACC,MAAM,CAACC,WAAW,CAACF,QAAQ,IAAI,CAAC;IACtD;IACAO,UAAS,EAAEC,IAAI,EAAmB;QAC9B,OAAO,CAAC,MAAM,EAAEd,WAAWc,MA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