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

187 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from typing import Any
WHATSAPP_MESSAGE_ACTIONS: dict[str, dict[str, Any]] = {
"send": {
"status": "implemented",
"description": "发送文本消息,支持 Markdown 转换和长文本自动分块",
"api": "sendMessage",
},
"sendAttachment": {
"status": "implemented",
"description": "发送图片/视频/音频/文件/贴纸,通过 DSM API 上传",
"api": "sendMessage + media",
},
"sendPoll": {
"status": "implemented",
"description": "创建投票(最多 12 选项),基于 Baileys poll 原生支持",
"api": "sendMessage + poll",
},
"sendSticker": {
"status": "implemented",
"description": "发送贴纸,通过专用 API 发送 WebP 格式贴纸",
"api": "sendSticker",
},
"sendLocation": {
"status": "implemented",
"description": "发送地理位置消息,支持经纬度和地点名称",
"api": "sendLocation",
},
"sendContact": {
"status": "implemented",
"description": "发送联系人名片,支持 vCard 格式",
"api": "sendContact",
},
"reply": {
"status": "implemented",
"description": "引用回复消息,通过 reply_to 参数绑定",
"api": "sendMessage + context",
},
"edit": {
"status": "unsupported",
"description": "WhatsApp 不支持消息编辑",
"api": "N/A",
},
"delete": {
"status": "implemented",
"description": "通过 Baileys Bridge revoke 实现消息删除",
"api": "sendMessage + delete",
},
"react": {
"status": "implemented",
"description": "发送 emoji 反应消息",
"api": "sendReaction",
},
"pin": {
"status": "unsupported",
"description": "WhatsApp 不支持消息置顶",
"api": "N/A",
},
"unpin": {
"status": "unsupported",
"description": "WhatsApp 不支持取消置顶",
"api": "N/A",
},
"broadcast": {
"status": "planned",
"description": "群发消息WhatsApp 限制较多",
"api": "sendMessageTemplate",
},
"typing": {
"status": "implemented",
"description": "发送 '正在输入' 指示",
"api": "markAsRead + typing indicator",
},
"streamChunk": {
"status": "implemented",
"description": "流式输出块,通过 typing indicator + 最终发送实现",
"api": "sendMessage (streaming via edit)",
},
"thread": {
"status": "unsupported",
"description": "WhatsApp 不支持线程",
"api": "N/A",
},
"kick": {
"status": "unsupported",
"description": "WhatsApp 不支持踢人(仅 Cloud API 群管理支持)",
"api": "N/A (Cloud API group admin)",
},
"ban": {
"status": "unsupported",
"description": "WhatsApp 不支持封禁",
"api": "N/A",
},
"mute": {
"status": "unsupported",
"description": "WhatsApp 不支持禁言",
"api": "N/A",
},
"leave": {
"status": "unsupported",
"description": "Bot 不支持主动退群",
"api": "N/A",
},
"addParticipant": {
"status": "unsupported",
"description": "WhatsApp 不支持通过 Bot 添加成员",
"api": "N/A",
},
"removeParticipant": {
"status": "unsupported",
"description": "WhatsApp 不支持通过 Bot 移除成员",
"api": "N/A",
},
"downloadFile": {
"status": "implemented",
"description": "下载媒体文件",
"api": "retrieveMediaUrl + download",
},
"uploadFile": {
"status": "implemented",
"description": "上传媒体文件",
"api": "uploadMedia + sendMessage",
},
"getContact": {
"status": "implemented",
"description": "获取联系人信息",
"api": "contact lookup",
},
}
_WHATSAPP_ACTION_MAPPING: dict[str, str] = {
"send": "send",
"sendAttachment": "send_attachment",
"sendPoll": "create_poll",
"sendSticker": "send_sticker",
"sendLocation": "send_location",
"sendContact": "send_contact",
"reply": "reply",
"edit": "edit",
"delete": "delete",
"react": "react",
"pin": "pin",
"unpin": "unpin",
"broadcast": "broadcast",
"streamChunk": "stream",
"thread": "thread_create",
"kick": "kick",
"ban": "ban",
"mute": "mute",
"leave": "leave_group",
"addParticipant": "add_participant",
"removeParticipant": "remove_participant",
"downloadFile": "download_file",
"uploadFile": "upload_file",
"getContact": "member_info",
}
def register_whatsapp_actions() -> None:
from yuxi.channels.message_actions import ActionDeclaration, ActionRegistry, ActionStatus, MessageAction
declarations: dict[MessageAction, ActionDeclaration] = {}
for action_name, info in WHATSAPP_MESSAGE_ACTIONS.items():
mapped = _WHATSAPP_ACTION_MAPPING.get(action_name)
if mapped is None:
continue
try:
action_enum = MessageAction(mapped)
except ValueError:
continue
status_str = info.get("status", "unsupported")
status = {
"implemented": ActionStatus.SUPPORTED,
"planned": ActionStatus.PARTIAL,
"unsupported": ActionStatus.UNSUPPORTED,
}.get(status_str, ActionStatus.UNSUPPORTED)
declarations[action_enum] = ActionDeclaration(
action=action_enum,
status=status,
reason=info.get("description", ""),
impl=info.get("api", ""),
)
ActionRegistry.register_channel_actions("whatsapp", declarations)