ForcePilot/backend/package/yuxi/channel/extensions/feishu/probe.py
Kris 5e91bb9985 feat(feishu): 新增飞书渠道完整插件实现
新增飞书渠道插件,包含基础配置、事件处理、消息收发、工具调用、权限管理等完整功能模块,支持WebSocket长连接、卡片消息、流式响应、群管理、审批通知等能力
2026-05-21 10:46:42 +08:00

59 lines
1.8 KiB
Python

from __future__ import annotations
import logging
import time
from yuxi.channel.extensions.feishu.client import get_client, get_bot_info
logger = logging.getLogger(__name__)
_probe_cache: dict[str, dict] = {}
PROBE_CACHE_MAX = 64
PROBE_TTL_SUCCESS = 600
PROBE_TTL_FAILURE = 60
async def probe_feishu(app_id: str, app_secret: str, domain: str = "feishu", http_timeout_ms: int = 30000) -> dict:
cache_key = f"{app_id}:{domain}"
cached = _probe_cache.get(cache_key)
now = time.monotonic()
if cached:
ttl = PROBE_TTL_SUCCESS if cached["ok"] else PROBE_TTL_FAILURE
if now - cached["ts"] < ttl:
return cached
result = {"ok": False, "app_id": app_id, "bot_name": "", "bot_open_id": ""}
try:
client = get_client(app_id, app_secret, domain, http_timeout_ms)
info = await get_bot_info(client)
if info:
result["ok"] = True
result["bot_name"] = info.get("bot_name", "")
result["bot_open_id"] = info.get("open_id", "")
result["activate_status"] = info.get("activate_status", 0)
except Exception:
logger.debug("Feishu probe failed for %s", app_id, exc_info=True)
result["ts"] = now
_probe_cache[cache_key] = result
if len(_probe_cache) > PROBE_CACHE_MAX:
oldest = min(_probe_cache, key=lambda k: _probe_cache[k]["ts"])
del _probe_cache[oldest]
return result
async def probe_account(account: dict) -> bool:
app_id = account.get("app_id") or account.get("appId", "")
app_secret = account.get("app_secret") or account.get("appSecret", "")
domain = account.get("domain", "feishu")
http_timeout_ms = account.get("http_timeout_ms", 30000)
if not app_id or not app_secret:
return False
result = await probe_feishu(app_id, app_secret, domain, http_timeout_ms)
return result.get("ok", False)