新增小红书、XMPP、元宝、Zalo 四个渠道扩展。 小红书渠道扩展主要模块:config, gateway, webhook, outbound, streaming, pairing, security, dedupe, media, status, window XMPP 渠道扩展主要模块:plugin, config, gateway, outbound, streaming, pairing, security, dedupe, accounts, commands, muc, rate_limiter, stanza_utils, status, monitor 元宝渠道扩展主要模块:plugin, client, config_schema, gateway, outbound(chunk/queue/transport), inbound(dispatcher), streaming, pairing, security, accounts, actions, commands, codec(biz/conn), session, shared, utils Zalo 渠道扩展主要模块:api, config, gateway, webhook, outbound, pairing, security, session, polling, monitor, status
93 lines
3.7 KiB
Python
93 lines
3.7 KiB
Python
import time
|
|
import logging
|
|
|
|
from yuxi.channel.extensions.zalo.api import ZaloBotApi
|
|
from yuxi.channel.protocols import ChannelAccountSnapshot, build_standard_summary
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ZaloStatus:
|
|
default_runtime = ChannelAccountSnapshot(account_id="default")
|
|
|
|
async def probe(self, account: dict | None = None, timeout_ms: int = 5000) -> dict:
|
|
if account is None:
|
|
return {"ok": False, "error": "No account provided", "elapsed_ms": 0}
|
|
|
|
bot_token = account.get("bot_token", "")
|
|
if not bot_token:
|
|
return {"ok": False, "error": "Missing bot_token", "elapsed_ms": 0}
|
|
|
|
start = time.monotonic()
|
|
api = ZaloBotApi(bot_token, proxy=account.get("proxy"))
|
|
try:
|
|
result = await api.get_me()
|
|
elapsed_ms = int((time.monotonic() - start) * 1000)
|
|
bot_info = result.get("result", {})
|
|
return {
|
|
"ok": True,
|
|
"bot": {
|
|
"id": bot_info.get("id", ""),
|
|
"name": bot_info.get("name", ""),
|
|
"avatar": bot_info.get("avatar", ""),
|
|
"can_join_groups": bot_info.get("can_join_groups", False),
|
|
},
|
|
"elapsed_ms": elapsed_ms,
|
|
}
|
|
except Exception as e:
|
|
elapsed_ms = int((time.monotonic() - start) * 1000)
|
|
return {"ok": False, "error": str(e), "elapsed_ms": elapsed_ms}
|
|
finally:
|
|
await api.close()
|
|
|
|
def build_summary(self, snapshot: object) -> dict:
|
|
if not isinstance(snapshot, ChannelAccountSnapshot):
|
|
return {}
|
|
return build_standard_summary(snapshot, "zalo")
|
|
|
|
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("bot_token")),
|
|
status_state="linked" if account.get("bot_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 and getattr(probe_result, "ok", False) 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": "zalo",
|
|
"account_id": acc.account_id,
|
|
"kind": "not-configured",
|
|
"message": "Zalo bot_token not configured",
|
|
"fix": "Set ZALO_BOT_TOKEN environment variable or configure in channel settings",
|
|
}
|
|
)
|
|
elif not acc.connected:
|
|
issues.append(
|
|
{
|
|
"channel": "zalo",
|
|
"account_id": acc.account_id,
|
|
"kind": "not-connected",
|
|
"message": "Zalo gateway not running",
|
|
"fix": "Check gateway status and restart if needed",
|
|
}
|
|
)
|
|
return issues
|