{
  "name": "Lead Autopilot — MarqueTi (plantilla base)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "lead-autopilot",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000001",
      "name": "Webhook Lead",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0],
      "webhookId": "lead-autopilot"
    },
    {
      "parameters": {
        "jsCode": "const b = $input.first().json.body || $input.first().json;\nconst phone = String(b.phone || '').replace(/\\D/g, '');\nreturn [{ json: {\n  name: (b.name || '').trim(),\n  first_name: (b.name || '').trim().split(/\\s+/)[0],\n  email: String(b.email || '').trim().toLowerCase(),\n  phone_e164: phone ? '+' + phone : null,\n  company: b.company || null,\n  budget: Number(b.budget || 1),\n  message: b.message || '',\n  source: b.source || 'web',\n  created_at: new Date().toISOString()\n} }];"
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000002",
      "name": "Normalizar datos",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"gpt-4o-mini\",\n  \"response_format\": { \"type\": \"json_object\" },\n  \"messages\": [\n    { \"role\": \"system\", \"content\": \"Eres un analista comercial. Califica el lead de 0 a 100 y responde SOLO JSON con: score, tier (caliente|tibio|frio), interest, urgent (bool), summary (1 frase).\" },\n    { \"role\": \"user\", \"content\": {{ JSON.stringify(JSON.stringify($json)) }} }\n  ]\n}",
        "options": {}
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000003",
      "name": "IA: Calificar lead",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 0]
    },
    {
      "parameters": {
        "jsCode": "const lead = $('Normalizar datos').first().json;\nconst ai = JSON.parse($input.first().json.choices[0].message.content);\nreturn [{ json: { ...lead, ...ai } }];"
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000004",
      "name": "Unir lead + IA",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 0]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": { "__rl": true, "mode": "url", "value": "REEMPLAZAR_URL_GOOGLE_SHEET" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Leads" },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000005",
      "name": "CRM: Crear contacto",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [880, 0]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": false },
          "conditions": [
            {
              "leftValue": "={{ $json.tier }}",
              "rightValue": "frio",
              "operator": { "type": "string", "operation": "notEquals" }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000006",
      "name": "¿Caliente o tibio?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1100, -100]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v20.0/REEMPLAZAR_PHONE_NUMBER_ID/messages",
        "sendHeaders": true,
        "headerParameters": { "parameters": [ { "name": "Authorization", "value": "Bearer REEMPLAZAR_TOKEN_WHATSAPP" } ] },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"messaging_product\": \"whatsapp\",\n  \"to\": \"{{ $json.phone_e164 }}\",\n  \"type\": \"template\",\n  \"template\": { \"name\": \"bienvenida_lead\", \"language\": { \"code\": \"es\" },\n    \"components\": [ { \"type\": \"body\", \"parameters\": [ { \"type\": \"text\", \"text\": \"{{ $json.first_name }}\" }, { \"type\": \"text\", \"text\": \"{{ $json.interest }}\" } ] } ] }\n}",
        "options": {}
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000007",
      "name": "WhatsApp: Bienvenida",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1320, -200]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": false },
          "conditions": [
            {
              "leftValue": "={{ $('Unir lead + IA').first().json.tier }}",
              "rightValue": "caliente",
              "operator": { "type": "string", "operation": "equals" }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000008",
      "name": "¿Caliente?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1540, -200]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": { "__rl": true, "mode": "name", "value": "#ventas" },
        "text": "=🔥 Lead caliente ({{ $('Unir lead + IA').first().json.score }}/100)\n{{ $('Unir lead + IA').first().json.summary }}\n📞 {{ $('Unir lead + IA').first().json.phone_e164 }} · ✉️ {{ $('Unir lead + IA').first().json.email }}",
        "otherOptions": {}
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000009",
      "name": "Slack: Alerta vendedor",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [1760, -300]
    },
    {
      "parameters": {
        "sendTo": "={{ $json.email }}",
        "subject": "={{ $json.first_name }}, gracias por escribirnos 🚀",
        "emailType": "text",
        "message": "=Hola {{ $json.first_name }},\n\nRecibimos tu consulta sobre {{ $json.interest }}. Te contactaremos muy pronto.\n\nSaludos,\nMarqueTi",
        "options": {}
      },
      "id": "1a1a1a1a-0000-4000-8000-000000000010",
      "name": "Email: Seguimiento",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [1100, 120]
    }
  ],
  "connections": {
    "Webhook Lead": { "main": [ [ { "node": "Normalizar datos", "type": "main", "index": 0 } ] ] },
    "Normalizar datos": { "main": [ [ { "node": "IA: Calificar lead", "type": "main", "index": 0 } ] ] },
    "IA: Calificar lead": { "main": [ [ { "node": "Unir lead + IA", "type": "main", "index": 0 } ] ] },
    "Unir lead + IA": { "main": [ [ { "node": "CRM: Crear contacto", "type": "main", "index": 0 } ] ] },
    "CRM: Crear contacto": { "main": [ [ { "node": "¿Caliente o tibio?", "type": "main", "index": 0 }, { "node": "Email: Seguimiento", "type": "main", "index": 0 } ] ] },
    "¿Caliente o tibio?": { "main": [ [ { "node": "WhatsApp: Bienvenida", "type": "main", "index": 0 } ], [] ] },
    "WhatsApp: Bienvenida": { "main": [ [ { "node": "¿Caliente?", "type": "main", "index": 0 } ] ] },
    "¿Caliente?": { "main": [ [ { "node": "Slack: Alerta vendedor", "type": "main", "index": 0 } ], [] ] }
  },
  "pinData": {},
  "settings": { "executionOrder": "v1" },
  "meta": { "templateCredsSetupCompleted": false },
  "tags": []
}
