import httpx from app.services.token_manager import get_access_token WECHAT_API_BASE = "https://qyapi.weixin.qq.com" async def send_msg(touser: str, open_kfid: str, msgtype: str, content: str) -> dict: """主动发送消息给客户""" token = await get_access_token() url = f"{WECHAT_API_BASE}/cgi-bin/kf/send_msg?access_token={token}" body = { "touser": touser, "open_kfid": open_kfid, "msgtype": msgtype, } if msgtype == "text": body["text"] = {"content": content} async with httpx.AsyncClient() as client: resp = await client.post(url, json=body) return resp.json() async def sync_msg(open_kfid: str, cursor: str = "", token: str = "", limit: int = 100) -> dict: """拉取客服消息(轮询模式)""" access_token = await get_access_token() url = f"{WECHAT_API_BASE}/cgi-bin/kf/sync_msg?access_token={access_token}" body = { "cursor": cursor, "token": token, "limit": limit, "open_kfid": open_kfid, } async with httpx.AsyncClient() as client: resp = await client.post(url, json=body) return resp.json()