ForcePilot/backend/package/yuxi/channels/adapters/wechat/message_actions.py
Kris 05abecc02b feat(wechat): 新增完整微信渠道适配器实现
该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块:
1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等
2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等
3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力
4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等
5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等

实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
2026-05-12 00:51:04 +08:00

181 lines
9.1 KiB
Python

from __future__ import annotations
from collections.abc import Awaitable, Callable
from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from yuxi.channels.message_actions import ActionStatus, MessageAction
WECHAT_MESSAGE_ACTIONS: dict[str, dict[str, Any]] = {
"send": {"status": "implemented", "impl": "adapter.send()"},
"broadcast": {"status": "unsupported", "reason": "WeChat API doesn't support broadcast"},
"reply": {"status": "implemented", "impl": "adapter.py (bridge reply_to_mode + wecom/mp reply quoting)"},
"sendWithEffect": {"status": "unsupported", "reason": "WeChat doesn't support message effects"},
"sendAttachment": {"status": "implemented", "impl": "adapter.send_media()"},
"sticker": {"status": "unsupported", "reason": "WeChat doesn't support stickers"},
"sticker-search": {"status": "unsupported", "reason": "WeChat doesn't support stickers"},
"sticker-upload": {"status": "unsupported", "reason": "WeChat doesn't support stickers"},
"edit": {"status": "unsupported", "reason": "WeChat API doesn't support message editing"},
"unsend": {"status": "unsupported", "reason": "WeChat API doesn't support unsend"},
"delete": {"status": "unsupported", "reason": "WeChat API doesn't support delete"},
"read": {"status": "implemented", "impl": "message_read.py"},
"pin": {"status": "unsupported", "reason": "WeChat doesn't support pin"},
"unpin": {"status": "unsupported", "reason": "WeChat doesn't support pin"},
"list-pins": {"status": "unsupported", "reason": "WeChat doesn't support pin"},
"permissions": {"status": "unsupported", "reason": "WeChat doesn't support permission API"},
"timeout": {"status": "unsupported", "reason": "WeChat doesn't support timeout/kick"},
"kick": {"status": "unsupported", "reason": "WeChat doesn't support kick API"},
"ban": {"status": "unsupported", "reason": "WeChat doesn't support ban API"},
"react": {"status": "unsupported", "reason": "WeChat doesn't support Reaction"},
"reactions": {"status": "unsupported", "reason": "WeChat doesn't support Reaction"},
"poll": {"status": "unsupported", "reason": "WeChat doesn't support Poll"},
"poll-vote": {"status": "unsupported", "reason": "WeChat doesn't support Poll"},
"emoji-list": {"status": "unsupported", "reason": "WeChat doesn't support Emoji API"},
"emoji-upload": {"status": "unsupported", "reason": "WeChat doesn't support Emoji API"},
"voice-status": {"status": "unsupported", "reason": "WeChat doesn't support voice status"},
"renameGroup": {"status": "implemented", "impl": "group_admin.py"},
"setGroupIcon": {"status": "unsupported", "reason": "WeCom API doesn't support group icon change"},
"addParticipant": {"status": "implemented", "impl": "group_admin.py"},
"removeParticipant": {"status": "implemented", "impl": "group_admin.py"},
"leaveGroup": {"status": "unsupported", "reason": "WeCom doesn't support leave group via API"},
"channel-info": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
"channel-list": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
"channel-create": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
"channel-edit": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
"channel-delete": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
"channel-move": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
"category-create": {"status": "unsupported", "reason": "WeChat doesn't support category concept"},
"category-edit": {"status": "unsupported", "reason": "WeChat doesn't support category concept"},
"category-delete": {"status": "unsupported", "reason": "WeChat doesn't support category concept"},
"topic-create": {"status": "unsupported", "reason": "WeChat doesn't support topic concept"},
"topic-edit": {"status": "unsupported", "reason": "WeChat doesn't support topic concept"},
"thread-create": {"status": "unsupported", "reason": "WeChat doesn't support thread concept"},
"thread-list": {"status": "unsupported", "reason": "WeChat doesn't support thread concept"},
"thread-reply": {"status": "unsupported", "reason": "WeChat doesn't support thread concept"},
"member-info": {"status": "unsupported", "reason": "WeChat doesn't support member info API"},
"role-info": {"status": "unsupported", "reason": "WeChat doesn't support role concept"},
"role-add": {"status": "unsupported", "reason": "WeChat doesn't support role concept"},
"role-remove": {"status": "unsupported", "reason": "WeChat doesn't support role concept"},
"event-list": {"status": "unsupported", "reason": "WeChat doesn't support event API"},
"event-create": {"status": "unsupported", "reason": "WeChat doesn't support event API"},
"download-file": {"status": "implemented", "impl": "adapter.download_media()"},
"upload-file": {"status": "implemented", "impl": "adapter.send_media()"},
"search": {"status": "unsupported", "reason": "WeChat doesn't support message search API"},
"set-profile": {"status": "unsupported", "reason": "WeChat doesn't support profile API"},
"set-presence": {"status": "unsupported", "reason": "WeChat doesn't support presence API"},
}
class WeChatMessageActionAdapter:
@staticmethod
def is_supported(action_name: str) -> bool:
info = WECHAT_MESSAGE_ACTIONS.get(action_name, {})
return info.get("status") == "implemented"
@staticmethod
def get_handler(action_name: str) -> Callable[..., Awaitable[Any]] | None:
return None
@staticmethod
def list_supported_actions() -> list[str]:
return [k for k, v in WECHAT_MESSAGE_ACTIONS.items() if v.get("status") == "implemented"]
@staticmethod
def list_all_actions() -> dict[str, dict[str, Any]]:
return dict(WECHAT_MESSAGE_ACTIONS)
def _status_to_action_status(status: str) -> ActionStatus:
from yuxi.channels.message_actions import ActionStatus
status_map = {
"implemented": ActionStatus.SUPPORTED,
"planned": ActionStatus.PARTIAL,
"unsupported": ActionStatus.UNSUPPORTED,
}
return status_map.get(status, ActionStatus.UNSUPPORTED)
def _action_name_to_enum(name: str) -> MessageAction | None:
from yuxi.channels.message_actions import MessageAction
mapping: dict[str, str] = {
"send": "send",
"broadcast": "broadcast",
"reply": "reply",
"sendWithEffect": "send_with_effect",
"sendAttachment": "send_attachment",
"sticker": "sticker",
"sticker-search": "sticker_search",
"sticker-upload": "sticker_upload",
"edit": "edit",
"unsend": "unsend",
"delete": "delete",
"read": "read",
"pin": "pin",
"unpin": "unpin",
"list-pins": "list_pins",
"permissions": "permissions",
"timeout": "mute",
"kick": "kick",
"ban": "ban",
"react": "react",
"reactions": "list_reactions",
"poll": "create_poll",
"poll-vote": "close_poll",
"emoji-list": "emoji_list",
"emoji-upload": "emoji_upload",
"voice-status": "voice_status",
"renameGroup": "rename_group",
"setGroupIcon": "set_group_icon",
"addParticipant": "add_participant",
"removeParticipant": "remove_participant",
"leaveGroup": "leave_group",
"channel-info": "channel_info",
"channel-list": "channel_list",
"channel-create": "channel_create",
"channel-edit": "channel_edit",
"channel-delete": "channel_delete",
"channel-move": "channel_move",
"category-create": "category_create",
"category-edit": "category_edit",
"category-delete": "category_delete",
"topic-create": "topic_create",
"topic-edit": "topic_edit",
"thread-create": "thread_create",
"thread-list": "thread_list",
"thread-reply": "thread_reply",
"member-info": "member_info",
"role-info": "role_info",
"role-add": "role_add",
"role-remove": "role_remove",
"event-list": "event_list",
"event-create": "event_create",
"download-file": "download_file",
"upload-file": "upload_file",
"search": "search",
"set-profile": "set_profile",
"set-presence": "set_presence",
}
try:
return MessageAction(mapping[name])
except (KeyError, ValueError):
return None
def register_wechat_actions() -> None:
from yuxi.channels.message_actions import ActionDeclaration, ActionRegistry
declarations: dict = {}
for action_name, info in WECHAT_MESSAGE_ACTIONS.items():
action_enum = _action_name_to_enum(action_name)
if action_enum is None:
continue
declarations[action_enum] = ActionDeclaration(
action=action_enum,
status=_status_to_action_status(info.get("status", "unsupported")),
reason=info.get("reason", ""),
impl=info.get("impl", ""),
)
ActionRegistry.register_channel_actions("wechat", declarations)