From 79bcf2ff727a493c3db2617c7fdd6c351874891a Mon Sep 17 00:00:00 2001 From: neken Date: Fri, 10 Apr 2026 15:11:18 +0500 Subject: [PATCH] feat: add telegram message splitting utility --- src/utils/telegram.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/utils/telegram.ts diff --git a/src/utils/telegram.ts b/src/utils/telegram.ts new file mode 100644 index 0000000..0af8e26 --- /dev/null +++ b/src/utils/telegram.ts @@ -0,0 +1,36 @@ +const MAX_LENGTH = 4096; + +export function splitMessage(text: string): string[] { + if (text.length <= MAX_LENGTH) return [text]; + + const chunks: string[] = []; + let remaining = text; + + while (remaining.length > 0) { + if (remaining.length <= MAX_LENGTH) { + chunks.push(remaining); + break; + } + + // Try to split at paragraph boundary + let splitAt = remaining.lastIndexOf("\n\n", MAX_LENGTH); + if (splitAt === -1 || splitAt < MAX_LENGTH / 2) { + // Try line boundary + splitAt = remaining.lastIndexOf("\n", MAX_LENGTH); + } + if (splitAt === -1 || splitAt < MAX_LENGTH / 2) { + // Hard split + splitAt = MAX_LENGTH; + } + + chunks.push(remaining.slice(0, splitAt)); + remaining = remaining.slice(splitAt).trimStart(); + } + + return chunks; +} + +export function escapeMarkdown(text: string): string { + // grammy supports Markdown parse mode — minimal escaping needed + return text; +}