feat(smartlead): Add Smartlead webhook integration [31f88f42]

This commit is contained in:
2026-03-10 07:38:47 +00:00
parent 46301f9b8c
commit 8bc5f4cbb8
3 changed files with 79 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ from zoneinfo import ZoneInfo
from threading import Thread, Lock
import uvicorn
import logging
from fastapi import FastAPI, Response, BackgroundTasks
from fastapi import FastAPI, Request, Response, BackgroundTasks
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from sqlalchemy.orm import sessionmaker
@@ -206,6 +206,50 @@ def create_calendar_invite(lead_email, company, start_time):
# --- FastAPI Server ---
app = FastAPI()
# --- Webhook Endpoints for Smartlead ---
SMARTLEAD_LOG_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), "Log")
SMARTLEAD_LOG_FILE = os.path.join(SMARTLEAD_LOG_DIR, "smartlead_webhooks.log")
# Ensure log directory exists
os.makedirs(SMARTLEAD_LOG_DIR, exist_ok=True)
async def log_webhook_data(webhook_type: str, request: Request):
"""Helper function to log incoming webhook data."""
try:
data = await request.json()
timestamp = datetime.now(TZ_BERLIN).isoformat()
log_entry = {
"timestamp": timestamp,
"webhook_type": webhook_type,
"source_ip": request.client.host,
"payload": data
}
with open(SMARTLEAD_LOG_FILE, "a", encoding="utf-8") as f:
f.write(json.dumps(log_entry) + "\n")
logging.info(f"Successfully processed and logged '{webhook_type}' webhook.")
return {"status": "success", "message": f"{webhook_type} webhook received"}
except json.JSONDecodeError:
logging.error("Webhook received with invalid JSON format.")
return Response(content='{"status": "error", "message": "Invalid JSON format"}', status_code=400, media_type="application/json")
except Exception as e:
logging.error(f"Error processing '{webhook_type}' webhook: {e}")
return Response(content='{"status": "error", "message": "Internal server error"}', status_code=500, media_type="application/json")
@app.post("/webhook/hot-lead", status_code=202)
async def webhook_hot_lead(request: Request):
"""Webhook endpoint for 'hot leads' from Smartlead."""
return await log_webhook_data("hot-lead", request)
@app.post("/webhook/follow-up-lead", status_code=202)
async def webhook_follow_up_lead(request: Request):
"""Webhook endpoint for 'follow-up leads' from Smartlead."""
return await log_webhook_data("follow-up-lead", request)
# --- END Webhook Endpoints ---
@app.get("/test_lead", status_code=202)
def trigger_test_lead(background_tasks: BackgroundTasks):
req_id = f"test_{int(time.time())}"