import os import re import json import time import asyncio from datetime import datetime from typing import List, Dict, Any, Optional, Union from pydantic import BaseModel, Field, EmailStr, field_validator from fastapi import FastAPI, HTTPException, Query, Depends, Request from fastapi.responses import JSONResponse, Response from fastapi.middleware.cors import CORSMiddleware from fastapi.openapi.utils import get_openapi import httpx from dotenv import load_dotenv import pandas as pd import psycopg2 from sqlalchemy import create_engine, inspect, text # LangChain and OpenAI imports try: from langchain_openai import ChatOpenAI from langchain.prompts import ChatPromptTemplate LANGCHAIN_AVAILABLE = True except ImportError: LANGCHAIN_AVAILABLE = False print("Warning: LangChain not available. Install with: pip install langchain langchain-openai") load_dotenv() # Configuration SMARTLEAD_API_KEY = os.getenv("SMARTLEAD_API_KEY", "your-api-key-here") SMARTLEAD_BASE_URL = "https://server.smartlead.ai/api/v1" DB_PARAMS = { 'dbname': os.getenv("DB_NAME"), 'user': os.getenv("DB_USER"), 'password': os.getenv("DB_PASSWORD"), 'host': os.getenv("DB_HOST"), 'port': os.getenv("DB_PORT") } DATABASE_URL = f"postgresql://{DB_PARAMS['user']}:{DB_PARAMS['password']}@{DB_PARAMS['host']}:{DB_PARAMS['port']}/{DB_PARAMS['dbname']}" # Initialize FastAPI app app = FastAPI( title="Smartlead API - Complete Integration", version="2.1.0", description="Comprehensive FastAPI wrapper for Smartlead email automation platform", docs_url="/docs", redoc_url="/redoc" ) # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # ============================================================================ # DATA MODELS # ============================================================================ class CreateCampaignRequest(BaseModel): name: str = Field(..., description="Campaign name") client_id: Optional[int] = Field(None, description="Client ID (leave null if no client)") class CampaignScheduleRequest(BaseModel): timezone: str = Field(..., description="Timezone for the campaign schedule (e.g., 'America/Los_Angeles')") days_of_the_week: List[int] = Field(..., description="Days of the week for scheduling [0=Sunday, 1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday]") start_hour: str = Field(..., description="Start hour for sending emails in HH:MM format (e.g., '09:00')") end_hour: str = Field(..., description="End hour for sending emails in HH:MM format (e.g., '18:00')") min_time_btw_emails: int = Field(..., description="Minimum time in minutes between sending emails") max_new_leads_per_day: int = Field(..., description="Maximum number of new leads to process per day") schedule_start_time: str = Field(..., description="Schedule start time in ISO 8601 format (e.g., '2023-04-25T07:29:25.978Z')") class CampaignSettingsRequest(BaseModel): track_settings: List[str] = Field(..., description="Tracking settings array (allowed values: DONT_TRACK_EMAIL_OPEN, DONT_TRACK_LINK_CLICK, DONT_TRACK_REPLY_TO_AN_EMAIL)") stop_lead_settings: str = Field(..., description="Settings for stopping leads (allowed values: CLICK_ON_A_LINK, OPEN_AN_EMAIL)") unsubscribe_text: str = Field(..., description="Text for the unsubscribe link") send_as_plain_text: bool = Field(..., description="Whether emails should be sent as plain text") follow_up_percentage: int = Field(ge=0, le=100, description="Follow-up percentage (max 100, min 0)") client_id: Optional[int] = Field(None, description="Client ID (leave as null if not needed)") enable_ai_esp_matching: bool = Field(False, description="Enable AI ESP matching (by default is false)") class LeadInput(BaseModel): first_name: Optional[str] = Field(None, description="Lead's first name") last_name: Optional[str] = Field(None, description="Lead's last name") email: str = Field(..., description="Lead's email address") phone_number: Optional[Union[str, int]] = Field(None, description="Lead's phone number (can be string or integer)") company_name: Optional[str] = Field(None, description="Lead's company name") website: Optional[str] = Field(None, description="Lead's website") location: Optional[str] = Field(None, description="Lead's location") custom_fields: Optional[Dict[str, str]] = Field(None, description="Custom fields as key-value pairs (max 20 fields)") linkedin_profile: Optional[str] = Field(None, description="Lead's LinkedIn profile URL") company_url: Optional[str] = Field(None, description="Company website URL") @field_validator('custom_fields') @classmethod def validate_custom_fields(cls, v): if v is not None and len(v) > 20: raise ValueError('Custom fields cannot exceed 20 fields') return v @field_validator('phone_number') @classmethod def validate_phone_number(cls, v): if v is not None: return str(v) return v class LeadSettings(BaseModel): ignore_global_block_list: bool = Field(True, description="Ignore leads if they are in the global block list") ignore_unsubscribe_list: bool = Field(True, description="Ignore leads if they are in the unsubscribe list") ignore_duplicate_leads_in_other_campaign: bool = Field(False, description="Allow leads to be added even if they are duplicates in other campaigns") class AddLeadsRequest(BaseModel): lead_list: List[LeadInput] = Field(..., max_items=100, description="List of leads to add (maximum 100 leads)") settings: Optional[LeadSettings] = Field(None, description="Settings for lead processing") class AddLeadsResponse(BaseModel): ok: bool = Field(..., description="Indicates if the operation was successful") upload_count: int = Field(..., description="Number of leads successfully uploaded") total_leads: int = Field(..., description="Total number of leads attempted to upload") already_added_to_campaign: int = Field(..., description="Number of leads already present in the campaign") duplicate_count: int = Field(..., description="Number of duplicate emails found") invalid_email_count: int = Field(..., description="Number of leads with invalid email format") unsubscribed_leads: Any = Field(..., description="Number of leads that had previously unsubscribed (can be int or empty list)") class SeqDelayDetails(BaseModel): delay_in_days: int = Field(..., description="Delay in days before sending this sequence") class SeqVariant(BaseModel): subject: str = Field(..., description="Email subject line") email_body: str = Field(..., description="Email body content (HTML format)") variant_label: str = Field(..., description="Variant label (A, B, C, etc.)") id: Optional[int] = Field(None, description="Variant ID (only for updating, not for creating)") class CampaignSequence(BaseModel): id: Optional[int] = Field(None, description="Sequence ID (only for updating, not for creating)") seq_number: int = Field(..., description="Sequence number (1, 2, 3, etc.)") seq_delay_details: SeqDelayDetails = Field(..., description="Delay details for this sequence") seq_variants: Optional[List[SeqVariant]] = Field(None, description="Email variants for A/B testing") subject: Optional[str] = Field("", description="Subject line (blank for follow-up in same thread)") email_body: Optional[str] = Field(None, description="Email body content (HTML format)") class SaveSequencesRequest(BaseModel): sequences: List[CampaignSequence] = Field(..., description="List of campaign sequences") class Job(BaseModel): id: str company_name: Optional[str] = None job_title: Optional[str] = None job_location: Optional[str] = None company_blurb: Optional[str] = None company_culture: Optional[str] = None description: Optional[str] = None company_size: Optional[str] = None requirements: Optional[str] = None salary: Optional[str] = None class GenerateSequencesRequest(BaseModel): job_id: str = Field(..., description="Job ID to fetch from database and generate sequences for") class Campaign(BaseModel): id: int user_id: int created_at: datetime updated_at: datetime status: str name: str track_settings: Union[str, List[Any]] scheduler_cron_value: Optional[Union[str, Dict[str, Any]]] = None min_time_btwn_emails: int max_leads_per_day: int stop_lead_settings: str unsubscribe_text: Optional[str] = None client_id: Optional[int] = None enable_ai_esp_matching: bool send_as_plain_text: bool follow_up_percentage: Optional[Union[str, int]] = None class CampaignListResponse(BaseModel): campaigns: List[Campaign] total: int source: str class Lead(BaseModel): id: int email: EmailStr first_name: Optional[str] = None last_name: Optional[str] = None company: Optional[str] = None position: Optional[str] = None phone_number: Optional[str] = None linkedin_url: Optional[str] = None status: Optional[str] = None class WarmupDetails(BaseModel): status: str total_sent_count: int total_spam_count: int warmup_reputation: str warmup_key_id: Optional[str] = None warmup_created_at: Optional[datetime] = None reply_rate: int blocked_reason: Optional[str] = None class EmailAccount(BaseModel): id: int created_at: datetime updated_at: datetime user_id: int from_name: str from_email: str username: str password: Optional[str] = None smtp_host: Optional[str] = None smtp_port: Optional[int] = None smtp_port_type: Optional[str] = None message_per_day: int different_reply_to_address: Optional[str] = None is_different_imap_account: bool imap_username: Optional[str] = None imap_password: Optional[str] = None imap_host: Optional[str] = None imap_port: Optional[int] = None imap_port_type: Optional[str] = None signature: Optional[str] = None custom_tracking_domain: Optional[str] = None bcc_email: Optional[str] = None is_smtp_success: bool is_imap_success: bool smtp_failure_error: Optional[str] = None imap_failure_error: Optional[str] = None type: str daily_sent_count: int client_id: Optional[int] = None campaign_count: Optional[int] = None warmup_details: Optional[WarmupDetails] = None class LeadCategoryUpdateRequest(BaseModel): category_id: int = Field(..., description="Category ID to assign to the lead") pause_lead: bool = Field(False, description="Whether to pause the lead after category update") class CampaignStatusUpdateRequest(BaseModel): status: str = Field(..., description="New campaign status (PAUSED, STOPPED, START)") class ResumeLeadRequest(BaseModel): resume_lead_with_delay_days: Optional[int] = Field(None, description="Delay in days before resuming (defaults to 0)") class DomainBlockListRequest(BaseModel): domain_block_list: List[str] = Field(..., description="List of domains/emails to block") client_id: Optional[int] = Field(None, description="Client ID if blocking is client-specific") class WebhookRequest(BaseModel): id: Optional[int] = Field(None, description="Webhook ID (null for creating new)") name: str = Field(..., description="Webhook name") webhook_url: str = Field(..., description="Webhook URL") event_types: List[str] = Field(..., description="List of event types to listen for") categories: Optional[List[str]] = Field(None, description="List of categories to filter by") class WebhookDeleteRequest(BaseModel): id: int = Field(..., description="Webhook ID to delete") class ClientRequest(BaseModel): name: str = Field(..., description="Client name") email: str = Field(..., description="Client email") permission: List[str] = Field(..., description="List of permissions") logo: Optional[str] = Field(None, description="Client logo text") logo_url: Optional[str] = Field(None, description="Client logo URL") password: str = Field(..., description="Client password") class MessageHistoryRequest(BaseModel): email_stats_id: str = Field(..., description="Email stats ID for the specific email") email_body: str = Field(..., description="Reply message email body") reply_message_id: str = Field(..., description="Message ID to reply to") reply_email_time: str = Field(..., description="Time of the message being replied to") reply_email_body: str = Field(..., description="Body of the message being replied to") cc: Optional[str] = Field(None, description="CC recipients") bcc: Optional[str] = Field(None, description="BCC recipients") add_signature: bool = Field(True, description="Whether to add signature") class AddLeadsAndSequencesRequest(BaseModel): lead_list: List[LeadInput] = Field(..., max_items=100, description="List of leads to add (maximum 100 leads)") settings: Optional[LeadSettings] = Field(None, description="Settings for lead processing") job_id: str = Field(..., description="Job ID to fetch from database and generate sequences for") # ============================================================================ # HELPER FUNCTIONS # ============================================================================ def get_database_connection(): """Get database connection""" try: conn_string = f"postgresql://{DB_PARAMS['user']}:{DB_PARAMS['password']}@{DB_PARAMS['host']}:{DB_PARAMS['port']}/{DB_PARAMS['dbname']}" return create_engine(conn_string) except Exception as e: raise HTTPException(status_code=500, detail=f"Database connection failed: {str(e)}") def fetch_job_by_id(job_id: str) -> Job: """Fetch job details from database by ID using pandas DataFrame""" try: conn = get_database_connection() df = pd.read_sql_table("jobs", con=conn) # Filter the DataFrame to find the job with the specified ID job_row = df[df['job_id'] == job_id] if job_row.empty: raise HTTPException(status_code=404, detail=f"Job with ID {job_id} not found") # Get the first (and should be only) row return Job( id=str(job_row.iloc[0]['job_id']), company_name=str(job_row.iloc[0]['company_name']) if pd.notna(job_row.iloc[0]['company_name']) else None, job_title=str(job_row.iloc[0]['job_title']) if pd.notna(job_row.iloc[0]['job_title']) else None, job_location=str(job_row.iloc[0]['job_location']) if pd.notna(job_row.iloc[0]['job_location']) else None, company_blurb=str(job_row.iloc[0]['company_blurb']) if pd.notna(job_row.iloc[0]['company_blurb']) else None, company_culture=str(job_row.iloc[0]['company_culture']) if pd.notna(job_row.iloc[0]['company_culture']) else None, description=str(job_row.iloc[0]['description']) if pd.notna(job_row.iloc[0]['description']) else None, company_size=str(job_row.iloc[0]['company_size']) if pd.notna(job_row.iloc[0]['company_size']) else None, requirements=str(job_row.iloc[0]['requirements']) if pd.notna(job_row.iloc[0]['requirements']) else None, salary=str(job_row.iloc[0]['compensation_benefits']) if pd.notna(job_row.iloc[0]['compensation_benefits']) else None ) except HTTPException: raise except Exception as e: raise HTTPException(status_code=500, detail=f"Error fetching job: {str(e)}") def list_available_jobs(limit: int = 10) -> List[Dict[str, Any]]: """List available jobs from database using pandas DataFrame""" try: conn = get_database_connection() df = pd.read_sql_table("jobs", con=conn) # Select only the required columns and limit the results selected_columns = ['job_id', 'company_name', 'job_title', 'job_location', 'company_size', 'salary'] df_subset = df[selected_columns].head(limit) # Convert DataFrame to list of dictionaries jobs_list = [] for _, row in df_subset.iterrows(): jobs_list.append({ "id": str(row['job_id']), # Ensure ID is returned as string "company_name": str(row['company_name']) if pd.notna(row['company_name']) else None, "job_title": str(row['job_title']) if pd.notna(row['job_title']) else None, "job_location": str(row['job_location']) if pd.notna(row['job_location']) else None, "company_size": str(row['company_size']) if pd.notna(row['company_size']) else None, "salary": str(row['salary']) if pd.notna(row['salary']) else None }) return jobs_list except Exception as e: raise HTTPException(status_code=500, detail=f"Error fetching jobs: {str(e)}") def build_job_description(job: Job) -> str: """Build a comprehensive job description from job details""" parts = [] if job.company_name: parts.append(f"Company: {job.company_name}") if job.job_title: parts.append(f"Position: {job.job_title}") if job.job_location: parts.append(f"Location: {job.job_location}") if job.company_size: parts.append(f"Company Size: {job.company_size}") if job.salary: parts.append(f"Salary: {job.salary}") if job.company_blurb: parts.append(f"About the Company: {job.company_blurb}") if job.company_culture: parts.append(f"Company Culture: {job.company_culture}") if job.description: parts.append(f"Job Description: {job.description}") if job.requirements: parts.append(f"Requirements: {job.requirements}") return "\n\n".join(parts) def _get_smartlead_url(endpoint: str) -> str: return f"{SMARTLEAD_BASE_URL}/{endpoint.lstrip('/')}" async def call_smartlead_api(method: str, endpoint: str, data: Any = None, params: Dict[str, Any] = None) -> Any: if SMARTLEAD_API_KEY == "your-api-key-here": raise HTTPException(status_code=400, detail="Smartlead API key not configured") if params is None: params = {} params['api_key'] = SMARTLEAD_API_KEY url = _get_smartlead_url(endpoint) try: async with httpx.AsyncClient(timeout=30.0) as client: if method.upper() in ("GET", "DELETE"): resp = await client.request(method, url, params=params) else: resp = await client.request(method, url, params=params, json=data) if resp.status_code >= 400: try: error_data = resp.json() error_message = error_data.get('message', error_data.get('error', 'Unknown error')) raise HTTPException(status_code=resp.status_code, detail=error_message) except (ValueError, KeyError): raise HTTPException(status_code=resp.status_code, detail=resp.text) return resp.json() except httpx.TimeoutException: raise HTTPException(status_code=408, detail="Request to Smartlead API timed out") except httpx.RequestError as e: raise HTTPException(status_code=503, detail=f"Failed to connect to Smartlead API: {str(e)}") # ============================================================================ # CAMPAIGN ENDPOINTS # ============================================================================ @app.post("/campaigns/create", response_model=Dict[str, Any], tags=["Campaigns"]) async def create_campaign(campaign: CreateCampaignRequest): """Create a new campaign in Smartlead""" return await call_smartlead_api("POST", "campaigns/create", data=campaign.dict()) @app.get("/campaigns", response_model=CampaignListResponse, tags=["Campaigns"]) async def list_campaigns(): """Fetch all campaigns from Smartlead API""" campaigns = await call_smartlead_api("GET", "campaigns") return {"campaigns": campaigns, "total": len(campaigns), "source": "smartlead"} @app.get("/campaigns/{campaign_id}", response_model=Campaign, tags=["Campaigns"]) async def get_campaign(campaign_id: int): """Get Campaign By Id""" return await call_smartlead_api("GET", f"campaigns/{campaign_id}") @app.post("/campaigns/{campaign_id}/settings", response_model=Dict[str, Any], tags=["Campaigns"]) async def update_campaign_settings(campaign_id: int, settings: CampaignSettingsRequest): """Update Campaign General Settings""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/settings", data=settings.dict()) @app.post("/campaigns/{campaign_id}/schedule", response_model=Dict[str, Any], tags=["Campaigns"]) async def schedule_campaign(campaign_id: int, schedule: CampaignScheduleRequest): """Update Campaign Schedule""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/schedule", data=schedule.dict()) @app.delete("/campaigns/{campaign_id}", response_model=Dict[str, Any], tags=["Campaigns"]) async def delete_campaign(campaign_id: int): """Delete Campaign""" return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}") @app.post("/campaigns/{campaign_id}/status", response_model=Dict[str, Any], tags=["Campaigns"]) async def patch_campaign_status(campaign_id: int, request: CampaignStatusUpdateRequest): """Patch campaign status""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/status", data=request.dict()) @app.get("/campaigns/{campaign_id}/analytics", response_model=Any, tags=["Analytics"]) async def campaign_analytics(campaign_id: int): """Fetch analytics for a campaign""" return await call_smartlead_api("GET", f"campaigns/{campaign_id}/analytics") @app.get("/campaigns/{campaign_id}/statistics", response_model=Dict[str, Any], tags=["Analytics"]) async def fetch_campaign_statistics_by_campaign_id( campaign_id: int, offset: int = 0, limit: int = 100, email_sequence_number: Optional[int] = None, email_status: Optional[str] = None ): """Fetch Campaign Statistics By Campaign Id""" params = {"offset": offset, "limit": limit} if email_sequence_number: params["email_sequence_number"] = email_sequence_number if email_status: params["email_status"] = email_status return await call_smartlead_api("GET", f"campaigns/{campaign_id}/statistics", params=params) @app.get("/campaigns/{campaign_id}/analytics-by-date", response_model=Dict[str, Any], tags=["Analytics"]) async def fetch_campaign_statistics_by_date_range( campaign_id: int, start_date: str, end_date: str ): """Fetch Campaign Statistics By Campaign Id And Date Range""" params = {"start_date": start_date, "end_date": end_date} return await call_smartlead_api("GET", f"campaigns/{campaign_id}/analytics-by-date", params=params) # ============================================================================ # LEAD MANAGEMENT ENDPOINTS # ============================================================================ @app.get("/campaigns/{campaign_id}/leads", response_model=Dict[str, Any], tags=["Leads"]) async def get_campaign_leads(campaign_id: int, offset: int = 0, limit: int = 100): """List all leads by campaign id""" params = {"offset": offset, "limit": limit} return await call_smartlead_api("GET", f"campaigns/{campaign_id}/leads", params=params) # *** MODIFIED: add_leads_to_campaign now uses asyncio.gather for performance *** @app.post("/campaigns/{campaign_id}/leads", response_model=Dict[str, Any], tags=["Leads"]) async def add_leads_to_campaign(campaign_id: int, request: AddLeadsRequest): """Add leads to a campaign by ID with personalized welcome and closing messages""" async def process_lead(lead: Dict[str, Any]) -> Dict[str, Any]: """Inner function to process a single lead.""" lead_cleaned = {k: v for k, v in lead.items() if v is not None and v != ""} try: personalized_messages = await generate_welcome_closing_messages(lead_cleaned) if "custom_fields" not in lead_cleaned: lead_cleaned["custom_fields"] = {} lead_cleaned["custom_fields"]["Welcome_Message"] = personalized_messages.get("welcome_message", "") lead_cleaned["custom_fields"]["Closing_Message"] = personalized_messages.get("closing_message", "") except Exception as e: print(f"Error generating AI messages for {lead.get('email')}: {e}. Falling back to template.") template_messages = generate_template_welcome_closing_messages(lead_cleaned) if "custom_fields" not in lead_cleaned: lead_cleaned["custom_fields"] = {} lead_cleaned["custom_fields"]["Welcome_Message"] = template_messages["welcome_message"] lead_cleaned["custom_fields"]["Closing_Message"] = template_messages["closing_message"] return lead_cleaned # Create a list of concurrent tasks for AI processing tasks = [process_lead(lead.dict()) for lead in request.lead_list] processed_leads = await asyncio.gather(*tasks) # Prepare the final request data for Smartlead request_data = { "lead_list": processed_leads, "settings": request.settings.dict() if request.settings else LeadSettings().dict() } return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads", data=request_data) @app.post("/campaigns/{campaign_id}/leads/bulk", response_model=Dict[str, Any], tags=["Leads"]) async def add_bulk_leads(campaign_id: int, leads: List[LeadInput]): """Add multiple leads to a Smartlead campaign with personalized messages (legacy endpoint)""" request = AddLeadsRequest(lead_list=leads) return await add_leads_to_campaign(campaign_id, request) @app.post("/campaigns/{campaign_id}/leads-and-sequences", response_model=Dict[str, Any], tags=["Leads"]) async def add_leads_and_generate_sequences(campaign_id: int, request: AddLeadsAndSequencesRequest): """Add leads to campaign and immediately generate informed sequences using their data""" # Step 1: Add leads with personalized messages leads_request = AddLeadsRequest(lead_list=request.lead_list, settings=request.settings) leads_result = await add_leads_to_campaign(campaign_id, leads_request) # Step 2: Fetch job details and generate informed sequences try: job = fetch_job_by_id(request.job_id) job_description = build_job_description(job) generated_sequences = await generate_sequences_with_llm(job_description, campaign_id) save_request = SaveSequencesRequest(sequences=generated_sequences) sequences_result = await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=save_request.dict()) except Exception as e: print(f"Error generating sequences after adding leads: {str(e)}") # Fallback to generic sequences job = fetch_job_by_id(request.job_id) job_description = build_job_description(job) generated_sequences = await generate_sequences_with_llm(job_description) save_request = SaveSequencesRequest(sequences=generated_sequences) sequences_result = await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=save_request.dict()) return { "ok": True, "message": "Leads added and informed sequences generated successfully", "leads_result": leads_result, "sequences_result": sequences_result, "generated_sequences": [seq for seq in generated_sequences] } @app.post("/campaigns/{campaign_id}/leads/{lead_id}/resume", response_model=Dict[str, Any], tags=["Leads"]) async def resume_lead_by_campaign_id(campaign_id: int, lead_id: int, request: ResumeLeadRequest): """Resume Lead By Campaign ID""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/resume", data=request.dict()) @app.post("/campaigns/{campaign_id}/leads/{lead_id}/pause", response_model=Dict[str, Any], tags=["Leads"]) async def pause_lead_by_campaign_id(campaign_id: int, lead_id: int): """Pause Lead By Campaign ID""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/pause") @app.delete("/campaigns/{campaign_id}/leads/{lead_id}", response_model=Dict[str, Any], tags=["Leads"]) async def delete_lead_by_campaign_id(campaign_id: int, lead_id: int): """Delete Lead By Campaign ID""" return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/leads/{lead_id}") @app.post("/campaigns/{campaign_id}/leads/{lead_id}/unsubscribe", response_model=Dict[str, Any], tags=["Leads"]) async def unsubscribe_lead_from_campaign(campaign_id: int, lead_id: int): """Unsubscribe/Pause Lead From Campaign""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/unsubscribe") @app.post("/leads/{lead_id}/unsubscribe", response_model=Dict[str, Any], tags=["Leads"]) async def unsubscribe_lead_from_all_campaigns(lead_id: int): """Unsubscribe Lead From All Campaigns""" return await call_smartlead_api("POST", f"leads/{lead_id}/unsubscribe") @app.post("/leads/{lead_id}", response_model=Dict[str, Any], tags=["Leads"]) async def update_lead(lead_id: int, lead_data: Dict[str, Any]): """Update lead using the Lead ID""" return await call_smartlead_api("POST", f"leads/{lead_id}", data=lead_data) @app.post("/campaigns/{campaign_id}/leads/{lead_id}/category", response_model=Dict[str, Any], tags=["Leads"]) async def update_lead_category_by_campaign(campaign_id: int, lead_id: int, request: LeadCategoryUpdateRequest): """Update a lead's category based on their campaign""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/leads/{lead_id}/category", data=request.dict()) @app.post("/leads/add-domain-block-list", response_model=Dict[str, Any], tags=["Leads"]) async def add_domain_to_global_block_list(request: DomainBlockListRequest): """Add Lead/Domain to Global Block List""" return await call_smartlead_api("POST", "leads/add-domain-block-list", data=request.dict()) @app.get("/leads/fetch-categories", response_model=List[Dict[str, Any]], tags=["Leads"]) async def fetch_lead_categories(): """Fetch lead categories""" return await call_smartlead_api("GET", "leads/fetch-categories") @app.get("/leads", response_model=Dict[str, Any], tags=["Leads"]) async def fetch_lead_by_email_address(email: str): """Fetch lead by email address""" return await call_smartlead_api("GET", "leads", params={"email": email}) @app.get("/leads/{lead_id}/campaigns", response_model=List[Dict[str, Any]], tags=["Leads"]) async def campaigns_for_lead(lead_id: int): """Fetch all campaigns that a lead belongs to""" return await call_smartlead_api("GET", f"leads/{lead_id}/campaigns") @app.get("/campaigns/{campaign_id}/leads/check", response_model=Dict[str, Any], tags=["Leads"]) async def check_lead_in_campaign(campaign_id: int, email: str): """Check if a lead exists in a campaign using efficient indexed lookups""" try: lead_response = await call_smartlead_api("GET", "leads", params={"email": email}) if not lead_response or "id" not in lead_response: return {"exists": False, "message": "Lead not found"} lead_id = lead_response["id"] campaigns_response = await call_smartlead_api("GET", f"leads/{lead_id}/campaigns") if not campaigns_response: return {"exists": False, "message": "No campaigns found for lead"} campaign_exists = any(campaign.get("id") == campaign_id for campaign in campaigns_response) return {"exists": campaign_exists, "message": "Lead found in campaign" if campaign_exists else "Lead not found in campaign"} except HTTPException as e: if e.status_code == 404: return {"exists": False, "message": "Lead not found"} raise e except Exception as e: raise HTTPException(status_code=500, detail=f"Error checking lead in campaign: {str(e)}") @app.get("/campaigns/{campaign_id}/leads-export", tags=["Leads"]) async def export_data_from_campaign(campaign_id: int): """Export data from a campaign as CSV""" if SMARTLEAD_API_KEY == "your-api-key-here": raise HTTPException(status_code=400, detail="Smartlead API key not configured") url = _get_smartlead_url(f"campaigns/{campaign_id}/leads-export") params = {"api_key": SMARTLEAD_API_KEY} try: async with httpx.AsyncClient(timeout=30.0) as client: resp = await client.get(url, params=params) if resp.status_code >= 400: try: error_data = resp.json() error_message = error_data.get('message', error_data.get('error', 'Unknown error')) raise HTTPException(status_code=resp.status_code, detail=error_message) except (ValueError, KeyError): raise HTTPException(status_code=resp.status_code, detail=resp.text) return Response( content=resp.text, media_type="text/csv", headers={"Content-Disposition": f"attachment; filename=campaign_{campaign_id}_leads.csv"} ) except httpx.TimeoutException: raise HTTPException(status_code=408, detail="Request to Smartlead API timed out") except httpx.RequestError as e: raise HTTPException(status_code=503, detail=f"Failed to connect to Smartlead API: {str(e)}") # ============================================================================ # SEQUENCE ENDPOINTS # ============================================================================ @app.get("/campaigns/{campaign_id}/sequences", response_model=Any, tags=["Sequences"]) async def get_campaign_sequences(campaign_id: int): """Fetch email sequences for a campaign""" return await call_smartlead_api("GET", f"campaigns/{campaign_id}/sequences") @app.post("/campaigns/{campaign_id}/sequences", response_model=Dict[str, Any], tags=["Sequences"]) async def save_campaign_sequences(campaign_id: int, request: SaveSequencesRequest): """Save Campaign Sequence""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=request.dict()) # *** MODIFIED: generate_campaign_sequences now uses the corrected AI function *** @app.post("/campaigns/{campaign_id}/sequences/generate", response_model=Dict[str, Any], tags=["Sequences"]) async def generate_campaign_sequences(campaign_id: int, request: GenerateSequencesRequest): """Generate a campaign sequence template using AI that leverages personalized custom fields.""" job_id = request.job_id # Fetch job details from database job = fetch_job_by_id(job_id) # Build comprehensive job description from job details job_description = build_job_description(job) # Generate the smart template generated_sequences = await generate_sequences_with_llm(job_description, campaign_id) # Save the template to the campaign save_request = SaveSequencesRequest(sequences=generated_sequences) result = await call_smartlead_api("POST", f"campaigns/{campaign_id}/sequences", data=save_request.dict()) return { "ok": True, "message": "Sequence template generated and saved successfully. It will use personalized fields for each lead.", "generated_sequences": [seq for seq in generated_sequences], "save_result": result } # ============================================================================ # WEBHOOK ENDPOINTS # ============================================================================ @app.get("/campaigns/{campaign_id}/webhooks", response_model=List[Dict[str, Any]], tags=["Webhooks"]) async def fetch_webhooks_by_campaign_id(campaign_id: int): """Fetch Webhooks By Campaign ID""" return await call_smartlead_api("GET", f"campaigns/{campaign_id}/webhooks") @app.post("/campaigns/{campaign_id}/webhooks", response_model=Dict[str, Any], tags=["Webhooks"]) async def add_update_campaign_webhook(campaign_id: int, request: WebhookRequest): """Add / Update Campaign Webhook""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/webhooks", data=request.dict()) @app.delete("/campaigns/{campaign_id}/webhooks", response_model=Dict[str, Any], tags=["Webhooks"]) async def delete_campaign_webhook(campaign_id: int, request: WebhookDeleteRequest): """Delete Campaign Webhook""" return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/webhooks", data=request.dict()) # ============================================================================ # CLIENT MANAGEMENT ENDPOINTS # ============================================================================ @app.post("/client/save", response_model=Dict[str, Any], tags=["Clients"]) async def add_client_to_system(request: ClientRequest): """Add Client To System (Whitelabel or not)""" return await call_smartlead_api("POST", "client/save", data=request.dict()) @app.get("/client", response_model=List[Dict[str, Any]], tags=["Clients"]) async def fetch_all_clients(): """Fetch all clients""" return await call_smartlead_api("GET", "client") # ============================================================================ # MESSAGE HISTORY AND REPLY ENDPOINTS # ============================================================================ @app.get("/campaigns/{campaign_id}/leads/{lead_id}/message-history", response_model=Dict[str, Any], tags=["Messages"]) async def fetch_lead_message_history_based_on_campaign(campaign_id: int, lead_id: int): """Fetch Lead Message History Based On Campaign""" return await call_smartlead_api("GET", f"campaigns/{campaign_id}/leads/{lead_id}/message-history") @app.post("/campaigns/{campaign_id}/reply-email-thread", response_model=Dict[str, Any], tags=["Messages"]) async def reply_to_lead_from_master_inbox(campaign_id: int, request: MessageHistoryRequest): """Reply To Lead From Master Inbox via API""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/reply-email-thread", data=request.dict()) # ============================================================================ # EMAIL ACCOUNT ENDPOINTS # ============================================================================ @app.get("/email-accounts", response_model=List[EmailAccount], tags=["Email Accounts"]) async def list_email_accounts(offset: int = 0, limit: int = 100): """List all email accounts with optional pagination""" params = {"offset": offset, "limit": limit} return await call_smartlead_api("GET", "email-accounts", params=params) @app.post("/email-accounts/save", response_model=Any, tags=["Email Accounts"]) async def save_email_account(account: Dict[str, Any]): """Create an Email Account""" return await call_smartlead_api("POST", "email-accounts/save", data=account) @app.get("/email-accounts/{account_id}", response_model=EmailAccount, tags=["Email Accounts"]) async def get_email_account(account_id: int): """Fetch Email Account By ID""" return await call_smartlead_api("GET", f"email-accounts/{account_id}") @app.post("/email-accounts/{account_id}", response_model=Any, tags=["Email Accounts"]) async def update_email_account(account_id: int, payload: Dict[str, Any]): """Update Email Account""" return await call_smartlead_api("POST", f"email-accounts/{account_id}", data=payload) @app.post("/email-accounts/{account_id}/warmup", response_model=Any, tags=["Email Accounts"]) async def set_warmup(account_id: int, payload: Dict[str, Any]): """Add/Update Warmup To Email Account""" return await call_smartlead_api("POST", f"email-accounts/{account_id}/warmup", data=payload) @app.get("/email-accounts/{account_id}/warmup-stats", response_model=Any, tags=["Email Accounts"]) async def get_warmup_stats(account_id: int): """Fetch Warmup Stats By Email Account ID""" return await call_smartlead_api("GET", f"email-accounts/{account_id}/warmup-stats") @app.get("/campaigns/{campaign_id}/email-accounts", response_model=List[EmailAccount], tags=["Email Accounts"]) async def list_campaign_email_accounts(campaign_id: int): """List all email accounts per campaign""" return await call_smartlead_api("GET", f"campaigns/{campaign_id}/email-accounts") @app.post("/campaigns/{campaign_id}/email-accounts", response_model=Any, tags=["Email Accounts"]) async def add_campaign_email_accounts(campaign_id: int, payload: Dict[str, Any]): """Add Email Account To A Campaign""" return await call_smartlead_api("POST", f"campaigns/{campaign_id}/email-accounts", data=payload) @app.delete("/campaigns/{campaign_id}/email-accounts", response_model=Any, tags=["Email Accounts"]) async def remove_campaign_email_accounts(campaign_id: int, payload: Dict[str, Any]): """Remove Email Account From A Campaign""" return await call_smartlead_api("DELETE", f"campaigns/{campaign_id}/email-accounts", data=payload) @app.post("/email-accounts/reconnect-failed-email-accounts", response_model=Dict[str, Any], tags=["Email Accounts"]) async def reconnect_failed_email_accounts(): """Reconnect failed email accounts""" return await call_smartlead_api("POST", "email-accounts/reconnect-failed-email-accounts") # ============================================================================ # UTILITY ENDPOINTS # ============================================================================ @app.get("/health", response_model=Dict[str, Any], tags=["Utilities"]) async def health_check(): """Health check endpoint to verify API connectivity""" try: campaigns = await call_smartlead_api("GET", "campaigns") return { "status": "healthy", "message": "Smartlead API is accessible", "campaigns_count": len(campaigns) if isinstance(campaigns, list) else 0, "timestamp": datetime.now().isoformat() } except Exception as e: return { "status": "unhealthy", "message": f"Smartlead API connection failed: {str(e)}", "timestamp": datetime.now().isoformat() } @app.get("/api-info", response_model=Dict[str, Any], tags=["Utilities"]) async def api_info(): """Get information about the API and available endpoints""" return { "name": "Smartlead API - Complete Integration", "version": "2.0.0", "description": "Comprehensive FastAPI wrapper for Smartlead email automation platform", "base_url": SMARTLEAD_BASE_URL, "available_endpoints": [ "Campaign Management", "Lead Management", "Sequence Management", "Webhook Management", "Client Management", "Message History & Reply", "Analytics", "Email Account Management" ], "documentation": "Based on Smartlead API documentation", "timestamp": datetime.now().isoformat() } @app.get("/jobs", response_model=List[Dict[str, Any]], tags=["Jobs"]) async def get_available_jobs(limit: int = Query(10, ge=1, le=100, description="Number of jobs to return")): """List available jobs from the database""" return list_available_jobs(limit) @app.get("/jobs/{job_id}", response_model=Job, tags=["Jobs"]) async def get_job_by_id(job_id: str): """Get job details by ID""" return fetch_job_by_id(job_id) # ============================================================================ # AI SEQUENCE GENERATION FUNCTIONS # ============================================================================ async def generate_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[str, str]: class structure(BaseModel): welcome_message: str = Field(description="Welcome message for the candidate") closing_message: str = Field(description="Closing message for the candidate") """Generate personalized welcome and closing messages using LLM based on candidate details""" if not LANGCHAIN_AVAILABLE: return generate_template_welcome_closing_messages(lead_data) try: openai_api_key = os.getenv("OPENAI_API_KEY") if not openai_api_key: print("Warning: OPENAI_API_KEY not set. Using template messages.") return generate_template_welcome_closing_messages(lead_data) llm = ChatOpenAI( model="gpt-4o", temperature=0.7, openai_api_key=openai_api_key ) str_llm = llm.with_structured_output(structure) first_name = lead_data.get("first_name", "") company_name = lead_data.get("company_name", "") title = lead_data.get("custom_fields", {}).get("Title", "") candidate_info = f"Name: {first_name}, Company: {company_name}, Title: {title}" system_prompt = """You are an expert recruiter creating personalized messages. Generate a 2-sentence welcome message and a 1-sentence closing message. Be professional and friendly and sound like real human recruitor. Reference their background. Respond with ONLY valid JSON. Start with Hi then first name and then from next line write the message. The welcome message should be mention the candidate current role and its amazing work there.""" prompt_template = ChatPromptTemplate.from_messages([ ("system", system_prompt), ("human", "Generate messages for this candidate: {candidate_info}") ]) messages = prompt_template.format_messages(candidate_info=candidate_info) response = await str_llm.ainvoke(messages) return { "welcome_message": response.welcome_message, "closing_message": response.closing_message } except Exception as e: print(f"Error generating welcome/closing messages with LLM: {str(e)}") return generate_template_welcome_closing_messages(lead_data) def generate_template_welcome_closing_messages(lead_data: Dict[str, Any]) -> Dict[str, str]: """Generate template-based welcome and closing messages as fallback""" first_name = lead_data.get("first_name", "there") welcome_message = f"Hi {first_name}, I came across your profile and was impressed by your background." closing_message = f"Looking forward to connecting with you, {first_name}!" return {"welcome_message": welcome_message, "closing_message": closing_message} # *** MODIFIED: generate_sequences_with_llm now creates a smart template *** async def generate_sequences_with_llm(job_description: str, campaign_id: Optional[int] = None) -> List[CampaignSequence]: """Generate an email sequence template using LangChain and OpenAI, optionally informed by campaign lead data.""" class EmailContent(BaseModel): subject: str = Field(description="Subject line for the email") body: str = Field(description="Body of the email, using placeholders") class SequenceStructure(BaseModel): introduction: EmailContent follow_up_1: EmailContent follow_up_2: EmailContent if not LANGCHAIN_AVAILABLE: return generate_template_sequences(job_description) try: openai_api_key = os.getenv("OPENAI_API_KEY") if not openai_api_key: print("Warning: OPENAI_API_KEY not set. Using template sequences.") return generate_template_sequences(job_description) # If campaign_id is provided, fetch lead data to inform the template lead_context = "" if campaign_id: try: leads_response = await call_smartlead_api("GET", f"campaigns/{campaign_id}/leads", params={"limit": 10}) campaign_leads = leads_response.get("leads", []) if isinstance(leads_response, dict) else leads_response if campaign_leads: # Sample lead data to inform template generation sample_leads = campaign_leads[:3] lead_info = [] for lead in sample_leads: custom_fields = lead.get("custom_fields", {}) lead_info.append({ "first_name": lead.get("first_name", ""), "company": lead.get("company_name", ""), "title": custom_fields.get("Title", ""), "welcome_msg": custom_fields.get("Welcome_Message", ""), "closing_msg": custom_fields.get("Closing_Message", "") }) lead_context = f"\n\nCampaign Lead Context (sample of {len(campaign_leads)} leads):\n{json.dumps(lead_info, indent=2)}" except Exception as e: print(f"Could not fetch lead data for campaign {campaign_id}: {e}") llm = ChatOpenAI(model="gpt-4o", temperature=0.7, openai_api_key=openai_api_key) structured_llm = llm.with_structured_output(SequenceStructure) system_prompt = """You are an expert email sequence template generator for recruitment campaigns on behalf of 'Ali Taghikhani, CEO SRN'. Your task is to generate a 3-step email sequence template for a given job description. Email Sequence Structure: 1. INTRODUCTION (Day 1): Ask for consent and interest in the role, In the starting use the welcome message placeholder after the salutation, and in the end use closing message template along with the name and title of sender 2. OUTREACH (Day 3): Provide detailed job information 3. FOLLOW-UP (Day 5): Follow up on updates and next steps Requirements: - First sequence will only ask about the consent and interest in the role, mentioning the company name, role title, and location type (onsite/hybrid/remote) - Second and third sequences are follow-ups (no subject line needed) providing more details about the role, company mission, and salary information (if available) - All emails should be HTML formatted with proper
tags - Professional but friendly tone - Each email must include a clear call-to-action: "If you're interested, send me your updated CV, salary expectations, and I'll get your application in front of the hiring manager ASAP." - Focus on building consent and trust **CRITICAL FORMATTING RULES:** 1. **PLACEHOLDER FORMAT IS ESSENTIAL:** You MUST use double curly braces for all placeholders. - **CORRECT:** `{{first_name}}` - **INCORRECT:** `{first_name}` or `[first_name]` or `` 2. **REQUIRED PLACEHOLDERS:** You MUST include `{{Welcome_Message}}` and `{{Closing_Message}}` in the first email. You should also use `{{first_name}}` in follow-ups. 3. **FIRST EMAIL STRUCTURE:** The first email's body MUST begin with `{{Welcome_Message}}` and end with `{{Closing_Message}}`. 4. **SIGNATURE:** End EVERY email body with `

