ForcePilot/backend/package/yuxi/channel/extensions/alipay/format.py
Kris 71364ea579 feat(alipay): 新增支付宝渠道插件完整实现
实现了支付宝生活号渠道的完整功能,包括消息接收回调、发送、安全验证、去重、配对绑定、流式回复支持等完整能力。
2026-05-21 10:39:23 +08:00

35 lines
1.2 KiB
Python

import re
def remove_markdown(text: str) -> str:
text = re.sub(r"\*\*(.+?)\*\*", r"\1", text)
text = re.sub(r"\*(.+?)\*", r"\1", text)
text = re.sub(r"__(.+?)__", r"\1", text)
text = re.sub(r"_(.+?)_", r"\1", text)
text = re.sub(r"~~(.+?)~~", r"\1", text)
text = re.sub(r"```[\s\S]*?```", "", text)
text = re.sub(r"`{1,3}[^`]*`{1,3}", "", text)
text = re.sub(r"\[([^\]]*)\]\([^)]*\)", r"\1", text)
text = re.sub(r"!\[.*?\]\(.*?\)", "[图片]", text)
text = re.sub(r"^#+ (.*?)$", r"\1", text, flags=re.MULTILINE)
text = re.sub(r"^[*-] (.*?)$", r"· \1", text, flags=re.MULTILINE)
text = re.sub(r"^> (.*?)$", r" \1", text, flags=re.MULTILINE)
text = re.sub(r"\n{3,}", "\n\n", text)
return text.strip()
def split_utf8_safe(text: str, max_len: int) -> list[str]:
if len(text) <= max_len:
return [text]
chunks = []
while text:
if len(text) <= max_len:
chunks.append(text)
break
split_at = text.rfind("\n", 0, max_len)
if split_at == -1 or split_at < max_len // 2:
split_at = max_len
chunks.append(text[:split_at])
text = text[split_at:].lstrip()
return chunks