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; +}