ForcePilot/backend/package/yuxi/channels/adapters/wechat/message_read.py

56 lines
2.2 KiB
Python
Raw Normal View History

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)}