File size: 5,164 Bytes
48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e 06fe0f7 48bd22e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | from fastapi import FastAPI, Depends, HTTPException, BackgroundTasks
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel
import jwt
import bcrypt
import os
import requests
from dotenv import load_dotenv
from database import create_customer, get_customer_by_email, create_ticket, get_tickets_by_customer, get_all_tickets, update_ticket_status
from models import analyze_with_groq
load_dotenv()
JWT_SECRET = os.getenv("JWT_SECRET", "multilingual_support_secret_2026")
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
security = HTTPBearer()
class RegisterRequest(BaseModel):
name: str
email: str
password: str
phone: str = ""
class LoginRequest(BaseModel):
email: str
password: str
class AnalyzeRequest(BaseModel):
text: str
class StatusRequest(BaseModel):
status: str
def get_current_user(credentials: HTTPAuthorizationCredentials = Depends(security)):
try:
payload = jwt.decode(credentials.credentials, JWT_SECRET, algorithms=["HS256"])
return payload
except Exception:
raise HTTPException(status_code=401, detail="Invalid token")
@app.get("/")
def root():
return {"status": "Multilingual Support Agent is running", "groq_key_loaded": bool(os.getenv("GROQ_API_KEY"))}
@app.post("/register")
def register(req: RegisterRequest):
existing = get_customer_by_email(req.email)
if existing:
raise HTTPException(status_code=400, detail="Email already registered")
hashed = bcrypt.hashpw(req.password.encode(), bcrypt.gensalt()).decode()
customer = create_customer(req.name, req.email, hashed, req.phone)
return {"message": "Account created successfully", "customer": customer}
@app.post("/login")
def login(req: LoginRequest):
customer = get_customer_by_email(req.email)
if not customer:
raise HTTPException(status_code=401, detail="Invalid email or password")
if not bcrypt.checkpw(req.password.encode(), customer["password"].encode()):
raise HTTPException(status_code=401, detail="Invalid email or password")
token = jwt.encode({"id": customer["id"], "email": customer["email"], "name": customer["name"]}, JWT_SECRET, algorithm="HS256")
return {"token": token, "name": customer["name"], "email": customer["email"]}
def send_to_n8n(ticket_data: dict):
webhook_url = os.getenv("N8N_WEBHOOK_URL")
auth_secret = os.getenv("N8N_AUTH_SECRET")
if not webhook_url or not auth_secret:
print("n8n webhook configuration missing. Skipping webhook.")
return
headers = {
"Authorization": f"Bearer {auth_secret}",
"Content-Type": "application/json"
}
try:
response = requests.post(webhook_url, json=ticket_data, headers=headers)
response.raise_for_status()
print(f"Successfully sent ticket {ticket_data.get('id')} to n8n.")
except Exception as e:
print(f"Failed to send ticket to n8n: {e}")
@app.post("/analyze")
def analyze(req: AnalyzeRequest, background_tasks: BackgroundTasks, user=Depends(get_current_user)):
try:
result = analyze_with_groq(req.text)
ticket = create_ticket(
customer_id=user["id"],
original_message=req.text,
language=result["language"]["language"],
translated_text=result["translation"]["translated"],
intent=result["intent"]["intent"],
sentiment=result["sentiment"]["sentiment"],
urgency=result["urgency"],
reply=result["reply"]["reply"]
)
ticket_data = ticket[0] if isinstance(ticket, list) and len(ticket) > 0 else ticket
# Inject the customer email into the payload for n8n before sending
webhook_payload = dict(ticket_data)
webhook_payload["customer_email"] = user["email"]
webhook_payload["customer_name"] = user["name"]
background_tasks.add_task(send_to_n8n, webhook_payload)
return {
"original": req.text,
"language": result["language"],
"translation": result["translation"],
"intent": result["intent"],
"sentiment": result["sentiment"],
"urgency": result["urgency"],
"reply": result["reply"],
"ticket": ticket
}
except Exception as e:
import traceback
traceback.print_exc()
return {"error": str(e), "traceback": traceback.format_exc()}
@app.get("/tickets/me")
def my_tickets(user=Depends(get_current_user)):
tickets = get_tickets_by_customer(user["id"])
return {"tickets": tickets}
@app.get("/tickets/all")
def all_tickets(user=Depends(get_current_user)):
tickets = get_all_tickets()
return {"tickets": tickets}
@app.put("/tickets/{ticket_id}/status")
def update_status(ticket_id: str, req: StatusRequest, user=Depends(get_current_user)):
ticket = update_ticket_status(ticket_id, req.status)
return {"ticket": ticket} |