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

51 lines
1.5 KiB
Python

from __future__ import annotations
import logging
import secrets
import time
logger = logging.getLogger(__name__)
CODE_TTL = 300
class FeishuPairingAdapter:
id_label = "feishuOpenId"
def __init__(self):
self._codes: dict[str, tuple[str, float]] = {}
async def generate_code(self, peer_id: str) -> str:
code = f"{secrets.randbelow(1_000_000):06d}"
self._codes[peer_id] = (code, time.monotonic())
self._evict_expired()
return code
async def verify_code(self, peer_id: str, code: str) -> bool:
self._evict_expired()
entry = self._codes.get(peer_id)
if entry is None:
return False
stored_code, _ts = entry
if stored_code == code:
del self._codes[peer_id]
return True
return False
def normalize_allow_entry(self, entry: str) -> str:
entry = entry.strip()
if entry.startswith("open_id:"):
return entry[len("open_id:"):]
if entry.startswith("user:"):
return entry[len("user:"):]
return entry
async def notify_approval(self, config: dict, peer_id: str, account_id: str | None = None) -> None:
logger.info("feishu pairing approved for peer %s", peer_id)
def _evict_expired(self) -> None:
now = time.monotonic()
expired = [pid for pid, (_code, ts) in self._codes.items() if now - ts > CODE_TTL]
for pid in expired:
del self._codes[pid]