该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块: 1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等 2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等 3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力 4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等 5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等 实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import httpx
|
|
|
|
from yuxi.channels.models import HealthStatus
|
|
from yuxi.utils.datetime_utils import utc_now_naive
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
|
|
def _safe_json(resp: httpx.Response) -> dict:
|
|
content_type = resp.headers.get("content-type", "")
|
|
if "application/json" not in content_type and not content_type:
|
|
text = resp.text[:200]
|
|
logger.warning(f"[WeChat/Probe] Non-JSON response: {text}")
|
|
return {}
|
|
try:
|
|
return resp.json()
|
|
except Exception:
|
|
logger.warning(f"[WeChat/Probe] JSON parse failed: {resp.text[:200]}")
|
|
return {}
|
|
|
|
|
|
async def probe_wecom(http_client: httpx.AsyncClient, config: dict[str, Any]) -> HealthStatus:
|
|
try:
|
|
token = await _get_wecom_token(http_client, config)
|
|
if token:
|
|
return HealthStatus(
|
|
status="healthy",
|
|
metadata={"mode": "wecom", "token_valid": True},
|
|
last_connected_at=utc_now_naive(),
|
|
)
|
|
return HealthStatus(status="unhealthy", last_error="Failed to obtain access_token")
|
|
except Exception as e:
|
|
return HealthStatus(status="unhealthy", last_error=str(e))
|
|
|
|
|
|
async def probe_mp(http_client: httpx.AsyncClient, config: dict[str, Any]) -> HealthStatus:
|
|
try:
|
|
token = await _get_mp_token(http_client, config)
|
|
if token:
|
|
return HealthStatus(
|
|
status="healthy",
|
|
metadata={"mode": "mp", "token_valid": True},
|
|
last_connected_at=utc_now_naive(),
|
|
)
|
|
return HealthStatus(status="unhealthy", last_error="Failed to obtain access_token")
|
|
except Exception as e:
|
|
return HealthStatus(status="unhealthy", last_error=str(e))
|
|
|
|
|
|
async def probe_bridge(http_client: httpx.AsyncClient, bridge_url: str) -> HealthStatus:
|
|
try:
|
|
resp = await http_client.get(f"{bridge_url}/health", timeout=5.0)
|
|
if resp.status_code == 200:
|
|
login_status = await _check_bridge_login(http_client, bridge_url)
|
|
return HealthStatus(
|
|
status="healthy",
|
|
metadata={
|
|
"mode": "personal",
|
|
"bridge_url": bridge_url,
|
|
"logged_in": login_status.get("logged_in", False),
|
|
},
|
|
last_connected_at=utc_now_naive(),
|
|
)
|
|
return HealthStatus(status="degraded", last_error=f"Bridge health check returned {resp.status_code}")
|
|
except Exception as e:
|
|
return HealthStatus(status="unhealthy", last_error=str(e))
|
|
|
|
|
|
async def _get_wecom_token(http_client: httpx.AsyncClient, config: dict[str, Any]) -> str | None:
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
|
|
params = {
|
|
"corpid": config["corp_id"],
|
|
"corpsecret": config["corp_secret"],
|
|
}
|
|
resp = await http_client.get(url, params=params, timeout=10.0)
|
|
data = _safe_json(resp)
|
|
return data.get("access_token")
|
|
|
|
|
|
async def _get_mp_token(http_client: httpx.AsyncClient, config: dict[str, Any]) -> str | None:
|
|
url = "https://api.weixin.qq.com/cgi-bin/token"
|
|
params = {
|
|
"grant_type": "client_credential",
|
|
"appid": config["app_id"],
|
|
"secret": config["app_secret"],
|
|
}
|
|
resp = await http_client.get(url, params=params, timeout=10.0)
|
|
data = _safe_json(resp)
|
|
return data.get("access_token")
|
|
|
|
|
|
async def _check_bridge_login(http_client: httpx.AsyncClient, bridge_url: str) -> dict:
|
|
try:
|
|
resp = await http_client.get(f"{bridge_url}/login/status", timeout=5.0)
|
|
return _safe_json(resp)
|
|
except Exception:
|
|
return {"logged_in": False}
|