```
feat(ai_agent): 完善AI适配器和服务功能 - 添加OpenClaw和Hermes健康检查接口 - 实现OpenClaw工具调用功能 - 重构AI适配器使用常量定义 - 增加AI技能系统支持 - 更新配置文件中的默认模型提供者设置 refactor(scheduler): 使用常量替换硬编码值 - 将硬编码的actor值替换为ActorValue常量 - 将receive_id_type替换为FeishuReceiveIdType枚举 refactor(audit): 统一审计日志常量使用 - 将硬编码的actor、source、risk_level等值替换为对应常量 - 更新审核服务中的状态和操作常量引用 refactor(approvals): 标准化审批模块常量使用 - 将applicant默认值替换为ActorValue.API常量 - 使用ApprovalStatus常量替代硬编码状态值 - 更新审核操作常量引用 ```
This commit is contained in:
@@ -5,7 +5,20 @@ from typing import Any
|
||||
import httpx
|
||||
from fastapi import HTTPException
|
||||
|
||||
from app.core.constants import BEARER_TOKEN_TEMPLATE, HttpHeader
|
||||
from app.core.config import get_settings
|
||||
from app.modules.feishu.constants import (
|
||||
FEISHU_AUTH_MISSING,
|
||||
FEISHU_DEFAULT_TOKEN_EXPIRE_SECONDS,
|
||||
FEISHU_MESSAGE_PATH,
|
||||
FEISHU_RECEIVE_ID_MISSING,
|
||||
FEISHU_SUCCESS_CODE,
|
||||
FEISHU_TENANT_TOKEN_PATH,
|
||||
FEISHU_TOKEN_EXPIRE_SAFETY_SECONDS,
|
||||
FeishuMessageType,
|
||||
FeishuPayloadKey,
|
||||
FeishuReceiveIdType,
|
||||
)
|
||||
|
||||
|
||||
class FeishuClient:
|
||||
@@ -21,23 +34,26 @@ class FeishuClient:
|
||||
|
||||
def _get_tenant_access_token(self) -> str:
|
||||
if not self._is_configured():
|
||||
raise HTTPException(status_code=503, detail="Feishu app credentials are not configured")
|
||||
raise HTTPException(status_code=503, detail=FEISHU_AUTH_MISSING)
|
||||
if self._tenant_access_token and time.time() < self._token_expires_at:
|
||||
return self._tenant_access_token
|
||||
|
||||
url = f"{self.settings.feishu_base_url}/auth/v3/tenant_access_token/internal"
|
||||
url = f"{self.settings.feishu_base_url}{FEISHU_TENANT_TOKEN_PATH}"
|
||||
payload = {
|
||||
"app_id": self.settings.feishu_app_id,
|
||||
"app_secret": self.settings.feishu_app_secret,
|
||||
FeishuPayloadKey.APP_ID: self.settings.feishu_app_id,
|
||||
FeishuPayloadKey.APP_SECRET: self.settings.feishu_app_secret,
|
||||
}
|
||||
with httpx.Client(timeout=20) as client:
|
||||
response = client.post(url, json=payload)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
if data.get("code") != 0:
|
||||
raise HTTPException(status_code=502, detail={"feishu_error": data})
|
||||
self._tenant_access_token = data["tenant_access_token"]
|
||||
self._token_expires_at = time.time() + int(data.get("expire", 7200)) - 300
|
||||
if data.get(FeishuPayloadKey.CODE) != FEISHU_SUCCESS_CODE:
|
||||
raise HTTPException(status_code=502, detail={FeishuPayloadKey.FEISHU_ERROR: data})
|
||||
self._tenant_access_token = data[FeishuPayloadKey.TENANT_ACCESS_TOKEN]
|
||||
expire_seconds = int(
|
||||
data.get(FeishuPayloadKey.EXPIRE, FEISHU_DEFAULT_TOKEN_EXPIRE_SECONDS)
|
||||
)
|
||||
self._token_expires_at = time.time() + expire_seconds - FEISHU_TOKEN_EXPIRE_SAFETY_SECONDS
|
||||
return self._tenant_access_token
|
||||
|
||||
def send_message(
|
||||
@@ -48,13 +64,13 @@ class FeishuClient:
|
||||
content: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
token = self._get_tenant_access_token()
|
||||
url = f"{self.settings.feishu_base_url}/im/v1/messages"
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
params = {"receive_id_type": receive_id_type}
|
||||
url = f"{self.settings.feishu_base_url}{FEISHU_MESSAGE_PATH}"
|
||||
headers = {HttpHeader.AUTHORIZATION: BEARER_TOKEN_TEMPLATE.format(token=token)}
|
||||
params = {FeishuPayloadKey.RECEIVE_ID_TYPE: receive_id_type}
|
||||
payload = {
|
||||
"receive_id": receive_id,
|
||||
"msg_type": msg_type,
|
||||
"content": json.dumps(content, ensure_ascii=False),
|
||||
FeishuPayloadKey.RECEIVE_ID: receive_id,
|
||||
FeishuPayloadKey.MESSAGE_TYPE: msg_type,
|
||||
FeishuPayloadKey.CONTENT: json.dumps(content, ensure_ascii=False),
|
||||
}
|
||||
with httpx.Client(timeout=20) as client:
|
||||
response = client.post(url, headers=headers, params=params, json=payload)
|
||||
@@ -66,26 +82,31 @@ class FeishuClient:
|
||||
self,
|
||||
text: str,
|
||||
receive_id: str | None = None,
|
||||
receive_id_type: str = "chat_id",
|
||||
receive_id_type: str = FeishuReceiveIdType.CHAT_ID,
|
||||
) -> dict:
|
||||
chat_id = receive_id or self.settings.feishu_default_chat_id
|
||||
if not chat_id:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="receive_id or FEISHU_DEFAULT_CHAT_ID is required",
|
||||
detail=FEISHU_RECEIVE_ID_MISSING,
|
||||
)
|
||||
return self.send_message(chat_id, receive_id_type, "text", {"text": text})
|
||||
return self.send_message(
|
||||
chat_id,
|
||||
receive_id_type,
|
||||
FeishuMessageType.TEXT,
|
||||
{FeishuPayloadKey.TEXT: text},
|
||||
)
|
||||
|
||||
def send_card(
|
||||
self,
|
||||
card: dict[str, Any],
|
||||
receive_id: str | None = None,
|
||||
receive_id_type: str = "chat_id",
|
||||
receive_id_type: str = FeishuReceiveIdType.CHAT_ID,
|
||||
) -> dict:
|
||||
chat_id = receive_id or self.settings.feishu_default_chat_id
|
||||
if not chat_id:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="receive_id or FEISHU_DEFAULT_CHAT_ID is required",
|
||||
detail=FEISHU_RECEIVE_ID_MISSING,
|
||||
)
|
||||
return self.send_message(chat_id, receive_id_type, "interactive", card)
|
||||
return self.send_message(chat_id, receive_id_type, FeishuMessageType.INTERACTIVE, card)
|
||||
|
||||
Reference in New Issue
Block a user