{
  "name": "LeadFlow AI \u2014 02 WhatsApp Inbound",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "leadflow/whatsapp",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "cce0ea2a-66d3-4424-9733-7ae02ba70201",
      "name": "WhatsApp Messages Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const envelope = $json.body ?? $json;\nconst value = envelope.entry?.[0]?.changes?.[0]?.value ?? {};\nconst message = value.messages?.[0];\nif (!message) {\n  // Meta also sends delivery/read status events. Stop processing them; the webhook already returned 200.\n  return [];\n}\nconst contact = value.contacts?.[0] ?? {};\nconst body = message.text?.body ?? message.button?.text ?? message.interactive?.button_reply?.title ?? message.interactive?.list_reply?.title ?? '';\nreturn [{json: {lead: {\n  source: 'whatsapp',\n  source_lead_id: message.id ?? '',\n  full_name: contact.profile?.name ?? '',\n  phone: message.from ?? contact.wa_id ?? '',\n  message: body,\n  raw_payload: envelope,\n}}}];"
      },
      "id": "abb727ee-8cdd-493a-b679-c8f7ea7e8436",
      "name": "Extract WhatsApp Message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://leadflow-api:8000/api/leads/upsert",
        "options": {
          "timeout": 30000
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-LeadFlow-Key",
              "value": "={{ $env.LEADFLOW_INTERNAL_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { lead: $json.lead } }}"
      },
      "id": "060ea7fa-5529-43af-830c-0bcb7597fc62",
      "name": "Deduplicate and Store",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        500,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=http://leadflow-api:8000/api/leads/{{$json.lead_id}}/qualify",
        "options": {
          "timeout": 60000
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-LeadFlow-Key",
              "value": "={{ $env.LEADFLOW_INTERNAL_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {} }}"
      },
      "id": "5e3d407f-00f8-483d-9919-7d447565d281",
      "name": "AI Qualification",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        760,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://leadflow-api:8000/api/crm/sync",
        "options": {
          "timeout": 30000
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-LeadFlow-Key",
              "value": "={{ $env.LEADFLOW_INTERNAL_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { lead_id: $json.lead_id } }}"
      },
      "id": "db94bc7c-b0b0-4eb5-8d61-e053e2161b5d",
      "name": "Sync CRM",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1020,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://leadflow-api:8000/api/messages/send-initial",
        "options": {
          "timeout": 30000
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-LeadFlow-Key",
              "value": "={{ $env.LEADFLOW_INTERNAL_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { lead_id: $json.lead_id } }}"
      },
      "id": "3a487988-f45d-4e5a-9e51-c2b01eafe175",
      "name": "Reply on WhatsApp",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1280,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://leadflow-api:8000/api/followups/schedule",
        "options": {
          "timeout": 30000
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-LeadFlow-Key",
              "value": "={{ $env.LEADFLOW_INTERNAL_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { lead_id: $json.lead_id } }}"
      },
      "id": "daaa3647-6a38-457c-91fd-baf5284190a2",
      "name": "Schedule Follow-ups",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1540,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://leadflow-api:8000/api/notifications/send-sales",
        "options": {
          "timeout": 30000
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-LeadFlow-Key",
              "value": "={{ $env.LEADFLOW_INTERNAL_API_KEY }}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ { lead_id: $json.lead_id } }}"
      },
      "id": "505fbb08-15d3-4d2e-ab68-6ae82e77b6c2",
      "name": "Notify Sales Team",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1800,
        0
      ]
    }
  ],
  "connections": {
    "WhatsApp Messages Webhook": {
      "main": [
        [
          {
            "node": "Extract WhatsApp Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract WhatsApp Message": {
      "main": [
        [
          {
            "node": "Deduplicate and Store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Deduplicate and Store": {
      "main": [
        [
          {
            "node": "AI Qualification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Qualification": {
      "main": [
        [
          {
            "node": "Sync CRM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sync CRM": {
      "main": [
        [
          {
            "node": "Reply on WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reply on WhatsApp": {
      "main": [
        [
          {
            "node": "Schedule Follow-ups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Follow-ups": {
      "main": [
        [
          {
            "node": "Notify Sales Team",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "56a74d66-aa1b-4751-bc53-29b7d13fdb7f",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": [],
  "id": "bd7381b6-3adb-4cd6-8779-53bb0eb17d2d"
}