50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
"""AI获客 - 客户咨询接口"""
|
|
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
import json, os, datetime, urllib.request
|
|
|
|
router = APIRouter(prefix="/api/cma/lead", tags=["AI获客"])
|
|
|
|
class LeadRequest(BaseModel):
|
|
name: str
|
|
phone: str
|
|
requirement: str = ""
|
|
source: str = "sxbh.ltd"
|
|
|
|
def notify_wecom(name, phone, requirement, source):
|
|
"""推送到企微全员群"""
|
|
msg = {
|
|
"msgtype": "markdown",
|
|
"markdown": {
|
|
"content": f"## 🔔 新客户咨询\n**姓名**: {name}\n**电话**: {phone}\n**需求**: {requirement}\n**来源**: {source}\n**时间**: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
|
}
|
|
}
|
|
try:
|
|
data = json.dumps(msg).encode('utf-8')
|
|
req = urllib.request.Request(
|
|
"http://127.0.0.1:8800/send",
|
|
data=data,
|
|
headers={"Content-Type": "application/json"},
|
|
method="POST"
|
|
)
|
|
urllib.request.urlopen(req, timeout=5)
|
|
except:
|
|
pass # 通知失败不影响主流程
|
|
|
|
@router.post("")
|
|
def create_lead(data: LeadRequest):
|
|
"""接收客户咨询并保存"""
|
|
record = data.model_dump()
|
|
record["timestamp"] = datetime.datetime.now().isoformat()
|
|
record["status"] = "new"
|
|
|
|
log_dir = "/root/leads"
|
|
os.makedirs(log_dir, exist_ok=True)
|
|
with open(f"{log_dir}/leads.json", "a") as f:
|
|
f.write(json.dumps(record, ensure_ascii=False) + "\n")
|
|
|
|
# 异步推送企微通知
|
|
notify_wecom(data.name, data.phone, data.requirement, data.source)
|
|
|
|
return {"success": True, "message": "咨询已提交,我们将在30分钟内联系您"}
|