{
  "id": "LeadAutopilot01",
  "active": false,
  "name": "Lead Autopilot (form -> sheet -> AI reply -> team alert)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "new-lead",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "a1b2c3d4-0001-4000-8000-000000000001",
      "name": "New lead (website form)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 300],
      "webhookId": "a1b2c3d4-0001-4000-8000-0000000000aa"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "f1", "name": "name", "value": "={{ ($json.body.name || '').trim() }}", "type": "string" },
            { "id": "f2", "name": "email", "value": "={{ ($json.body.email || '').trim().toLowerCase() }}", "type": "string" },
            { "id": "f3", "name": "phone", "value": "={{ ($json.body.phone || '').replace(/\\D/g, '').replace(/^0/, '92') }}", "type": "string" },
            { "id": "f4", "name": "service", "value": "={{ $json.body.service || 'General enquiry' }}", "type": "string" },
            { "id": "f5", "name": "message", "value": "={{ $json.body.message || '' }}", "type": "string" },
            { "id": "f6", "name": "received_at", "value": "={{ $now.toFormat('dd-MM-yyyy HH:mm') }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "a1b2c3d4-0002-4000-8000-000000000002",
      "name": "Clean the data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": { "__rl": true, "mode": "url", "value": "PASTE_GOOGLE_SHEET_URL_HERE" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Leads" },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {},
          "matchingColumns": []
        },
        "options": {}
      },
      "id": "a1b2c3d4-0003-4000-8000-000000000003",
      "name": "Save to Leads sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [440, 300]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Write a short, warm reply email (max 90 words) to a new enquiry for our business.\nName: {{ $('Clean the data').item.json.name }}\nService: {{ $('Clean the data').item.json.service }}\nTheir message: {{ $('Clean the data').item.json.message }}\nRules: thank them, confirm we received the request, say a team member will contact them within 24 hours. Do not promise prices or dates. No emojis. Sign as 'Team Musa Studio'. Return only the email body."
      },
      "id": "a1b2c3d4-0004-4000-8000-000000000004",
      "name": "AI writes the reply",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [660, 300]
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.5-flash",
        "options": { "temperature": 0.4 }
      },
      "id": "a1b2c3d4-0005-4000-8000-000000000005",
      "name": "Gemini model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [660, 520],
      "credentials": {
        "googlePalmApi": { "id": "", "name": "Google Gemini(PaLM) Api account" }
      }
    },
    {
      "parameters": {
        "sendTo": "={{ $('Clean the data').item.json.email }}",
        "subject": "=Thank you, {{ $('Clean the data').item.json.name }}. We received your request",
        "emailType": "text",
        "message": "={{ $json.text }}",
        "options": {}
      },
      "id": "a1b2c3d4-0006-4000-8000-000000000006",
      "name": "Email the lead",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [880, 300]
    },
    {
      "parameters": {
        "chatId": "PASTE_TEAM_TELEGRAM_CHAT_ID",
        "text": "=New lead: {{ $('Clean the data').item.json.name }}\nService: {{ $('Clean the data').item.json.service }}\nPhone: +{{ $('Clean the data').item.json.phone }}\nEmail: {{ $('Clean the data').item.json.email }}\nMessage: {{ $('Clean the data').item.json.message }}\nWhatsApp: https://wa.me/{{ $('Clean the data').item.json.phone }}",
        "additionalFields": {}
      },
      "id": "a1b2c3d4-0007-4000-8000-000000000007",
      "name": "Alert the team (Telegram)",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1100, 300]
    },
    {
      "parameters": {
        "content": "## Lead Autopilot\n1. Website form posts to the webhook\n2. Data is cleaned (name, email, phone in 92 format)\n3. Saved to Google Sheet 'Leads'\n4. Gemini writes a personal reply\n5. Gmail sends it to the lead\n6. Team gets a Telegram alert with a WhatsApp link\n\nSetup: connect Google Sheets, Gmail, Gemini and Telegram credentials, paste the Sheet URL and Telegram chat ID.",
        "height": 300,
        "width": 420
      },
      "id": "a1b2c3d4-0008-4000-8000-000000000008",
      "name": "How it works",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [0, -40]
    }
  ],
  "connections": {
    "New lead (website form)": { "main": [[{ "node": "Clean the data", "type": "main", "index": 0 }]] },
    "Clean the data": { "main": [[{ "node": "Save to Leads sheet", "type": "main", "index": 0 }]] },
    "Save to Leads sheet": { "main": [[{ "node": "AI writes the reply", "type": "main", "index": 0 }]] },
    "Gemini model": { "ai_languageModel": [[{ "node": "AI writes the reply", "type": "ai_languageModel", "index": 0 }]] },
    "AI writes the reply": { "main": [[{ "node": "Email the lead", "type": "main", "index": 0 }]] },
    "Email the lead": { "main": [[{ "node": "Alert the team (Telegram)", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "pinData": {}
}
