该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块: 1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等 2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等 3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力 4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等 5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等 实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
from __future__ import annotations
|
|
|
|
from enum import Enum
|
|
from typing import Any
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
|
|
class IntegrationStatus(str, Enum):
|
|
NOT_CONFIGURED = "not_configured"
|
|
CONFIGURED = "configured"
|
|
CONNECTING = "connecting"
|
|
CONNECTED = "connected"
|
|
ERROR = "error"
|
|
DISCONNECTED = "disconnected"
|
|
|
|
|
|
class WeChatIntegrationStatus:
|
|
@staticmethod
|
|
def resolve_status(config: dict[str, Any], connection_status: str) -> IntegrationStatus:
|
|
has_config = any(config.get(k) for k in ("corp_id", "app_id", "bridge_url"))
|
|
if not has_config:
|
|
return IntegrationStatus.NOT_CONFIGURED
|
|
status_map = {
|
|
"connected": IntegrationStatus.CONNECTED,
|
|
"connecting": IntegrationStatus.CONNECTING,
|
|
"disconnected": IntegrationStatus.DISCONNECTED,
|
|
"error": IntegrationStatus.ERROR,
|
|
}
|
|
return status_map.get(connection_status, IntegrationStatus.CONFIGURED)
|
|
|
|
@staticmethod
|
|
def get_status_summary(status: IntegrationStatus) -> dict[str, Any]:
|
|
return {
|
|
"status": status.value,
|
|
"is_operational": status in (IntegrationStatus.CONNECTED,),
|
|
"is_configured": status != IntegrationStatus.NOT_CONFIGURED,
|
|
}
|
|
|
|
@staticmethod
|
|
def log_self_id(
|
|
config: dict[str, Any],
|
|
mode: str = "wecom",
|
|
user_id: str = "",
|
|
group_ids: list[str] | None = None,
|
|
) -> dict[str, Any]:
|
|
identity_info: dict[str, Any] = {
|
|
"mode": mode,
|
|
"user_id": user_id or config.get("agent_id", config.get("app_id", "")),
|
|
"group_ids": group_ids or [],
|
|
}
|
|
|
|
if mode == "wecom":
|
|
identity_info.update(
|
|
{
|
|
"corp_id": config.get("corp_id", "")[:8] + "***",
|
|
"agent_id": config.get("agent_id", ""),
|
|
}
|
|
)
|
|
elif mode == "mp":
|
|
identity_info.update(
|
|
{
|
|
"app_id": config.get("app_id", "")[:8] + "***",
|
|
}
|
|
)
|
|
elif mode == "personal":
|
|
identity_info.update(
|
|
{
|
|
"bridge_url": config.get("bridge_url", ""),
|
|
}
|
|
)
|
|
|
|
logger.info(f"[WeChat/Status] Self identity: {identity_info}")
|
|
return identity_info
|