Files
company-ai-platform/app/modules/feishu/service.py
JiuContinent 09933b15ad ```
feat(feishu): 添加飞书长连接支持并重构事件处理

添加了 FeishuEventService 来统一处理飞书消息事件,
新增 long_connection.py 实现长连接客户端,
修改 webhook 路由使用新的事件处理服务,
添加了 lark-oapi 依赖支持长连接功能,
更新测试用例覆盖新的事件处理逻辑。

BREAKING CHANGE: 飞书事件处理逻辑重构,统一使用
FeishuEventService 进行消息处理和审计记录。
```
2026-06-22 14:54:20 +08:00

86 lines
2.7 KiB
Python

from typing import Any
from fastapi import HTTPException, status
from sqlalchemy.orm import Session
from app.core.config import get_settings
from app.modules.audit.schemas import AuditLogCreate
from app.modules.audit.service import AuditService
from app.modules.feishu.client import FeishuClient
class FeishuService:
"""Send Feishu messages and record audit entries for outbound actions."""
def __init__(self, db: Session):
self.db = db
self.audit = AuditService(db)
self.client = FeishuClient()
def verify_event(self, payload: dict[str, Any]) -> None:
settings = get_settings()
expected = settings.feishu_verification_token
header = payload.get("header") or {}
token = payload.get("token") or header.get("token")
if expected and token and token != expected:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid Feishu token",
)
def send_text(
self,
text: str,
receive_id: str | None = None,
receive_id_type: str = "chat_id",
actor: str = "system",
) -> dict[str, Any]:
result = self.client.send_text(text, receive_id, receive_id_type)
self.audit.log(
AuditLogCreate(
actor=actor,
source="feishu",
action="send_text",
request_payload={
"receive_id": receive_id,
"receive_id_type": receive_id_type,
"text": text,
},
response_payload=result,
)
)
return result
def send_card(
self,
card: dict[str, Any],
receive_id: str | None = None,
receive_id_type: str = "chat_id",
actor: str = "system",
) -> dict[str, Any]:
result = self.client.send_card(card, receive_id, receive_id_type)
self.audit.log(
AuditLogCreate(
actor=actor,
source="feishu",
action="send_card",
request_payload={
"receive_id": receive_id,
"receive_id_type": receive_id_type,
"card": card,
},
response_payload=result,
)
)
return result
@staticmethod
def build_basic_card(title: str, lines: list[str]) -> dict[str, Any]:
return {
"config": {"wide_screen_mode": True},
"header": {"title": {"tag": "plain_text", "content": title}},
"elements": [
{"tag": "div", "text": {"tag": "lark_md", "content": "\n".join(lines) or "暂无数据"}}
],
}