{
  "name": "Facebook lead ad instant reply",
  "nodes": [
    {
      "parameters": {
        "content": "## Facebook lead ad instant reply\n\nPortfolio demo only. This workflow is an original example for demonstration. It is not client work and it does not represent a deployment for any customer.\n\nRun **Run demo** to execute the offline rules engine. The live branch is used only when the incoming item sets `demo` to false and the placeholder credentials have been replaced.",
        "height": 220,
        "width": 520,
        "color": 4
      },
      "id": "760e50e3-8ac2-4a79-9f0c-f65245709cfc",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -40
      ]
    },
    {
      "parameters": {},
      "id": "4aea21f4-de80-4cff-909f-0c49cd14a108",
      "name": "Run demo",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        40,
        280
      ],
      "notes": "CLI and the n8n editor start here. An empty manual run loads the bundled sample.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "facebook-lead-ad-instant-reply",
        "authentication": "none",
        "responseMode": "lastNode",
        "responseData": "firstEntryJson",
        "options": {}
      },
      "id": "699302f2-dc44-4edf-9c7e-ea5d21a9cca3",
      "name": "Inbound webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        40,
        460
      ],
      "notes": "POST JSON to this path. Include demo:false only after credentials are connected.",
      "notesInFlow": true,
      "webhookId": "9c348dcb-5053-4817-9c0b-a8b8541fccc4"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const sample = {\n  \"demo\": true,\n  \"firstName\": \"Ibrahim\",\n  \"email\": \"ibrahim@fieldnote.example\",\n  \"phone\": \"+15555550144\",\n  \"company\": \"Fieldnote\",\n  \"source\": \"facebook-lead\",\n  \"receivedAt\": \"2026-11-25T08:00:00.000Z\"\n};\nconst incoming = $input.first()?.json ?? {};\nconst provided = Object.keys(incoming).filter((key) => incoming[key] !== undefined && incoming[key] !== null && incoming[key] !== '');\nconst base = provided.length ? { ...sample, ...incoming } : { ...sample };\nconst flag = incoming.demo;\nbase.demo = !(flag === false || flag === 'false' || flag === 0 || flag === '0');\nreturn [{ json: base }];\n"
      },
      "id": "e086d169-beea-4ca1-844f-c0d2927e8415",
      "name": "Normalize input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        340,
        360
      ],
      "notes": "Merges the request over the bundled sample. demo stays true unless the caller explicitly turns it off.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nconst produced = (() => {\n\nconst received = Date.parse(item.receivedAt || new Date().toISOString());\nconst ageSeconds = Math.max(0, Math.round((Date.now() - received) / 1000));\nconst withinWindow = ageSeconds <= 1 * 60;\nconst owners = ['Maya'];\nconst bucket = Math.abs(hash(String(item.email || item.phone || 'lead'))) % owners.length;\nconst owner = item.owner || owners[bucket];\nconst first = item.firstName || 'there';\nconst company = item.company || 'your team';\nconst message = withinWindow\n  ? 'Hi ' + first + ', this is ' + owner + ' at Northwind. I just saw the ' + (item.source || 'request') + ' from ' + company + ' and can reply right now.'\n  : 'Hi ' + first + ', this is ' + owner + '. Sorry we missed the first minute. I can still help ' + company + ' today.';\nconst record = {\n  to: item.phone || '',\n  email: item.email || '',\n  from: '+15555550100',\n  owner,\n  message,\n  subject: 'Re: ' + (item.source || 'your request'),\n  body: message,\n  ageSeconds,\n  withinWindow,\n  channel: 'whatsapp',\n};\nreturn {\n  summary: (withinWindow ? 'Inside' : 'Outside') + ' the 1-minute window. ' + owner + ' owns the whatsapp reply.',\n  route: withinWindow ? 'instant' : 'late',\n  record,\n  slackText: owner + ' should answer ' + (item.email || item.phone) + ' (' + ageSeconds + 's old).',\n  actions: [{ app: 'whatsapp', operation: 'send', payload: record }],\n};\nfunction hash(value) {\n  let h = 0;\n  for (let i = 0; i < value.length; i++) h = (h * 31 + value.charCodeAt(i)) >>> 0;\n  return h;\n}\n\n})();\nif (!produced || typeof produced.summary !== 'string' || !produced.summary.trim()) {\n  throw new Error('Apply rules must return a summary string');\n}\nreturn [{\n  json: {\n    ...item,\n    ...produced,\n    demo: item.demo !== false,\n  },\n}];\n"
      },
      "id": "8392a60d-b6ed-4308-99bf-77b2499b4faa",
      "name": "Apply rules",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        620,
        360
      ],
      "notes": "Deterministic stand-in for the model or enrichment API. Replace or precede this with a live model node when you turn demo mode off.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "c7741d7a-6de3-4769-a126-f9c50d0e16ae",
              "leftValue": "={{ $json.demo }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "3e687e48-b95b-488c-bf95-8f6ab9bf5feb",
      "name": "Demo mode?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        900,
        360
      ],
      "notes": "True: package the offline result. False: run the credentialed apps.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const item = $input.first().json;\nreturn [{\n  json: {\n    demo: true,\n    status: 'demo_ready',\n    workflow: 'facebook-lead-ad-instant-reply',\n    summary: item.summary,\n    route: item.route ?? null,\n    record: item.record ?? {},\n    actions: item.actions ?? [],\n    slackText: item.slackText ?? item.summary,\n    notes: 'Demo mode did not call external apps. Send {\"demo\": false} after placeholder credentials are replaced.',\n  },\n}];\n"
      },
      "id": "7cd38e0f-81ea-4a67-9996-81342e47dd88",
      "name": "Package demo result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1180,
        180
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "send",
        "messageType": "text",
        "phoneNumberId": "YOUR_PHONE_NUMBER_ID",
        "recipientPhoneNumber": "={{ $json.record.phone || $json.phone || '+15555550100' }}",
        "textBody": "={{ $json.record.message || $json.slackText || $json.summary }}"
      },
      "id": "af872881-345d-4fba-a71b-a244f60c3d8a",
      "name": "Send WhatsApp message",
      "type": "n8n-nodes-base.whatsApp",
      "typeVersion": 1.1,
      "position": [
        1180,
        760
      ],
      "credentials": {
        "whatsAppApi": {
          "id": "PLACEHOLDER_WHATSAPPAPI",
          "name": "WhatsApp account"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "sheet",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_SPREADSHEET_ID"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Meta leads"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "4043de9f-faef-42fc-ac11-340fcf222566",
      "name": "Append Meta leads",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1460,
        760
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "PLACEHOLDER_GOOGLESHEETSOAUTH2API",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#ads"
        },
        "text": "={{ $json.slackText || $json.summary }}",
        "otherOptions": {}
      },
      "id": "c5ed8610-4a7c-41a1-841e-ee8604ccbf7d",
      "name": "Post #ads",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1740,
        760
      ],
      "credentials": {
        "slackOAuth2Api": {
          "id": "PLACEHOLDER_SLACKOAUTH2API",
          "name": "Slack account"
        }
      }
    },
    {
      "parameters": {
        "content": "## Live path\n\nReplace every `PLACEHOLDER_*` credential, then POST the sample with `\"demo\": false`.\n\nThe nodes on this branch use the `record` and `actions` fields produced by Apply rules.",
        "height": 180,
        "width": 420,
        "color": 6
      },
      "id": "141246a3-8f0c-41a0-8c7d-a35079789afc",
      "name": "Live path",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1120,
        520
      ]
    }
  ],
  "connections": {
    "Run demo": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Inbound webhook": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize input": {
      "main": [
        [
          {
            "node": "Apply rules",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Apply rules": {
      "main": [
        [
          {
            "node": "Demo mode?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Demo mode?": {
      "main": [
        [
          {
            "node": "Package demo result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send WhatsApp message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp message": {
      "main": [
        [
          {
            "node": "Append Meta leads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append Meta leads": {
      "main": [
        [
          {
            "node": "Post #ads",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "A lead-ad webhook is answered on WhatsApp inside a minute and logged to a sheet for the closer."
  },
  "active": false
}
