该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块: 1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等 2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等 3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力 4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等 5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等 实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
105 lines
3.7 KiB
Python
105 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import httpx
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
from .mp.client import MPClient
|
|
from .wecom.client import WeComClient
|
|
|
|
|
|
async def list_peers_mp(client: MPClient, http_client: httpx.AsyncClient) -> list[dict[str, Any]]:
|
|
try:
|
|
token = await client.get_access_token()
|
|
url = "https://api.weixin.qq.com/cgi-bin/user/get"
|
|
resp = await http_client.get(url, params={"access_token": token})
|
|
data = resp.json()
|
|
if data.get("errcode") != 0:
|
|
return []
|
|
openids = data.get("data", {}).get("openid", [])
|
|
results: list[dict[str, Any]] = []
|
|
for openid in openids[:100]:
|
|
results.append(
|
|
{
|
|
"id": f"wx:{openid}",
|
|
"name": openid,
|
|
}
|
|
)
|
|
return results
|
|
except Exception as e:
|
|
logger.warning(f"[WeChat/Directory] list_peers_mp failed: {e}")
|
|
return []
|
|
|
|
|
|
async def list_peers_wecom(client: WeComClient, http_client: httpx.AsyncClient) -> list[dict[str, Any]]:
|
|
try:
|
|
token = await client.get_access_token()
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/user/list"
|
|
resp = await http_client.get(url, params={"access_token": token, "department_id": 1, "fetch_child": 1})
|
|
data = resp.json()
|
|
if data.get("errcode") != 0:
|
|
return []
|
|
userlist = data.get("userlist", [])
|
|
return [{"id": f"wx:{u.get('userid', '')}", "name": u.get("name", u.get("userid", ""))} for u in userlist]
|
|
except Exception as e:
|
|
logger.warning(f"[WeChat/Directory] list_peers_wecom failed: {e}")
|
|
return []
|
|
|
|
|
|
async def list_peers_bridge(bridge_client, cfg: dict[str, Any]) -> list[dict[str, Any]]:
|
|
try:
|
|
contacts = cfg.get("contacts", [])
|
|
return [{"id": f"wx:{c.get('id', '')}", "name": c.get("name", c.get("id", ""))} for c in contacts]
|
|
except Exception as e:
|
|
logger.warning(f"[WeChat/Directory] list_peers_bridge failed: {e}")
|
|
return []
|
|
|
|
|
|
async def list_groups_wecom(client: WeComClient, http_client: httpx.AsyncClient) -> list[dict[str, Any]]:
|
|
try:
|
|
token = await client.get_access_token()
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/list"
|
|
resp = await http_client.get(url, params={"access_token": token})
|
|
data = resp.json()
|
|
if data.get("errcode") != 0:
|
|
return []
|
|
chat_list = data.get("chat_list", [])
|
|
return [
|
|
{"id": f"wx:group:{c.get('chatid', '')}", "name": c.get("name", c.get("chatid", ""))} for c in chat_list
|
|
]
|
|
except Exception as e:
|
|
logger.warning(f"[WeChat/Directory] list_groups_wecom failed: {e}")
|
|
return []
|
|
|
|
|
|
async def list_groups_mp(client: MPClient, http_client: httpx.AsyncClient) -> list[dict[str, Any]]:
|
|
return []
|
|
|
|
|
|
async def list_groups_bridge(bridge_client, cfg: dict[str, Any]) -> list[dict[str, Any]]:
|
|
try:
|
|
groups_cfg = cfg.get("groups", {})
|
|
return [
|
|
{"id": f"wx:group:{chat_id}", "name": chat_config.get("name", chat_id)}
|
|
for chat_id, chat_config in groups_cfg.items()
|
|
]
|
|
except Exception as e:
|
|
logger.warning(f"[WeChat/Directory] list_groups_bridge failed: {e}")
|
|
return []
|
|
|
|
|
|
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}"
|
|
|
|
|
|
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}]"
|