feat: add telegram message splitting utility

This commit is contained in:
neken
2026-04-10 15:11:18 +05:00
parent 9d409be5b9
commit 79bcf2ff72
+36
View File
@@ -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;
}