ForcePilot/backend/package/yuxi/channel/extensions/whatsapp/status.py
Kris 7215418610 feat(channel): 添加企业微信、微博、WhatsApp 和 Workplace 渠道扩展
新增企业微信、微博、WhatsApp、Workplace 四个渠道扩展。

企业微信渠道扩展主要模块:config, gateway, webhook, webhook_bot, outbound, streaming, pairing, security, crypto, dedupe, persistent_dedupe, card, directory, events, externalcontact, media, mentions, menu, message, oauth, status

微博渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, passive_reply, broadcast, message, menu, media, subscription, template, status

WhatsApp 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, monitor, status

Workplace 渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, actions, challenge, groups, media, mentions, menu, monitor, persona, quick_reply, signature, subscriptions, template, threading, users, status
2026-05-21 12:01:56 +08:00

83 lines
3.2 KiB
Python

"""WhatsApp Status 适配器 — 健康探针 + 状态快照"""
import aiohttp
import logging
from yuxi.channel.protocols import ChannelAccountSnapshot
logger = logging.getLogger(__name__)
WHATSAPP_API_BASE = "https://graph.facebook.com/v22.0"
class WhatsAppStatus:
default_runtime = ChannelAccountSnapshot(account_id="default")
async def probe(self, account: dict | None = None) -> bool:
if not account:
return False
phone_number_id = account.get("phone_number_id", "")
access_token = account.get("access_token", "")
if not phone_number_id or not access_token:
return False
url = f"{WHATSAPP_API_BASE}/{phone_number_id}"
headers = {"Authorization": f"Bearer {access_token}"}
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers, timeout=aiohttp.ClientTimeout(total=10)) as resp:
return resp.status == 200
except Exception:
logger.exception("WhatsApp probe failed")
return False
def build_summary(self, snapshot: object) -> dict:
if not isinstance(snapshot, ChannelAccountSnapshot):
return {}
from yuxi.channel.protocols import build_standard_summary
return build_standard_summary(snapshot, "whatsapp")
def build_account_snapshot(
self,
account: dict,
config: dict,
runtime: ChannelAccountSnapshot | None = None,
probe_result: object | None = None,
audit: object | None = None,
) -> ChannelAccountSnapshot:
return ChannelAccountSnapshot(
account_id=account.get("account_id", ""),
name=account.get("name", ""),
enabled=account.get("enabled", True),
configured=bool(account.get("phone_number_id") and account.get("access_token")),
status_state="linked" if account.get("access_token") else "not-linked",
running=getattr(runtime, "running", False) if runtime else False,
connected=getattr(runtime, "connected", False) if runtime else False,
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", "pairing"),
)
def collect_status_issues(self, accounts: list[ChannelAccountSnapshot]) -> list:
issues = []
for acc in accounts:
if not acc.configured:
issues.append({
"channel": "whatsapp",
"account_id": acc.account_id,
"kind": "not-configured",
"message": "WhatsApp Cloud API credentials not configured",
"fix": "Set WHATSAPP_ACCESS_TOKEN and WHATSAPP_PHONE_NUMBER_ID",
})
elif not acc.connected:
issues.append({
"channel": "whatsapp",
"account_id": acc.account_id,
"kind": "not-connected",
"message": "WhatsApp gateway not running",
"fix": "Check webhook endpoint and gateway status",
})
return issues