{
  "name": "Lead to AI Call - SniperThink",
  "nodes": [
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "filter-valid-phone",
              "leftValue": "={{ $json['Phone Number 1'] }}",
              "rightValue": "NULL",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            },
            {
              "id": "filter-has-phone",
              "leftValue": "={{ $json['Phone Number 1'] }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
      "name": "Filter Valid Leads"
    },
    {
      "parameters": {
        "jsCode": "// -----------------------------------------------------------------------\n// PREPARE PAYLOAD FOR SNIPERTHINK AI CALLING API\n// -----------------------------------------------------------------------\n// Endpoint: POST /api/webhooks/n8n/lead-call\n// -----------------------------------------------------------------------\n\n// \u26a0\ufe0f IMPORTANT: Replace with your Bolna Agent ID (not database id)\n// Find this in: Dashboard \u2192 Agents \u2192 Select Agent \u2192 bolna_agent_id field\n// Example agents:\n// - Jeet kaur: 8b63ccd2-a002-4f6d-9950-93056ceb8c9c\n// - ST Outbound 3: 95f4d4c7-e6c8-4810-8a30-6949ae6019c8\n// - Sarthak new hinglish outbound: 1378a93d-8bce-4fbf-a7f8-b5f7ed0f4f16\nconst AGENT_ID = \"YOUR-BOLNA-AGENT-ID\";\n\nconst outputItems = [];\n\nfor (const item of items) {\n  const lead = item.json;\n  \n  // Skip if no valid phone number\n  if (!lead[\"Phone Number 1\"] || lead[\"Phone Number 1\"] === \"NULL\") {\n    continue;\n  }\n  \n  // Build the lead name (use Sender Name, fallback to Company Name)\n  let leadName = lead[\"Sender Name\"];\n  if (!leadName || leadName === \"NULL\") {\n    leadName = lead[\"Company Name\"];\n  }\n  if (!leadName || leadName === \"NULL\") {\n    leadName = \"Lead from \" + (lead[\"Source\"] || \"Email\");\n  }\n  \n  // Build comprehensive notes from all available info\n  const notesParts = [];\n  \n  if (lead[\"Product Name\"] && lead[\"Product Name\"] !== \"NULL\") {\n    notesParts.push(`Product: ${lead[\"Product Name\"]}`);\n  }\n  if (lead[\"Quantity\"] && lead[\"Quantity\"] !== \"NULL\") {\n    notesParts.push(`Qty: ${lead[\"Quantity\"]}`);\n  }\n  if (lead[\"Order Value\"] && lead[\"Order Value\"] !== \"NULL\") {\n    notesParts.push(`Value: ${lead[\"Order Value\"]}`);\n  }\n  if (lead[\"Requirement Details\"] && lead[\"Requirement Details\"] !== \"NULL\") {\n    notesParts.push(lead[\"Requirement Details\"]);\n  }\n  if (lead[\"Requirement Type\"] && lead[\"Requirement Type\"] !== \"NULL\") {\n    notesParts.push(`Type: ${lead[\"Requirement Type\"]}`);\n  }\n  if (lead[\"Date\"] && lead[\"Date\"] !== \"NULL\") {\n    notesParts.push(`Lead Date: ${lead[\"Date\"]}`);\n  }\n  \n  // Construct API payload\n  const payload = {\n    agent_id: AGENT_ID,\n    lead_name: leadName,\n    recipient_phone_number: lead[\"Phone Number 1\"],\n    email: lead[\"Email Address\"] !== \"NULL\" ? lead[\"Email Address\"] : undefined,\n    Source: lead[\"Source\"] || \"n8n\",\n    Notes: notesParts.join(\" | \") || undefined,\n    company: lead[\"Company Name\"] !== \"NULL\" ? lead[\"Company Name\"] : undefined,\n    city: lead[\"City\"] !== \"NULL\" ? lead[\"City\"] : undefined,\n    country: lead[\"Country\"] !== \"NULL\" ? lead[\"Country\"] : \"India\"\n  };\n  \n  // Remove undefined fields\n  Object.keys(payload).forEach(key => {\n    if (payload[key] === undefined) {\n      delete payload[key];\n    }\n  });\n  \n  outputItems.push({ json: payload });\n}\n\nreturn outputItems;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        0
      ],
      "id": "b2c3d4e5-f6a7-8901-bcde-f23456789012",
      "name": "Prepare API Payload"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://calling-agent-with-bolna-production.up.railway.app/api/webhooks/n8n/lead-call",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}",
        "options": {
          "timeout": 30000,
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "json"
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        448,
        0
      ],
      "id": "c3d4e5f6-a7b8-9012-cdef-345678901234",
      "name": "Trigger AI Call",
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "check-success",
              "leftValue": "={{ $json.body.success }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        672,
        0
      ],
      "id": "d4e5f6a7-b8c9-0123-def4-567890123456",
      "name": "Check API Response"
    },
    {
      "parameters": {
        "jsCode": "// Log successful call initiation\nconst response = $input.first().json;\n\nreturn [{\n  json: {\n    status: \"SUCCESS\",\n    message: \"AI call initiated successfully\",\n    contact_id: response.body?.data?.contact_id,\n    call_id: response.body?.data?.call_id,\n    execution_id: response.body?.data?.execution_id,\n    contact_created: response.body?.data?.contact_created,\n    request_id: response.body?.request_id,\n    processing_time_ms: response.body?.processing_time_ms\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        896,
        -100
      ],
      "id": "e5f6a7b8-c9d0-1234-ef56-789012345678",
      "name": "Log Success"
    },
    {
      "parameters": {
        "jsCode": "// Log failed call initiation for debugging\nconst response = $input.first().json;\n\nreturn [{\n  json: {\n    status: \"FAILED\",\n    error: response.body?.error || \"Unknown error\",\n    statusCode: response.statusCode,\n    request_id: response.body?.request_id,\n    full_response: response\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        896,
        100
      ],
      "id": "f6a7b8c9-d0e1-2345-f678-901234567890",
      "name": "Log Failure"
    }
  ],
  "connections": {
    "Filter Valid Leads": {
      "main": [
        [
          {
            "node": "Prepare API Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare API Payload": {
      "main": [
        [
          {
            "node": "Trigger AI Call",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Trigger AI Call": {
      "main": [
        [
          {
            "node": "Check API Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check API Response": {
      "main": [
        [
          {
            "node": "Log Success",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Log Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}