{
  "name": "Instant Lead Reply for Real Estate Agents (Gmail + Google Sheets)",
  "nodes": [
    {
      "id": "b492447d-aaec-4af4-aee9-d5177458f406",
      "name": "Lead Form",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.6,
      "position": [
        0,
        100
      ],
      "webhookId": "fab9634c-0dc2-48d6-9862-477ec9586115",
      "parameters": {
        "authentication": "none",
        "formTitle": "Contact me about a home",
        "formDescription": "Leave your details and I'll get back to you.",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Your name",
              "fieldName": "name",
              "fieldType": "text",
              "requiredField": true
            },
            {
              "fieldLabel": "Email",
              "fieldName": "email",
              "fieldType": "email",
              "requiredField": true
            },
            {
              "fieldLabel": "I am interested in",
              "fieldName": "interest",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Buying a home"
                  },
                  {
                    "option": "Selling a home"
                  },
                  {
                    "option": "Renting"
                  }
                ]
              },
              "requiredField": true
            },
            {
              "fieldLabel": "Message",
              "fieldName": "message",
              "fieldType": "textarea",
              "requiredField": false
            }
          ]
        },
        "responseMode": "onReceived",
        "options": {
          "appendAttribution": false,
          "buttonLabel": "Send",
          "path": "lead-form",
          "ignoreBots": true,
          "respondWithOptions": {
            "values": {
              "respondWith": "text",
              "formSubmittedText": "Thanks! Check your inbox for a reply."
            }
          }
        }
      }
    },
    {
      "id": "872d82db-9209-4e29-928b-cfd55aa3c48e",
      "name": "Build Reply and Lead Row",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        224,
        100
      ],
      "parameters": {
        "jsCode": "// ===== EDIT THESE THREE VALUES =====\nconst CONFIG = {\n  agentName: 'Your Name',            // signs the reply\n  ownerEmail: 'REPLACE_WITH_YOUR_EMAIL', // where new-lead alerts go\n  callPromise: 'today',              // e.g. 'today', 'within the hour'\n};\n// ====================================\n\nconst input = $input.first().json;\nconst NL = String.fromCharCode(10);\nconst clean = (v, max) => String(v == null ? '' : v).trim().slice(0, max);\nconst esc = (s) => String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;');\n\nconst name = clean(input.name, 120);\nconst email = clean(input.email, 200).toLowerCase();\nconst interest = clean(input.interest, 80) || 'Buying a home';\nconst message = clean(input.message, 3000);\nconst firstName = name.split(' ')[0] || 'there';\nconst now = new Date();\nconst leadId = 'L-' + now.getTime().toString(36).toUpperCase();\nconst valid = name.length >= 2 && email.indexOf('@') > 0 && email.lastIndexOf('.') > email.indexOf('@');\nif (!valid) { return []; } // invalid or bot submissions: no email, no row\n\nconst WRAP = 'font-family:Segoe UI,Roboto,Helvetica,Arial,sans-serif;font-size:16px;line-height:1.6;color:#222;max-width:640px';\nconst msgHtml = message ? esc(message).split(NL).join('<br>') : '<em>No message</em>';\n\nreturn [{ json: {\n  leadId, receivedAtIso: now.toISOString(), name, email, interest, message,\n  ownerEmail: CONFIG.ownerEmail,\n  replySubject: 'Thanks ' + firstName + ', I got your message',\n  replyHtml: '<div style=\"' + WRAP + '\"><p>Hi ' + esc(firstName) + ',</p>'\n    + '<p>Thanks for reaching out about <strong>' + esc(interest.toLowerCase()) + '</strong>. I have your message and I will call or email you personally ' + esc(CONFIG.callPromise) + '.</p>'\n    + '<p>If it is urgent, reply to this email with the best number and time to reach you.</p>'\n    + '<p>Talk soon,<br>' + esc(CONFIG.agentName) + '</p></div>',\n  notifySubject: 'New lead: ' + name,\n  notifyHtml: '<div style=\"' + WRAP + '\"><p><strong>Name:</strong> ' + esc(name) + '<br><strong>Email:</strong> ' + esc(email)\n    + '<br><strong>Interest:</strong> ' + esc(interest) + '<br><strong>Lead ID:</strong> ' + leadId + '</p><p><strong>Message:</strong><br>' + msgHtml + '</p></div>',\n} }];",
        "language": "javaScript",
        "mode": "runOnceForAllItems"
      }
    },
    {
      "id": "aa10b5db-1721-41c1-851d-06e42db87d2a",
      "name": "Send Instant Reply to Lead",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        496,
        0
      ],
      "webhookId": "8f546d28-c738-4eb5-bf44-4d9a63e88710",
      "onError": "continueErrorOutput",
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "={{ $json.email }}",
        "subject": "={{ $json.replySubject }}",
        "emailType": "html",
        "message": "={{ $json.replyHtml }}",
        "options": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "40a5287d-0de9-4843-96f7-b5b17c2cf62f",
      "name": "Alert Owner: Reply Failed",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        720,
        0
      ],
      "webhookId": "8d0ca6d9-159d-42c7-b989-11660ee26d5a",
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "={{ $('Build Reply and Lead Row').item.json.ownerEmail }}",
        "subject": "=Auto-reply FAILED for {{ $('Build Reply and Lead Row').item.json.name }}",
        "emailType": "html",
        "message": "=<p>The automatic reply could not be sent to <b>{{ $('Build Reply and Lead Row').item.json.email }}</b>. Contact them manually.</p>",
        "options": {
          "appendAttribution": false
        }
      }
    },
    {
      "id": "db944888-28e1-47a5-b1cb-15186edd2b04",
      "name": "Log Lead to Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        496,
        220
      ],
      "onError": "continueRegularOutput",
      "parameters": {
        "resource": "sheet",
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Leads"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Received": "={{ $json.receivedAtIso }}",
            "Lead ID": "={{ $json.leadId }}",
            "Name": "={{ $json.name }}",
            "Email": "={{ $json.email }}",
            "Interest": "={{ $json.interest }}",
            "Message": "={{ $json.message }}"
          },
          "schema": [
            {
              "id": "Received",
              "displayName": "Received",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Lead ID",
              "displayName": "Lead ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Name",
              "displayName": "Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Email",
              "displayName": "Email",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Interest",
              "displayName": "Interest",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Message",
              "displayName": "Message",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      }
    },
    {
      "id": "95f754ef-f6a1-472d-8baa-f2e502cf5c9d",
      "name": "Notify Owner of New Lead",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        720,
        220
      ],
      "webhookId": "bbc03de9-27e1-4d30-bee8-84c9643a8c64",
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "={{ $('Build Reply and Lead Row').item.json.ownerEmail }}",
        "subject": "={{ $('Build Reply and Lead Row').item.json.notifySubject }}",
        "emailType": "html",
        "message": "={{ $('Build Reply and Lead Row').item.json.notifyHtml }}<p style=\"font-size:14px\">{{ $json.error ? 'NOT saved to Google Sheet.' : 'Saved to Google Sheet.' }}</p>",
        "options": {
          "appendAttribution": false,
          "replyTo": "={{ $('Build Reply and Lead Row').item.json.email }}"
        }
      }
    },
    {
      "id": "6b684333-9abf-47d5-93f3-3e6f54fe0b69",
      "name": "Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        380
      ],
      "parameters": {
        "width": 760,
        "height": 300,
        "color": 4,
        "content": "## Instant Lead Reply: setup (about 15 minutes)\n1. Connect your Gmail account on the three Gmail nodes (Google sign-in, no password shared).\n2. Create a Google Sheet with a tab named **Leads** and this header row: Received | Lead ID | Name | Email | Interest | Message. Pick it in *Log Lead to Google Sheet*.\n3. Edit the three values at the top of *Build Reply and Lead Row*.\n4. Send yourself a test through the form, then publish. Embed the form URL on your site or link to it from listings.\n\nEmail only, no texting. Free template by V2Developer: https://www.v2developer.com/instant-lead-reply"
      }
    }
  ],
  "connections": {
    "Lead Form": {
      "main": [
        [
          {
            "node": "Build Reply and Lead Row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Reply and Lead Row": {
      "main": [
        [
          {
            "node": "Send Instant Reply to Lead",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log Lead to Google Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Instant Reply to Lead": {
      "main": [
        [],
        [
          {
            "node": "Alert Owner: Reply Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Lead to Google Sheet": {
      "main": [
        [
          {
            "node": "Notify Owner of New Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}