Skip to main content
Normalized for Mintlify from knowledge-base/aiconnected-apps-and-modules/modules/funnelChat/legacy-kb-chat-lead-capture-routing.mdx.

Lead Capture & Routing System

Overview

This specification extends the kbChat platform with lead capture and routing capabilities. The system allows agencies and businesses to create custom forms, capture lead information during chat conversations, and route that information to their preferred destination. Platform Responsibility: Capture the data, deliver the data. Agency/Business Responsibility: What happens after delivery.

Admin Interface: Lead Capture Settings

Location: Business Settings → Lead Capture (or Agency Settings → Default Lead Capture for agency-wide defaults)

Part 1: Form Builder

1.1 Form Configuration

SettingDescription
Form NameInternal reference name (e.g., “Main Contact Form”, “Service Request Form”)
Form EnabledToggle on/off
Multiple FormsBusiness can create multiple forms for different purposes

1.2 Field Builder

Each form consists of custom fields. The admin can add, remove, reorder, and configure fields. Field Properties:
PropertyDescription
Field LabelDisplay name shown to user (e.g., “Your Name”, “Email Address”)
Field KeySystem identifier for JSON payload (e.g., “name”, “email”, “phone”) - auto-generated from label but editable
Field TypeSee field types below
RequiredYes/No - conversation cannot complete without this field
PlaceholderHelper text shown in empty field
ValidationType-specific validation rules
Field Types:
TypeUse CaseValidation
TextName, company, general inputMin/max length
EmailEmail addressValid email format
PhonePhone numberValid phone format (configurable by country)
NumberQuantity, budget, ageMin/max value
DropdownService type, location, predefined optionsMust select from list
Multi-selectMultiple services interested inMust select at least one (if required)
DatePreferred appointment dateFuture dates only (optional)
TimePreferred timeTime range restrictions (optional)
Long TextMessage, project description, detailsMin/max length
HiddenUTM parameters, page URL, referrerAuto-populated, not shown to user

1.3 Preset Templates

To speed up setup, offer starter templates:
TemplateFields Included
Basic ContactName, Email, Phone
Service RequestName, Email, Phone, Service Type (dropdown), Message
Appointment RequestName, Email, Phone, Preferred Date, Preferred Time, Notes
Quote RequestName, Email, Phone, Service Type, Budget Range, Project Description
Agency can set a default template that applies to all new businesses.

1.4 Form Display Options

SettingOptions
Display ModeInline (fields appear in chat) / Modal (form pops up)
TriggerAI-initiated (AI decides when to show) / User-initiated (user clicks button) / Always (form appears at conversation start)
Submit Button TextCustomizable (default: “Submit”)
Success MessageMessage shown after form submission (default: “Thanks! Someone will be in touch shortly.”)

Part 2: AI Integration

2.1 How the AI Uses the Form

The AI needs to know:
  1. What form(s) exist
  2. When to present the form
  3. What to do after submission
System Prompt Addition: When generating the business system prompt, append form instructions:
## Lead Capture

When the user expresses interest in [services/booking/getting started/learning more], collect their information using the following form:

Required fields: [list required fields]
Optional fields: [list optional fields]

You may collect this information conversationally (asking one field at a time) or present the form directly, depending on conversation flow.

After collecting information, confirm receipt and inform the user: "[Success Message]"

2.2 Conversational vs. Form Collection

Option A: Conversational Collection AI asks for fields naturally within the conversation: > “I’d be happy to have someone reach out to you. What’s the best email to contact you at?” Option B: Direct Form AI triggers form display: > “Let me grab your details so we can follow up. Just fill out this quick form:” > [Form appears] Admin Setting: Collection Style
  • Conversational (AI asks naturally)
  • Form (AI presents form)
  • Hybrid (AI asks 1-2 key fields, then presents form for rest)

Part 3: Routing Configuration

After form submission, where does the data go?

3.1 Routing Destinations

Admin can enable one or more destinations. All enabled destinations fire simultaneously.

Email Notification

SettingDescription
Enable EmailToggle
Recipient(s)One or more email addresses (comma-separated)
| Subject Line | Customizable, supports variables: {form_name}, {business_name}, {field:name} |
| Email Format | HTML (formatted) / Plain Text | | Include Transcript | Yes/No - attach full conversation transcript | | Include AI Summary | Yes/No - include AI-generated lead summary |
**Default Subject:** "New Lead: {field:name} - {business_name}"

Email Body Contains:
  • All captured form fields (label: value format)
  • Timestamp
  • Conversation transcript (if enabled)
  • AI summary (if enabled): “User is interested in [X], asked about [Y], sentiment: [Z]“

SMS Notification

