ForcePilot/backend/package/yuxi/channel/extensions/wechat-mp/status.py

94 lines
3.3 KiB
Python
Raw Normal View History

feat(channel): 添加微信客服、微信公众号和微信支付通知渠道扩展 新增微信客服、微信公众号、微信支付通知三个渠道扩展。 微信客服渠道扩展功能模块: - account: 账户管理 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - crypto: 加解密处理 - dedupe: 消息去重 - customer: 客户管理 - servicer: 客服管理 - session: 会话管理 - status: 会话状态管理 - media: 媒体资源处理 - statistics: 统计功能 - sync: 数据同步 - upgrade: 升级处理 微信公众号渠道扩展功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - crypto: 加解密处理 - dedupe: 消息去重 - passive_reply: 被动回复 - message: 消息处理 - broadcast: 群发消息 - template: 模板消息 - menu: 菜单管理 - qrcode: 二维码管理 - user: 用户管理 - media: 媒体资源处理 - status: 会话状态管理 微信支付通知渠道扩展功能模块: - config: 渠道配置管理 - webhook: Webhook 事件处理 - crypto: 加解密与签名校验 - cert_manager: 证书管理 - event_router: 事件路由 - dedupe: 消息去重 - pay_repo: 支付数据仓库 - query_client: 查询客户端 - arq_tasks: 异步任务 - callback_compensator: 回调补偿
2026-05-21 12:00:30 +08:00
import logging
import httpx
from yuxi.channel.protocols import ChannelAccountSnapshot
logger = logging.getLogger(__name__)
PROBE_URL = "https://api.weixin.qq.com/cgi-bin/getcallbackip"
class WeChatMPStatus:
def __init__(self, gateway=None):
self._gateway = gateway
async def probe(self, account: dict | None = None) -> bool:
gateway = self._gateway
if gateway is None:
return False
token = gateway.access_token
if not token:
return False
url = f"{PROBE_URL}?access_token={token}"
try:
async with httpx.AsyncClient(timeout=10.0) as client:
resp = await client.get(url)
data = resp.json()
if "errcode" in data and data["errcode"] != 0:
logger.warning("WeChat MP probe failed: errcode=%s", data.get("errcode"))
return False
return True
except Exception:
logger.exception("WeChat MP probe failed")
return False
def build_summary(self, snapshot: object) -> dict:
if not isinstance(snapshot, ChannelAccountSnapshot):
return {}
return {
"account_id": snapshot.account_id,
"running": snapshot.running,
"connected": snapshot.connected,
"health_state": snapshot.health_state,
}
def build_account_snapshot(
self,
account: dict,
config: dict,
runtime: ChannelAccountSnapshot | None = None,
probe_result: object | None = None,
audit: object | None = None,
) -> ChannelAccountSnapshot:
gateway = self._gateway
token = gateway.access_token if gateway else None
return ChannelAccountSnapshot(
account_id=account.get("account_id", "default"),
name=account.get("name", ""),
enabled=account.get("enabled", True),
configured=bool(account.get("app_id") and account.get("app_secret")),
status_state="linked" if token else "not-linked",
running=getattr(runtime, "running", False) if runtime else False,
connected=bool(token),
last_message_at=getattr(runtime, "last_message_at", None) if runtime else None,
health_state="ok" if probe_result else "unknown",
dm_policy=account.get("dm_policy", "open"),
)
def collect_status_issues(self, accounts: list[ChannelAccountSnapshot]) -> list:
issues = []
for acc in accounts:
if not acc.configured:
issues.append({
"channel": "wechat-mp",
"account_id": acc.account_id,
"kind": "not-configured",
"message": "WeChat MP credentials not configured",
"fix": "Set WECHAT_MP_APP_ID, WECHAT_MP_APP_SECRET, WECHAT_MP_TOKEN, WECHAT_MP_ENCODING_AES_KEY",
})
elif not acc.connected:
issues.append({
"channel": "wechat-mp",
"account_id": acc.account_id,
"kind": "not-connected",
"message": "WeChat MP gateway not running",
"fix": "Check environment variables and gateway status",
})
return issues