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

56 lines
2.2 KiB
Python

from __future__ import annotations
import time
from typing import Any
import httpx
from yuxi.utils.logging_config import logger
from .bridge import BridgeClient
from .mp.client import MPClient
from .wecom.client import WeComClient
async def read_message_wecom(client: WeComClient, http_client: httpx.AsyncClient, msg_id: str) -> dict[str, Any]:
try:
token = await client.get_access_token()
url = "https://qyapi.weixin.qq.com/cgi-bin/media/get"
params = {"access_token": token, "media_id": msg_id}
resp = await http_client.head(url, params=params)
available = resp.status_code == 200
return {"msg_id": msg_id, "available": available, "read_at": time.time()}
except Exception as e:
logger.warning(f"[WeChat/MessageRead] WeCom read failed: {e}")
return {"msg_id": msg_id, "available": False, "error": str(e)}
async def read_message_mp(client: MPClient, http_client: httpx.AsyncClient, msg_id: str) -> dict[str, Any]:
try:
token = await client.get_access_token()
url = "https://api.weixin.qq.com/cgi-bin/media/get"
params = {"access_token": token, "media_id": msg_id}
resp = await http_client.head(url, params=params)
available = resp.status_code == 200
return {"msg_id": msg_id, "available": available, "read_at": time.time()}
except Exception as e:
logger.warning(f"[WeChat/MessageRead] MP read failed: {e}")
return {"msg_id": msg_id, "available": False, "error": str(e)}
async def read_message_bridge(bridge_client: BridgeClient, msg_id: str) -> dict[str, Any]:
try:
healthy = await bridge_client.health_check()
if healthy:
events = await bridge_client.fetch_events()
found = any(
str(event.get("data", {}).get("msg_id", "")) == msg_id
for event in events
if event.get("type") == "message"
)
return {"msg_id": msg_id, "available": found, "read_at": time.time()}
return {"msg_id": msg_id, "available": False, "read_at": time.time()}
except Exception as e:
logger.warning(f"[WeChat/MessageRead] Bridge read failed: {e}")
return {"msg_id": msg_id, "available": False, "error": str(e)}