{
  "name": "WhatsApp Lead Follow-Up — Wati reference workflow",
  "nodes": [
    {
      "parameters": {
        "content": "## WhatsApp Lead Follow-Up (free reference workflow)\n\nExample intake → Wati template request → Google Sheet → reply check before one follow-up after 24 hours.\n\nRequires approved templates, permission to contact the recipient, Wati API credentials and a configured Google Sheet. Other providers need their own request and response adapters. No response-time or delivery guarantee. Test before enabling.\n\nExpected payload: {\"name\":\"...\",\"phone\":\"+15551234567\",\"source\":\"website\",\"message\":\"...\"}\n\nTemplates: lead_instant_reply and lead_follow_up_24h, both with a name variable. Reply history must have messages.items with boolean owner and valid created/timestamp values. Empty, unknown or potentially incomplete history stops the workflow.\n\nhttps://trystackpick.com/templates/",
        "height": 540,
        "width": 420
      },
      "id": "note-setup",
      "name": "Setup notes",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -720,
        -160
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "lead-intake",
        "options": {}
      },
      "id": "webhook-lead",
      "name": "New lead (webhook)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -240,
        80
      ],
      "webhookId": "lead-intake"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1",
              "name": "name",
              "value": "={{ $json.body.name || 'there' }}",
              "type": "string"
            },
            {
              "id": "a2",
              "name": "phone",
              "value": "={{ ($json.body.phone || '').replace(/[^+\\d]/g, '') }}",
              "type": "string"
            },
            {
              "id": "a3",
              "name": "source",
              "value": "={{ $json.body.source || 'unknown' }}",
              "type": "string"
            },
            {
              "id": "a4",
              "name": "message",
              "value": "={{ $json.body.message || '' }}",
              "type": "string"
            },
            {
              "id": "a5",
              "name": "received_at",
              "value": "={{ $now.toISO() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "set-normalize",
      "name": "Normalize lead",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -20,
        80
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://live-mt-server.wati.io/YOUR_TENANT_ID/api/v1/sendTemplateMessage?whatsappNumber={{ $json.phone }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_WATI_API_KEY"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ template_name: 'lead_instant_reply', broadcast_name: 'lead_instant_reply', parameters: [{ name: 'name', value: $json.name }] }) }}",
        "options": {}
      },
      "id": "http-instant-reply",
      "name": "Send instant WhatsApp reply",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        200,
        80
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "received_at": "={{ $('Normalize lead').item.json.received_at }}",
            "name": "={{ $('Normalize lead').item.json.name }}",
            "phone": "={{ $('Normalize lead').item.json.phone }}",
            "source": "={{ $('Normalize lead').item.json.source }}",
            "message": "={{ $('Normalize lead').item.json.message }}",
            "status": "instant_reply_sent"
          }
        },
        "options": {}
      },
      "id": "sheets-log",
      "name": "Log lead to sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        420,
        80
      ]
    },
    {
      "parameters": {
        "amount": 24,
        "unit": "hours"
      },
      "id": "wait-24h",
      "name": "Wait 24 hours",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        640,
        80
      ],
      "webhookId": "wait-24h-lead"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://live-mt-server.wati.io/YOUR_TENANT_ID/api/v1/sendTemplateMessage?whatsappNumber={{ $('Normalize lead').item.json.phone }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_WATI_API_KEY"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ template_name: 'lead_follow_up_24h', broadcast_name: 'lead_follow_up_24h', parameters: [{ name: 'name', value: $('Normalize lead').item.json.name }] }) }}",
        "options": {}
      },
      "id": "http-follow-up",
      "name": "Send 24h follow-up",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        860,
        80
      ]
    },
    {
      "parameters": {
        "content": "The 24-hour follow-up now requires verified reply history. An inbound reply stops the cadence; unknown, empty or potentially incomplete history raises an error. Review Wati pagination/fields and use test numbers before enabling.",
        "height": 180,
        "width": 320
      },
      "id": "note-replied",
      "name": "Reply-check tip",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        820,
        -140
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://live-mt-server.wati.io/YOUR_TENANT_ID/api/v1/getMessages/{{ $('Normalize lead').item.json.phone }}?pageSize=100&pageNumber=1",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_WATI_API_KEY"
            }
          ]
        },
        "options": {}
      },
      "id": "verify-replies-before-followup",
      "name": "Check replies (24h)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1480,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Reference Wati adapter: stop instead of treating missing/unrecognized history as no reply.\nconst lead = $('Normalize lead').first().json;\nconst response = $input.first().json;\nconst msgs = response.messages && response.messages.items;\nconst since = Date.parse(lead.received_at);\nif (!Number.isFinite(since) || !Array.isArray(msgs) || response.result === false) {\n  throw new Error('Reply history could not be verified. Check the Wati response adapter before sending.');\n}\nif (msgs.length === 0 || msgs.length >= 100 || response.messages.hasMore === true ||\n    (Number.isFinite(response.messages.totalCount) && response.messages.totalCount > msgs.length)) {\n  throw new Error('Reply history is empty or may be incomplete. Review/paginate it before sending.');\n}\nfunction messageTime(m) {\n  const raw = m.created ?? m.timestamp;\n  if (typeof raw === 'number') return raw < 100000000000 ? raw * 1000 : raw;\n  if (typeof raw === 'string' && /^\\d+(\\.\\d+)?$/.test(raw)) {\n    const n = Number(raw); return n < 100000000000 ? n * 1000 : n;\n  }\n  return typeof raw === 'string' ? Date.parse(raw) : NaN;\n}\nlet replied = false;\nfor (const m of msgs) {\n  const at = messageTime(m);\n  if (!Number.isFinite(at) || typeof m.owner !== 'boolean') {\n    throw new Error('Unknown message timestamp/direction. Validate the Wati response mapping before sending.');\n  }\n  if (m.owner === false && at > since) replied = true;\n}\nreturn [{ json: { ...lead, replied } }];\n"
      },
      "id": "classify-replies-before-followup",
      "name": "Replied? (24h)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1700,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "no-reply",
              "leftValue": "={{ $json.replied }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "false",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "allow-followup-with-verified-history",
      "name": "No reply yet (24h)?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1920,
        300
      ]
    }
  ],
  "connections": {
    "New lead (webhook)": {
      "main": [
        [
          {
            "node": "Normalize lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize lead": {
      "main": [
        [
          {
            "node": "Send instant WhatsApp reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send instant WhatsApp reply": {
      "main": [
        [
          {
            "node": "Log lead to sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log lead to sheet": {
      "main": [
        [
          {
            "node": "Wait 24 hours",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 24 hours": {
      "main": [
        [
          {
            "node": "Check replies (24h)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check replies (24h)": {
      "main": [
        [
          {
            "node": "Replied? (24h)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Replied? (24h)": {
      "main": [
        [
          {
            "node": "No reply yet (24h)?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "No reply yet (24h)?": {
      "main": [
        [
          {
            "node": "Send 24h follow-up",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
