PMFlow/backend/app/api/feishu_api/message_routes.py

77 lines
2.3 KiB
Python

"""Feishu message sending routes."""
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from ...core.database import get_db
from ...dependencies.auth import get_current_user
from ...dependencies.tenant import get_tenant_id
from ...models import User
from ...services.integrations.feishu_integration_service import get_feishu_service
from .schemas import FeishuSendCardRequest, FeishuSendMessageRequest
router = APIRouter()
@router.post("/send-message")
async def send_feishu_message(
request: FeishuSendMessageRequest,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
"""
发送文本消息到飞书群。
用于发送简报、通知等。
"""
_ = current_user
try:
service = get_feishu_service(db)
await service.load_config(tenant_id)
result = await service.send_message_to_group(
chat_id=request.chat_id,
message=request.message,
)
return {
"status": "success",
"result": result,
}
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
except Exception as exc:
raise HTTPException(status_code=500, detail=f"发送消息失败: {str(exc)}")
@router.post("/send-card")
async def send_feishu_card(
request: FeishuSendCardRequest,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
tenant_id: int = Depends(get_tenant_id),
):
"""
发送富文本卡片到飞书群。
用于发送格式化的每日简报、风险预警等。
"""
_ = current_user
try:
service = get_feishu_service(db)
await service.load_config(tenant_id)
result = await service.send_rich_card(
chat_id=request.chat_id,
title=request.title,
content=request.content,
)
return {
"status": "success",
"result": result,
}
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc))
except Exception as exc:
raise HTTPException(status_code=500, detail=f"发送卡片失败: {str(exc)}")