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}]"
|