ForcePilot/backend/package/yuxi/channel/extensions/messenger/status.py

49 lines
1.7 KiB
Python
Raw Normal View History

import logging
import httpx
logger = logging.getLogger(__name__)
class MessengerStatus:
async def probe(self, account: dict) -> dict:
page_id = account.get("page_id", "")
access_token = account.get("page_access_token", "")
if not page_id or not access_token:
return {"healthy": False, "reason": "missing credentials"}
try:
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client:
resp = await client.get(
f"https://graph.facebook.com/v22.0/{page_id}",
params={"fields": "id,name,access_token", "access_token": access_token},
)
if resp.status_code == 200:
data = resp.json()
return {
"healthy": True,
"page_id": page_id,
"page_name": data.get("name", ""),
"token_valid": True,
}
return {"healthy": False, "reason": f"graph api error: {resp.status_code}"}
except Exception as e:
logger.exception("messenger probe error")
return {"healthy": False, "reason": str(e)}
@staticmethod
def build_summary(snapshot: dict) -> dict:
probes = {}
if snapshot:
if "page_name" in snapshot:
probes["page_name"] = snapshot["page_name"]
if "token_valid" in snapshot:
probes["token_valid"] = snapshot["token_valid"]
if "page_id" in snapshot:
probes["page_id"] = snapshot["page_id"]
return {
"channel": "messenger",
"probes": probes,
}