SettingDescription
Enable SMSToggle
Recipient(s)One or more phone numbers
Message TemplateCustomizable, supports variables, max 160 characters recommended
**Default Message:** "New lead from {business_name}: {field:name}, {field:phone}. Check email for details."

Webhook (JSON Payload)

SettingDescription
Enable WebhookToggle
Webhook URLDestination endpoint
HTTP MethodPOST (default) / PUT
Authentication TypeNone / API Key / Bearer Token / Basic Auth
API Key HeaderIf API Key auth: header name (default: “X-API-Key”)
API Key ValueIf API Key auth: the key value
Bearer TokenIf Bearer auth: the token
Basic Auth UsernameIf Basic auth
Basic Auth PasswordIf Basic auth
Custom HeadersOptional additional headers (key:value pairs)
Include TranscriptYes/No
Include AI SummaryYes/No

3.2 JSON Payload Structure

{
  "event": "lead_captured",
  "timestamp": "2025-01-14T15:30:00Z",
  "business": {
    "id": "uuid",
    "name": "Business Name"
  },
  "form": {
    "id": "uuid",
    "name": "Form Name"
  },
  "lead": {
    "name": "John Smith",
    "email": "john@example.com",
    "phone": "555-123-4567",
    "service_type": "Kitchen Remodel",
    "message": "Looking to update my kitchen, wondering about timeline and cost."
  },
  "meta": {
    "page_url": "https://example.com/services",
    "referrer": "https://google.com",
    "utm_source": "google",
    "utm_medium": "cpc",
    "utm_campaign": "spring_promo"
  },
  "conversation": {
    "id": "uuid",
    "transcript": "[Full transcript if enabled]",
    "summary": "User interested in kitchen remodel. Asked about timeline (wants completion by summer) and financing options. Ready to schedule consultation.",
    "message_count": 12,
    "duration_seconds": 180
  }
}
Field Mapping Note: The lead object keys match the Field Key set in the form builder. Agencies can customize these keys to match their destination system’s expected field names, eliminating the need for field mapping on the receiving end.

3.3 Webhook Testing

FeatureDescription
Test ButtonSends sample payload to configured URL
Response DisplayShows HTTP status code and response body
Payload PreviewShows exact JSON that will be sent
Recent DeliveriesLog of last 10 webhook attempts with status

3.4 Redirect Option

After form submission, optionally redirect the user:
SettingDescription
Enable RedirectToggle
Redirect URLWhere to send user (e.g., Calendly link, thank you page)
Redirect DelaySeconds to wait before redirect (default: 2)
Redirect MessageMessage shown during delay (default: “Taking you to schedule your appointment…”)
This allows: Capture lead info → Send to webhook → Redirect to Calendly The business gets the lead data AND the user can self-schedule. Best of both worlds.

Part 4: Database Schema Additions

4.1 New Tables

