该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块: 1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等 2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等 3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力 4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等 5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等 实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from yuxi.channels.models import ChannelMessage, ChatType
|
|
|
|
|
|
class WeChatResolverAdapter:
|
|
def __init__(self):
|
|
self._alias_map: dict[str, str] = {}
|
|
|
|
def load_aliases(self, config: dict[str, Any]) -> None:
|
|
self._alias_map.clear()
|
|
aliases = config.get("aliases", {})
|
|
if isinstance(aliases, dict):
|
|
self._alias_map.update(aliases)
|
|
|
|
@staticmethod
|
|
def looks_like_id(raw: str) -> bool:
|
|
if not raw or not raw.strip():
|
|
return False
|
|
return raw.strip().startswith("wx:") or bool(raw.strip())
|
|
|
|
@staticmethod
|
|
def resolve_target(raw: str) -> dict[str, Any]:
|
|
raw = raw.strip()
|
|
if raw.startswith("wx:group:"):
|
|
return {"to": raw, "chatType": "group"}
|
|
if raw.startswith("wx:"):
|
|
return {"to": raw, "chatType": "direct"}
|
|
return {"to": f"wx:{raw}", "chatType": "direct"}
|
|
|
|
@property
|
|
def hint(self) -> str:
|
|
return "格式: wx:<open_id> 或 wx:group:<chat_id>"
|
|
|
|
def resolve_alias(self, alias: str) -> str | None:
|
|
return self._alias_map.get(alias)
|
|
|
|
def resolve_target_with_aliases(self, raw: str) -> dict[str, Any]:
|
|
target = self.resolve_target(raw)
|
|
alias_resolved = self._alias_map.get(target["to"])
|
|
if alias_resolved:
|
|
target["to"] = alias_resolved
|
|
target["alias_resolved"] = True
|
|
return target
|
|
|
|
@staticmethod
|
|
def format_target_display(target: dict[str, Any]) -> str:
|
|
to = target.get("to", "unknown")
|
|
chat_type = target.get("chatType", "direct")
|
|
return f"[{chat_type}] {to}"
|
|
|
|
@staticmethod
|
|
def build_cross_context_presentation(
|
|
source_ctx: dict[str, Any],
|
|
target_ctx: dict[str, Any] | None = None,
|
|
) -> str:
|
|
src_peer = source_ctx.get("peer_name", source_ctx.get("to", "unknown"))
|
|
return f"[转发自 {src_peer}]"
|
|
|
|
@staticmethod
|
|
def resolve_session_target(message: ChannelMessage) -> dict[str, Any]:
|
|
return {
|
|
"to": message.identity.channel_user_id,
|
|
"chatId": message.identity.channel_chat_id,
|
|
"chatType": message.metadata.get("chat_type", "direct"),
|
|
}
|
|
|
|
@staticmethod
|
|
def infer_chat_type(target_id: str) -> ChatType:
|
|
if "group" in target_id:
|
|
return ChatType.GROUP
|
|
return ChatType.DIRECT
|
|
|
|
def list_aliases(self) -> list[dict[str, str]]:
|
|
return [{"alias": alias, "target": target} for alias, target in self._alias_map.items()]
|