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

56 lines
1.7 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
import httpx
from yuxi.channel.extensions.viber.types import VIBER_API_BASE
logger = logging.getLogger(__name__)
class ViberStatusAdapter:
async def probe(self, account: dict | None = None) -> bool:
if not account:
return False
auth_token = account.get("auth_token", "")
if not auth_token:
return False
try:
headers = {"X-Viber-Auth-Token": auth_token}
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client:
resp = await client.post(
f"{VIBER_API_BASE}/get_account_info",
headers=headers,
)
result = resp.json()
ok = result.get("status") == 0
if ok:
logger.info(
"Viber health: name=%s uri=%s subscribers=%s",
result.get("name"),
result.get("uri"),
result.get("subscribers_count"),
)
return ok
except Exception:
logger.exception("Viber health probe error")
return False
def build_summary(self, snapshot: object) -> dict:
account = getattr(snapshot, "account", {})
return {
"channel": "viber",
"mode": "webhook",
"account_id": account.get("account_id", "") if isinstance(account, dict) else "",
}
def resolve_account_state(self, account: dict, config: dict, configured: bool, enabled: bool) -> str:
if not configured:
return "unconfigured"
if not enabled:
return "disabled"
return "ok"