ForcePilot/backend/package/yuxi/channels/adapters/wechat/outbound_adapter.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

60 lines
1.8 KiB
Python

from __future__ import annotations
from typing import Any
from yuxi.channels.models import ChannelResponse, DeliveryResult
WECHAT_DELIVERY_MODE = "direct"
_MESSAGE_PRIORITY = {
"text": 0,
"image": 5,
"file": 10,
"voice": 15,
}
class WeChatOutboundAdapter:
delivery_mode: str = "direct"
def __init__(self):
self._priority_enabled = False
self._pending_messages: list[tuple[int, Any]] = []
def configure(self, config: dict[str, Any]) -> None:
self._priority_enabled = config.get("outbound_priority_enabled", False)
async def before_deliver_payload(self, payload: dict[str, Any], response: ChannelResponse) -> dict[str, Any]:
import time
payload["_sent_at"] = time.time()
if response.metadata.get("agent_version"):
payload["_agent_version"] = response.metadata["agent_version"]
return payload
@staticmethod
def should_suppress_local_payload_prompt(config: dict[str, Any]) -> bool:
return True
@staticmethod
def build_attached_results(result: DeliveryResult, mode: str = "") -> dict[str, Any]:
attached: dict[str, Any] = {
"success": result.success,
"error": result.error,
"message_id": result.message_id,
"sent_message_id": result.message_id,
}
if mode:
attached["mode"] = mode
if result.metadata:
attached.update(result.metadata)
return attached
@staticmethod
def sort_outbound_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
def _priority(msg: dict[str, Any]) -> int:
msg_type = str(msg.get("msgtype", "text"))
return _MESSAGE_PRIORITY.get(msg_type, 0)
return sorted(messages, key=_priority, reverse=True)