该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块: 1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等 2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等 3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力 4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等 5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等 实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
92 lines
4.0 KiB
Python
92 lines
4.0 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import httpx
|
|
|
|
from yuxi.channels.models import DeliveryResult
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
from .wecom.client import WeComClient
|
|
|
|
|
|
class WeChatGroupAdmin:
|
|
async def get_channel_info(
|
|
self, client: WeComClient, http_client: httpx.AsyncClient, chat_id: str
|
|
) -> dict[str, Any]:
|
|
try:
|
|
token = await client.get_access_token()
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/get"
|
|
params = {"access_token": token, "chatid": chat_id}
|
|
resp = await http_client.get(url, params=params)
|
|
data = resp.json()
|
|
if data.get("errcode") == 0:
|
|
return data.get("chat_info", {})
|
|
except Exception as e:
|
|
logger.warning(f"[WeChat/GroupAdmin] get_channel_info failed: {e}")
|
|
return {}
|
|
|
|
async def rename_group(
|
|
self, client: WeComClient, http_client: httpx.AsyncClient, chat_id: str, name: str
|
|
) -> DeliveryResult:
|
|
try:
|
|
token = await client.get_access_token()
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update"
|
|
payload = {"chatid": chat_id, "name": name}
|
|
resp = await http_client.post(url, params={"access_token": token}, json=payload)
|
|
data = resp.json()
|
|
if data.get("errcode") == 0:
|
|
return DeliveryResult(success=True)
|
|
return DeliveryResult(success=False, error=data.get("errmsg", "Unknown"))
|
|
except Exception as e:
|
|
return DeliveryResult(success=False, error=str(e))
|
|
|
|
async def set_group_icon(
|
|
self, client: WeComClient, http_client: httpx.AsyncClient, chat_id: str, image_data: bytes
|
|
) -> DeliveryResult:
|
|
return DeliveryResult(success=False, error="WeCom API does not support group icon")
|
|
|
|
async def add_participant(
|
|
self, client: WeComClient, http_client: httpx.AsyncClient, chat_id: str, user_id: str
|
|
) -> DeliveryResult:
|
|
try:
|
|
token = await client.get_access_token()
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update"
|
|
current = await self.get_channel_info(client, http_client, chat_id)
|
|
userlist = list(current.get("userlist", []))
|
|
if user_id not in userlist:
|
|
userlist.append(user_id)
|
|
payload = {"chatid": chat_id, "add_user_list": [user_id]}
|
|
resp = await http_client.post(url, params={"access_token": token}, json=payload)
|
|
data = resp.json()
|
|
if data.get("errcode") == 0:
|
|
return DeliveryResult(success=True)
|
|
return DeliveryResult(success=False, error=data.get("errmsg", "Unknown"))
|
|
except Exception as e:
|
|
return DeliveryResult(success=False, error=str(e))
|
|
|
|
async def remove_participant(
|
|
self, client: WeComClient, http_client: httpx.AsyncClient, chat_id: str, user_id: str
|
|
) -> DeliveryResult:
|
|
try:
|
|
token = await client.get_access_token()
|
|
url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/update"
|
|
payload = {"chatid": chat_id, "del_user_list": [user_id]}
|
|
resp = await http_client.post(url, params={"access_token": token}, json=payload)
|
|
data = resp.json()
|
|
if data.get("errcode") == 0:
|
|
return DeliveryResult(success=True)
|
|
return DeliveryResult(success=False, error=data.get("errmsg", "Unknown"))
|
|
except Exception as e:
|
|
return DeliveryResult(success=False, error=str(e))
|
|
|
|
async def leave_group(self, client: WeComClient, http_client: httpx.AsyncClient, chat_id: str) -> DeliveryResult:
|
|
return DeliveryResult(success=False, error="WeCom does not support leave group via API")
|
|
|
|
async def list_members(
|
|
self, client: WeComClient, http_client: httpx.AsyncClient, chat_id: str
|
|
) -> list[dict[str, Any]]:
|
|
info = await self.get_channel_info(client, http_client, chat_id)
|
|
userlist = info.get("userlist", [])
|
|
return [{"user_id": uid} for uid in userlist]
|