新增 Zalo OA 官方账号完整集成能力,包含: 1. 基础通信能力:消息编解码、目标归一化、文本分块 2. 安全与校验:Webhook 签名验证、DM 策略管理、配对流程 3. 辅助工具:重复事件去重、请求限流、异常告警 4. 管理功能:账号多实例管理、配置验证、健康诊断 5. 扩展能力:媒体托管、视觉识别、TTS 语音合成 6. 运维支持:审计日志、状态监控、目录同步
53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
from __future__ import annotations
|
||
|
||
import re
|
||
|
||
_PARAGRAPH_BOUNDARY = re.compile(r"\n\s*\n")
|
||
_SENTENCE_BOUNDARY = re.compile(r"(?<=[.!?。!?])\s+")
|
||
_CHUNK_BOUNDARY = re.compile(r"[\n,,;;]+")
|
||
|
||
|
||
def chunk_text(text: str, max_chars: int) -> list[str]:
|
||
if len(text) <= max_chars:
|
||
return [text]
|
||
|
||
chunks: list[str] = []
|
||
remaining = text
|
||
|
||
while len(remaining) > max_chars:
|
||
split_point = _find_split_point(remaining, max_chars)
|
||
chunk = remaining[:split_point].strip()
|
||
remaining = remaining[split_point:].strip()
|
||
if chunk:
|
||
chunks.append(chunk)
|
||
|
||
if remaining:
|
||
chunks.append(remaining)
|
||
|
||
if not chunks:
|
||
chunks.append(text[:max_chars])
|
||
|
||
return chunks
|
||
|
||
|
||
def _find_split_point(text: str, max_chars: int) -> int:
|
||
head = text[:max_chars]
|
||
|
||
para_matches = list(_PARAGRAPH_BOUNDARY.finditer(head))
|
||
if para_matches:
|
||
return para_matches[-1].start()
|
||
|
||
sent_matches = list(_SENTENCE_BOUNDARY.finditer(head))
|
||
if sent_matches:
|
||
return sent_matches[-1].end()
|
||
|
||
chunk_matches = list(_CHUNK_BOUNDARY.finditer(head))
|
||
if chunk_matches:
|
||
return chunk_matches[-1].start()
|
||
|
||
space_match = head.rfind(" ")
|
||
if space_match > max_chars // 2:
|
||
return space_match
|
||
|
||
return max_chars
|