-- Lead capture forms
CREATE TABLE lead_forms (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  business_id UUID REFERENCES businesses(id) ON DELETE CASCADE,
  name VARCHAR(255) NOT NULL,
  is_active BOOLEAN DEFAULT true,
  display_mode VARCHAR(20) DEFAULT 'inline', -- 'inline' | 'modal'
  trigger_type VARCHAR(20) DEFAULT 'ai_initiated', -- 'ai_initiated' | 'user_initiated' | 'always'
  collection_style VARCHAR(20) DEFAULT 'conversational', -- 'conversational' | 'form' | 'hybrid'
  submit_button_text VARCHAR(100) DEFAULT 'Submit',
  success_message TEXT DEFAULT 'Thanks! Someone will be in touch shortly.',
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Form fields
CREATE TABLE lead_form_fields (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  form_id UUID REFERENCES lead_forms(id) ON DELETE CASCADE,
  field_label VARCHAR(255) NOT NULL,
  field_key VARCHAR(100) NOT NULL,
  field_type VARCHAR(50) NOT NULL,
  is_required BOOLEAN DEFAULT false,
  placeholder VARCHAR(255),
  validation_rules JSONB, -- type-specific validation
  options JSONB, -- for dropdown/multi-select
  display_order INTEGER NOT NULL,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Routing configuration
CREATE TABLE lead_routing (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  form_id UUID REFERENCES lead_forms(id) ON DELETE CASCADE,
  
  -- Email settings
  email_enabled BOOLEAN DEFAULT false,
  email_recipients TEXT[], -- array of email addresses
  email_subject VARCHAR(255),
  email_include_transcript BOOLEAN DEFAULT true,
  email_include_summary BOOLEAN DEFAULT true,
  
  -- SMS settings
  sms_enabled BOOLEAN DEFAULT false,
  sms_recipients TEXT[], -- array of phone numbers
  sms_template VARCHAR(320),
  
  -- Webhook settings
  webhook_enabled BOOLEAN DEFAULT false,
  webhook_url TEXT,
  webhook_method VARCHAR(10) DEFAULT 'POST',
  webhook_auth_type VARCHAR(20) DEFAULT 'none', -- 'none' | 'api_key' | 'bearer' | 'basic'
  webhook_auth_config JSONB, -- stores auth details (encrypted)
  webhook_custom_headers JSONB,
  webhook_include_transcript BOOLEAN DEFAULT true,
  webhook_include_summary BOOLEAN DEFAULT true,
  
  -- Redirect settings
  redirect_enabled BOOLEAN DEFAULT false,
  redirect_url TEXT,
  redirect_delay INTEGER DEFAULT 2,
  redirect_message TEXT,
  
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- Captured leads
CREATE TABLE leads (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  business_id UUID REFERENCES businesses(id) ON DELETE CASCADE,
  form_id UUID REFERENCES lead_forms(id) ON DELETE SET NULL,
  conversation_id UUID REFERENCES conversations(id) ON DELETE SET NULL,
  
  lead_data JSONB NOT NULL, -- captured form fields
  meta_data JSONB, -- page_url, referrer, utm params
  ai_summary TEXT,
  
  -- Delivery tracking
  email_sent BOOLEAN DEFAULT false,
  email_sent_at TIMESTAMPTZ,
  sms_sent BOOLEAN DEFAULT false,
  sms_sent_at TIMESTAMPTZ,
  webhook_sent BOOLEAN DEFAULT false,
  webhook_sent_at TIMESTAMPTZ,
  webhook_response_code INTEGER,
  
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Webhook delivery log
CREATE TABLE webhook_logs (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  lead_id UUID REFERENCES leads(id) ON DELETE CASCADE,
  webhook_url TEXT NOT NULL,
  request_payload JSONB,
  response_code INTEGER,
  response_body TEXT,
  error_message TEXT,
  attempted_at TIMESTAMPTZ DEFAULT NOW()
);

4.2 RLS Policies

-- Lead forms: business and parent agency can manage
ALTER TABLE lead_forms ENABLE ROW LEVEL SECURITY;

CREATE POLICY lead_forms_policy ON lead_forms
  USING (
    business_id IN (
      SELECT id FROM businesses WHERE id = business_id
      UNION
      SELECT b.id FROM businesses b
      JOIN agencies a ON b.agency_id = a.id
      WHERE a.id = current_user_agency_id()
    )
  );

-- Similar policies for lead_form_fields, lead_routing, leads, webhook_logs

Part 5: UI Mockup Descriptions

5.1 Form Builder Interface

Header:
  • Form Name (editable)
  • Active/Inactive toggle
  • Save / Delete buttons
Left Panel: Field List
  • Draggable list of current fields
  • Each field shows: Label, Type, Required indicator
  • Drag handle for reordering
  • Click to edit, X to delete
Right Panel: Field Editor
  • Appears when field is selected
  • All field properties editable
  • Live preview of field appearance
Bottom: Add Field
  • ”+ Add Field” button
  • Dropdown of field types
  • Or “Use Template” to load preset

5.2 Routing Configuration Interface

Tab Layout:
  • Email | SMS | Webhook | Redirect
Each Tab:
  • Enable toggle at top
  • Configuration fields below (grayed out if disabled)
  • Test button where applicable
Webhook Tab Extras:
  • Payload Preview panel (shows live JSON)
  • Test Result panel (shows last test response)
  • Delivery Log link

5.3 Leads Dashboard

Table View:
  • Date/Time
  • Name
  • Email
  • Phone
  • Form Used
  • Delivery Status (icons: ✓ email, ✓ SMS, ✓ webhook)
  • Actions: View Details
Detail View:
  • All captured fields
  • Full conversation transcript
  • AI summary
  • Delivery log (what was sent where, when, response codes)

Part 6: Implementation Priority

Phase 1: Minimum Viable

  1. Form builder with basic fields (text, email, phone)
  2. Email notification only
  3. Basic leads table

Phase 2: Full Routing

  1. All field types
  2. SMS notification
  3. Webhook with authentication options
  4. Redirect option

Phase 3: Polish

  1. Preset templates
  2. Webhook testing and logs
  3. Leads dashboard with filtering/search
  4. AI summary generation

Summary

What we built: A form builder and routing system that captures leads and delivers them wherever the business needs. What we didn’t build: Integrations with specific platforms. That’s the agency’s job. The value proposition: “Never miss a lead. Your AI assistant captures contact information and sends it to you instantly—by email, text, or directly into your existing systems via webhook.”
Last modified on April 18, 2026