ForcePilot/backend/package/yuxi/channels/adapters/whatsapp/send.py
Kris e9b57546ea feat(whatsapp): 新增WhatsApp适配器完整功能模块
新增大量WhatsApp适配器相关代码,包括账号管理、会话处理、消息收发、验证授权、媒体处理、互动命令、审批流程、健康检测等完整功能模块,搭建基础的Baileys协议WhatsApp接入能力
2026-05-12 00:51:58 +08:00

98 lines
2.4 KiB
Python

from __future__ import annotations
from yuxi.channels.models import DeliveryResult
async def send_formatted_text(
bridge,
jid: str,
content: str,
reply_to: str | None = None,
) -> DeliveryResult:
return await bridge.send_message(
jid=jid,
content=content,
reply_to=reply_to,
)
async def send_formatted_media(
bridge,
jid: str,
media_type: str,
media_path: str,
caption: str = "",
reply_to: str | None = None,
) -> DeliveryResult:
return await bridge.send_media(
jid=jid,
media_type=media_type,
media_path=media_path,
caption=caption,
reply_to=reply_to,
)
async def send_chunked_text(
bridge,
jid: str,
chunks: list[str],
reply_to: str | None = None,
) -> list[DeliveryResult]:
results = []
for chunk in chunks:
result = await bridge.send_message(
jid=jid,
content=chunk,
reply_to=reply_to if len(results) == 0 else None,
)
results.append(result)
return results
def chunk_message(content: str, chunk_limit: int = 4000, mode: str = "length") -> list[str]:
if len(content) <= chunk_limit:
return [content]
if mode == "newline":
return _chunk_by_newline(content, chunk_limit)
return _chunk_by_length(content, chunk_limit)
def _chunk_by_length(content: str, chunk_limit: int) -> list[str]:
chunks: list[str] = []
start = 0
while start < len(content):
end = min(start + chunk_limit, len(content))
if end < len(content):
last_space = content.rfind(" ", start, end)
last_newline = content.rfind("\n", start, end)
boundary = max(last_space, last_newline)
if boundary > start:
end = boundary + 1
chunks.append(content[start:end])
start = end
return chunks
def _chunk_by_newline(content: str, chunk_limit: int) -> list[str]:
lines = content.split("\n")
chunks: list[str] = []
current = ""
for line in lines:
candidate = f"{current}\n{line}" if current else line
if len(candidate) > chunk_limit:
if current:
chunks.append(current)
current = line
else:
chunks.append(line)
current = ""
else:
current = candidate
if current:
chunks.append(current)
return chunks