-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
43 lines (31 loc) · 1.22 KB
/
utils.py
File metadata and controls
43 lines (31 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from typing import List
# Telegram message limit is 4096 characters
TELEGRAM_MAX_MESSAGE_LENGTH = 4096
def split_message(text: str, max_length: int = TELEGRAM_MAX_MESSAGE_LENGTH) -> List[str]:
"""
Split a long message into chunks that fit within Telegram's message limit.
Tries to split at paragraph boundaries when possible.
"""
if len(text) <= max_length:
return [text]
chunks: List[str] = []
remaining = text
while len(remaining) > max_length:
# Try to split at a double newline (paragraph break)
split_pos = remaining.rfind("\n\n", 0, max_length)
# If no paragraph break, try single newline
if split_pos == -1:
split_pos = remaining.rfind("\n", 0, max_length)
# If no newline, split at word boundary
if split_pos == -1:
split_pos = remaining.rfind(" ", 0, max_length)
# If no space, force split at max_length
if split_pos == -1:
split_pos = max_length
chunk = remaining[:split_pos].strip()
if chunk:
chunks.append(chunk)
remaining = remaining[split_pos:].strip()
if remaining:
chunks.append(remaining)
return chunks