Telecaller & Leads
Lead Ingestion
Facebook Lead Processor

Facebook Lead Processor

The FaceBookLeadProcessor.cs module handles lead ingestion from Facebook Lead Ads. This processor receives webhook notifications from Facebook, extracts lead data, performs deduplication checks, and routes leads either to AI Auto-Caller or to human telecallers based on system configuration.

Core Business Logic

Webhook Reception

Facebook sends lead data via HTTP POST webhook to a configured endpoint:

POST /api/webhooks/facebook-leads
Content-Type: application/json

The webhook payload contains:

  • Lead ID: Facebook lead identifier
  • Lead Gen Form ID: Which ad campaign generated the lead
  • Lead Data: Name, phone number, email, and additional fields
  • Timestamp: When the lead was generated

Lead Data Parsing

The processor extracts and normalizes lead information:

FieldSourceUsage
Full Namefirst_name + last_nameLead identification
Mobile Numberphone_number fieldDeduplication & assignment
Emailemail fieldContact method & CRM sync
Campaign IDlead_gen_form_idMaps to internal CallSource
Lead Gen Timecreated_timeTimestamp for analytics

AI Routing Decision

The processor evaluates a routing configuration to determine lead destination:

  1. Configuration Check: System setting EnableFacebookAIAutoCall or per-campaign setting
  2. AI Route: If enabled, lead status set to Pending_AI_Call
    • AI Auto-Caller (separate service) picks up and attempts call
    • If AI succeeds, call outcome recorded
    • If AI fails, lead falls back to staff assignment
  3. Direct Staff Route: If disabled, normal assignment flow (same as external leads)

Duplicate Detection

Same 4-day mobile deduplication as external leads:

  • Query past 4 days for same mobile number
  • If found, assign to same staff
  • If not found, assign via round-robin or manager routing

CallSource Mapping

Facebook campaigns are mapped to internal CallSource records:

Facebook Lead Gen Form ID → CallSourceId

Stores in: lead.CallSourceId

Used for: Analytics, attribution, staff allocation rules

Webhook Processing Flow

Webhook Processing Sequence Diagram

Configuration Options

Routing Configuration

EnableFacebookAIAutoCall = true/false (global)
  OR
PerCampaignRouting:
  - Campaign_ID_1: AI_ROUTE
  - Campaign_ID_2: STAFF_ROUTE
  - Campaign_ID_3: MANAGER_ASSIGNMENT

Database Schema

TableFieldPurpose
FacebookLeadsLeadIdFacebook lead identifier
FacebookLeadsStaffIdAssigned staff member
FacebookLeadsMobilePhone number
FacebookLeadsEmailContact email
FacebookLeadsCallSourceIdCampaign reference
FacebookLeadsStatusAssigned, Pending_AI_Call, AI_Called, Completed
FacebookLeadsCreatedDateLead received timestamp
FacebookLeadsLastModifiedLast status update

Error Handling

Webhook Failures

ScenarioAction
Invalid signatureReject (401), log security alert
Malformed JSONReject (400), log parsing error
Database insert failsReturn 202 Accepted, retry with backoff
All staff unavailableAssign to queue, notify manager

Fallback Behavior

  • AI Route Failure: Automatically fall back to available staff assignment
  • Staff Unavailable: Escalate to manager or hold in queue
  • Mobile Invalid: Log exception, require manual review

Performance Considerations

  • Webhook Timeout: Should complete within 5 seconds to avoid Facebook timeout
  • Asynchronous Processing: Move heavy operations (AI assignment) to background queue
  • Caching: Cache CallSource mappings and staff availability
  • Batching: If high volume, use message queue to batch leads

Integration Points

  • Facebook Lead Ads API: Webhook endpoint configuration
  • CallSource Mapping: Maps Facebook Form ID to internal CallSource
  • Deduplication Service: Shared with external leads processor
  • AI Auto-Caller Service: Consumes Pending_AI_Call leads
  • Staff Queue System: Assigns leads to human telecallers
  • Analytics Dashboard: Tracks Facebook lead metrics

Related Components

  • ExternalLeadBO: Handles non-Facebook external leads with same dedup logic
  • CallSourceBO: Maps campaigns to internal call sources
  • AI Auto-Caller Service: Processes AI-routed leads
  • Staff Service: Manages telecaller allocation