Initial commit: 浼佷笟寰俊 AI 鏈哄櫒浜哄姪鐞?MVP
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
67
backend/app/routers/tickets.py
Normal file
67
backend/app/routers/tickets.py
Normal file
@@ -0,0 +1,67 @@
|
||||
"""工单转人工:创建工单入库、手动回复调企业微信 API。"""
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database import get_db
|
||||
from app.deps import get_current_user_id
|
||||
from app.logging_config import get_trace_id
|
||||
from app.models import ChatSession, Ticket
|
||||
from app.services.wecom_api import send_text_to_external
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class CreateTicketBody(BaseModel):
|
||||
session_id: int
|
||||
reason: str = ""
|
||||
|
||||
|
||||
class SendReplyBody(BaseModel):
|
||||
session_id: int
|
||||
content: str
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def create_ticket(
|
||||
body: CreateTicketBody,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_user: str = Depends(get_current_user_id),
|
||||
):
|
||||
"""创建转人工工单并更新会话状态。"""
|
||||
r = await db.execute(select(ChatSession).where(ChatSession.id == body.session_id))
|
||||
session = r.scalar_one_or_none()
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="会话不存在")
|
||||
ticket = Ticket(session_id=body.session_id, reason=body.reason or None)
|
||||
db.add(ticket)
|
||||
session.status = "transferred"
|
||||
await db.flush()
|
||||
return {
|
||||
"code": 0,
|
||||
"message": "ok",
|
||||
"data": {"ticket_id": str(ticket.id)},
|
||||
"trace_id": get_trace_id(),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/reply")
|
||||
async def send_reply(
|
||||
body: SendReplyBody,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
_user: str = Depends(get_current_user_id),
|
||||
):
|
||||
"""手动回复:通过企业微信 API 发给客户。"""
|
||||
r = await db.execute(select(ChatSession).where(ChatSession.id == body.session_id))
|
||||
session = r.scalar_one_or_none()
|
||||
if not session:
|
||||
raise HTTPException(status_code=404, detail="会话不存在")
|
||||
try:
|
||||
await send_text_to_external(session.external_user_id, body.content)
|
||||
except Exception as e:
|
||||
logger.exception("wecom send reply failed")
|
||||
raise HTTPException(status_code=502, detail=str(e))
|
||||
return {"code": 0, "message": "ok", "data": None, "trace_id": get_trace_id()}
|
||||
Reference in New Issue
Block a user