feat: add telegram message splitting utility
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user