Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as telegrafModule from 'telegraf';
import * as telegramHtml from '../../utils/telegramHtml.js';
import { TelegramAdapter } from '../../adapters/TelegramAdapter.js';
import type { IncomingMessage } from '../../types.js';

Expand Down Expand Up @@ -255,6 +256,32 @@ describe('TelegramAdapter', () => {
expect(plainChunk).not.toContain('&');
});

it('should deliver nested lists without throwing', async () => {
const bot = getMockBot();

await adapter.sendMessage('12345', '- agent\n - Status');

expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);
expect(bot.telegram.sendMessage.mock.calls[0][1]).toContain('Status');
});

it('should send plain text when markdown formatting throws', async () => {
const bot = getMockBot();
const formatter = vi.spyOn(telegramHtml, 'markdownToTelegramHtml')
.mockImplementationOnce(() => {
throw new Error('formatter failed');
});

await adapter.sendMessage('12345', '- agent\n - Status');

expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);
expect(bot.telegram.sendMessage.mock.calls[0][0]).toBe('12345');
expect(bot.telegram.sendMessage.mock.calls[0][1]).toBe('- agent\n - Status');
expect(bot.telegram.sendMessage.mock.calls[0][2]).toBeUndefined();

formatter.mockRestore();
});

it('should propagate non-parse-entities errors without falling back', async () => {
const bot = getMockBot();
const otherError = Object.assign(new Error('403'), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ describe('markdownToTelegramHtml', () => {
expect(out).not.toContain('<ul>');
});

it('renders nested lists without throwing', () => {
const out = markdownToTelegramHtml('- agent\n - Status');

expect(out).toContain('• agent');
expect(out).toContain('• Status');
expect(out).not.toContain('<ul>');
});

it('renders ordered lists with numbers', () => {
const out = markdownToTelegramHtml('1. one\n2. two');
expect(out).toContain('1. one');
Expand Down
14 changes: 11 additions & 3 deletions packages/channel-connector/src/adapters/TelegramAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,17 @@ export class TelegramAdapter implements ChannelAdapter {
* mid-tag and produce a partial render in the second chunk.
*/
async sendMessage(chatId: string, text: string): Promise<void> {
const html = markdownToTelegramHtml(text);
const chunks = chunkMessage(html, TELEGRAM_MAX_MESSAGE_LENGTH);
for (const chunk of chunks) {
let html: string;
try {
html = markdownToTelegramHtml(text);
} catch {
for (const chunk of chunkMessage(text, TELEGRAM_MAX_MESSAGE_LENGTH)) {
await this.bot.telegram.sendMessage(chatId, chunk);
}
return;
}

for (const chunk of chunkMessage(html, TELEGRAM_MAX_MESSAGE_LENGTH)) {
try {
await this.bot.telegram.sendMessage(chatId, chunk, { parse_mode: TELEGRAM_PARSE_MODE });
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/channel-connector/src/utils/telegramHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const renderer: RendererObject = {
const startNum = typeof start === 'number' ? start : 1;
const lines = items.map((item, i) => {
const marker = ordered ? `${startNum + i}.` : '•';
const inner = this.parser.parseInline(item.tokens).trimEnd();
const inner = this.parser.parse(item.tokens).trimEnd();
return `${marker} ${inner}`;
});
return `${lines.join('\n')}\n\n`;
Expand Down
Loading