{
  "name": "AutoLatam — RespondeLeads WhatsApp",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "whatsapp-inbound",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "a1b2c3d4-0001-4000-8000-000000000001",
      "name": "Webhook WhatsApp entrante",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [-460, 300],
      "notes": "Apunta aquí el webhook de la WhatsApp Business API (o Twilio/360dialog)."
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "phone",
              "name": "phone",
              "value": "={{ $json.entry?.[0]?.changes?.[0]?.value?.messages?.[0]?.from || $json.From }}",
              "type": "string"
            },
            {
              "id": "text",
              "name": "text",
              "value": "={{ $json.entry?.[0]?.changes?.[0]?.value?.messages?.[0]?.text?.body || $json.Body }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "a1b2c3d4-0002-4000-8000-000000000002",
      "name": "Extraer teléfono y mensaje",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [-240, 300]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "content": "=Eres el asistente comercial de la empresa. Responde en español, máximo 80 palabras. Clasifica el mensaje como LEAD (pregunta por servicios/precios) o SOPORTE (otro tema), responde de forma útil y, si es LEAD, invita a agendar una llamada. Mensaje del cliente: {{ $json.text }}"
            }
          ]
        },
        "jsonOutput": false,
        "options": { "temperature": 0.3 }
      },
      "id": "a1b2c3d4-0003-4000-8000-000000000003",
      "name": "IA: clasifica y responde",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [0, 300],
      "notes": "Configura tu credencial OpenAI/Azure OpenAI aquí."
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "TU_SPREADSHEET_ID",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Leads",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "telefono": "={{ $('Extraer teléfono y mensaje').item.json.phone }}",
            "mensaje": "={{ $('Extraer teléfono y mensaje').item.json.text }}",
            "respuesta_ia": "={{ $json.message?.content ?? $json.text ?? '' }}",
            "fecha": "={{ $now.toISO() }}"
          },
          "matchingColumns": [],
          "schema": []
        },
        "options": {}
      },
      "id": "a1b2c3d4-0004-4000-8000-000000000004",
      "name": "Registrar lead en Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [240, 180]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://graph.facebook.com/v21.0/TU_PHONE_NUMBER_ID/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer TU_TOKEN_WHATSAPP"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\"messaging_product\":\"whatsapp\",\"to\":\"{{ $('Extraer teléfono y mensaje').item.json.phone }}\",\"type\":\"text\",\"text\":{\"body\":\"{{ $json.message?.content ?? $json.text ?? '' }}\"}}",
        "options": {}
      },
      "id": "a1b2c3d4-0005-4000-8000-000000000005",
      "name": "Responder por WhatsApp",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [240, 420]
    }
  ],
  "connections": {
    "Webhook WhatsApp entrante": {
      "main": [[{ "node": "Extraer teléfono y mensaje", "type": "main", "index": 0 }]]
    },
    "Extraer teléfono y mensaje": {
      "main": [[{ "node": "IA: clasifica y responde", "type": "main", "index": 0 }]]
    },
    "IA: clasifica y responde": {
      "main": [
        [
          { "node": "Registrar lead en Sheets", "type": "main", "index": 0 },
          { "node": "Responder por WhatsApp", "type": "main", "index": 0 }
        ]
      ]
    }
  },
  "settings": { "executionOrder": "v1" },
  "active": false
}