Best regards`. DO NOT WRITE NAME AND TITLE AS IT WILL BE ALREADY ADDED BY DEFAULT 5. **CONTENT REQUIREMENTS:** Each email must mention: - Company name - Role title - Work location (onsite/hybrid/remote) - Company mission (in emails 2 and 3) - Salary information or Compensation (if available, in all the emails) - His/Her responsibilities in the company 6. **CALL-TO-ACTION:** Each email must end with the CTA (before the signature): "If you're interested, send me your updated CV, salary expectations, and I'll get your application in front of the hiring manager ASAP." 7. **EXAMPLE BODY:** ```html {{Welcome_Message}}

I saw your profile and was impressed. We have an opening for a Senior Engineer at [Company Name] that seems like a great fit. This is a [remote/hybrid/onsite] position.

If you're interested, send me your updated CV, salary expectations, and I'll get your application in front of the hiring manager ASAP.

{{Closing_Message}}

Best regards,
Ali Taghikhani
CEO, SRN ``` Always try to start the message with the salutation except for the first email. If lead context is provided, use it to make the templates more relevant. Respond with ONLY a valid JSON object matching the required structure. """ prompt = ChatPromptTemplate.from_messages([ ("system", system_prompt), ("human", "Generate the 3-step email sequence template for this job description: {job_description}{lead_context}") ]) # By using .partial, we tell LangChain to treat the Smartlead placeholders as literals # and not expect them as input variables. This is the correct way to handle this. partial_prompt = prompt.partial( first_name="", company_name="", **{"Welcome_Message": "", "Closing_Message": "", "Title": ""} ) chain = partial_prompt | structured_llm response = await chain.ainvoke({"job_description": job_description, "lead_context": lead_context}) # Post-process the AI's response to enforce double curly braces. # This is a robust way to fix the AI's tendency to use single braces. def fix_braces(text: str) -> str: if not text: return "" # This regex finds all occurrences of `{...}` that are not `{{...}}` # and replaces them with `{{...}}`. return re.sub(r'{([^{}\n]+)}', r'{{\1}}', text) sequences = [ CampaignSequence( seq_number=1, seq_delay_details=SeqDelayDetails(delay_in_days=1), seq_variants=[SeqVariant( subject=fix_braces(response.introduction.subject), email_body=fix_braces(response.introduction.body), variant_label="A" )] ), CampaignSequence( seq_number=2, seq_delay_details=SeqDelayDetails(delay_in_days=3), subject="", # Same thread email_body=fix_braces(response.follow_up_1.body) ), CampaignSequence( seq_number=3, seq_delay_details=SeqDelayDetails(delay_in_days=5), subject="", # Same thread email_body=fix_braces(response.follow_up_2.body) ) ] return sequences except Exception as e: print(f"Error generating sequences with LLM: {str(e)}. Falling back to template.") return generate_template_sequences(job_description) def generate_template_sequences(job_description: str) -> List[CampaignSequence]: """Generate template-based sequences as fallback, using correct placeholders.""" # This is the corrected structure for the first email first_email_body = f"""

{{{{custom.Welcome_Message}}}}

I'm reaching out because we have some exciting opportunities for a {job_description} that might be a great fit for your background. Are you currently open to exploring new roles?

{{{{custom.Closing_Message}}}}

Best regards,
Ali Taghikhani
CEO, SRN

""" follow_up_1_body = f"""

Hi {{{{first_name}}}},

Just wanted to follow up on my previous email regarding the {job_description} role. I'd love to hear your thoughts when you have a moment.

Best regards,
Ali Taghikhani
CEO, SRN

""" follow_up_2_body = f"""

Hi {{{{first_name}}}},

Checking in one last time about the {job_description} opportunity. If the timing isn't right, no worries at all. Otherwise, I look forward to hearing from you.

Best regards,
Ali Taghikhani
CEO, SRN

""" sequences = [ CampaignSequence( seq_number=1, seq_delay_details=SeqDelayDetails(delay_in_days=1), seq_variants=[ SeqVariant( subject=f"Regarding a {job_description} opportunity", email_body=first_email_body, variant_label="A" ) ] ), CampaignSequence( seq_number=2, seq_delay_details=SeqDelayDetails(delay_in_days=3), subject="", email_body=follow_up_1_body ), CampaignSequence( seq_number=3, seq_delay_details=SeqDelayDetails(delay_in_days=5), subject="", email_body=follow_up_2_body ) ] return sequences # ============================================================================ # RATE LIMITING MIDDLEWARE # ============================================================================ class RateLimiter: def __init__(self, max_requests: int = 10, window_seconds: int = 2): self.max_requests = max_requests self.window_seconds = window_seconds self.requests = [] def is_allowed(self) -> bool: now = time.time() self.requests = [req_time for req_time in self.requests if now - req_time < self.window_seconds] if len(self.requests) >= self.max_requests: return False self.requests.append(now) return True rate_limiter = RateLimiter(max_requests=10, window_seconds=2) @app.middleware("http") async def rate_limit_middleware(request: Request, call_next): """Rate limiting middleware to respect Smartlead's API limits""" if not rate_limiter.is_allowed(): return JSONResponse( status_code=429, content={"error": "Rate limit exceeded"} ) response = await call_next(request) return response # ============================================================================ # ERROR HANDLING # ============================================================================ @app.exception_handler(HTTPException) async def http_exception_handler(request: Request, exc: HTTPException): """Custom HTTP exception handler""" return JSONResponse( status_code=exc.status_code, content={"error": True, "message": exc.detail} ) @app.exception_handler(Exception) async def general_exception_handler(request: Request, exc: Exception): """General exception handler""" return JSONResponse( status_code=500, content={ "error": True, "message": "Internal server error", "detail": str(exc) if os.getenv("DEBUG") else None } ) # ============================================================================ # CUSTOM OPENAPI SCHEMA # ============================================================================ def custom_openapi(): if app.openapi_schema: return app.openapi_schema openapi_schema = get_openapi( title="Smartlead API - Complete Integration", version="2.1.0", description="A comprehensive FastAPI wrapper for the Smartlead email automation platform.", routes=app.routes, ) openapi_schema["tags"] = [ {"name": "Campaigns", "description": "Campaign management operations"}, {"name": "Leads", "description": "Lead management operations"}, {"name": "Sequences", "description": "Email sequence management"}, {"name": "Webhooks", "description": "Webhook management"}, {"name": "Clients", "description": "Client account management"}, {"name": "Messages", "description": "Message history and reply operations"}, {"name": "Analytics", "description": "Campaign analytics and statistics"}, {"name": "Email Accounts", "description": "Email account management"}, {"name": "Utilities", "description": "Utility endpoints"} ] app.openapi_schema = openapi_schema return app.openapi_schema app.openapi = custom_openapi # ============================================================================ # MAIN APPLICATION ENTRY POINT # ============================================================================ if __name__ == "__main__": import uvicorn print("Starting Smartlead API - Complete Integration") uvicorn.run( "__main__:app", host="0.0.0.0", port=8000, reload=True, log_level="info" )