Skip to content
Open
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
44 changes: 43 additions & 1 deletion packages/channel-connector/src/adapters/TelegramAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export class TelegramAdapter implements ChannelAdapter {
async sendMessage(chatId: string, text: string): Promise<void> {
const chunks = chunkMessage(text, TELEGRAM_MAX_MESSAGE_LENGTH);
for (const chunk of chunks) {
await this.bot.telegram.sendMessage(chatId, chunk);
const html = markdownToHtml(chunk);
await this.bot.telegram.sendMessage(chatId, html, { parse_mode: 'HTML' });
}
}

Expand Down Expand Up @@ -108,3 +109,44 @@ function chunkMessage(text: string, maxLen: number): string[] {

return chunks;
}

function escapeHtml(text: string): string {
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

/**
* Convert standard Markdown to Telegram HTML.
* Handles code blocks first to prevent formatting inside them.
*/
function markdownToHtml(text: string): string {
const codeBlocks: string[] = [];
const inlineCodes: string[] = [];

let result = text.replace(/```(\w*)\n?([\s\S]*?)```/g, (_, lang, code) => {
const escaped = escapeHtml(code.trimEnd());
const block = lang
? `<pre><code class="language-${lang}">${escaped}</code></pre>`
: `<pre><code>${escaped}</code></pre>`;
codeBlocks.push(block);
return `\x00CODE${codeBlocks.length - 1}\x00`;
});

result = result.replace(/`([^`]+)`/g, (_, code) => {
inlineCodes.push(`<code>${escapeHtml(code)}</code>`);
return `\x00INLINE${inlineCodes.length - 1}\x00`;
});

result = escapeHtml(result);

result = result
.replace(/\*\*(.+?)\*\*/g, '<b>$1</b>')
.replace(/__(.+?)__/g, '<b>$1</b>')
.replace(/\*(.+?)\*/g, '<i>$1</i>')
.replace(/_(.+?)_/g, '<i>$1</i>')
.replace(/~~(.+?)~~/g, '<s>$1</s>');

result = result.replace(/\x00CODE(\d+)\x00/g, (_, i) => codeBlocks[parseInt(i)]);
result = result.replace(/\x00INLINE(\d+)\x00/g, (_, i) => inlineCodes[parseInt(i)]);

return result;
}
23 changes: 21 additions & 2 deletions packages/cli/src/commands/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,23 @@ function setupInputHandler(
});
}

const SYSTEM_TAGS = [
'command-name',
'command-message',
'command-args',
'local-command-stdout',
'system-reminder',
'user-prompt-submit-hook',
];

function stripSystemTags(content: string): string {
let result = content;
for (const tag of SYSTEM_TAGS) {
result = result.replace(new RegExp(`<${tag}[^>]*>[\\s\\S]*?<\\/${tag}>`, 'g'), '');
}
return result.trim();
}

function startOutputPolling(
telegram: TelegramAdapter,
agentAdapter: AgentAdapter,
Expand Down Expand Up @@ -137,8 +154,10 @@ function startOutputPolling(

for (const msg of newMessages) {
if (msg.role !== 'user' && msg.content) {
await telegram.sendMessage(chatIdRef.value, msg.content);
debug(`Sent agent response to Telegram (role: ${msg.role}, length: ${msg.content.length})`);
const cleaned = stripSystemTags(msg.content);
if (!cleaned) continue;
await telegram.sendMessage(chatIdRef.value, cleaned);
debug(`Sent agent response to Telegram (role: ${msg.role}, length: ${cleaned.length})`);
}
}
} catch {
Expand Down