{
  "name": "Captura de Leads de Facebook a Google Sheets",
  "nodes": [
    {
      "parameters": {
        "formId": "",
        "options": {}
      },
      "id": "a1b2c3d4-0001-4a01-b001-ef1234567001",
      "name": "Facebook Lead Ads Trigger",
      "type": "n8n-nodes-base.facebookLeadAdsTrigger",
      "typeVersion": 1,
      "position": [250, 300],
      "credentials": {
        "facebookLeadAdsApi": {
          "id": "",
          "name": "Facebook Lead Ads account"
        }
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "nombre",
              "value": "={{ $json.full_name }}"
            },
            {
              "name": "telefono",
              "value": "={{ $json.phone_number }}"
            },
            {
              "name": "correo",
              "value": "={{ $json.email }}"
            },
            {
              "name": "ciudad",
              "value": "={{ $json.city }}"
            },
            {
              "name": "fecha",
              "value": "={{ $now.toISO() }}"
            }
          ]
        },
        "options": {}
      },
      "id": "a1b2c3d4-0001-4a02-b002-ef1234567002",
      "name": "Formatear Datos del Lead",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [500, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "byName",
          "value": "Leads"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      },
      "id": "a1b2c3d4-0001-4a03-b003-ef1234567003",
      "name": "Google Sheets - Guardar Lead",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [750, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.ciudad }}",
              "operation": "contains",
              "value2": "Quito"
            }
          ]
        }
      },
      "id": "a1b2c3d4-0001-4a04-b004-ef1234567004",
      "name": "IF Ciudad es Quito",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [1000, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://graph.facebook.com/v17.0/FROM_PHONE_NUMBER_ID/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "messaging_product",
              "value": "whatsapp"
            },
            {
              "name": "to",
              "value": "=VENDEDOR_QUITO_PHONE"
            },
            {
              "name": "type",
              "value": "text"
            },
            {
              "name": "text",
              "value": "={{ JSON.stringify({ body: '🟢 Nuevo lead de Quito:\\nNombre: ' + $json.nombre + '\\nTeléfono: ' + $json.telefono + '\\nCorreo: ' + $json.correo }) }}"
            }
          ]
        },
        "options": {}
      },
      "id": "a1b2c3d4-0001-4a05-b005-ef1234567005",
      "name": "WhatsApp - Vendedor Quito",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1250, 200]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://graph.facebook.com/v17.0/FROM_PHONE_NUMBER_ID/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "messaging_product",
              "value": "whatsapp"
            },
            {
              "name": "to",
              "value": "=VENDEDOR_GUAYAQUIL_PHONE"
            },
            {
              "name": "type",
              "value": "text"
            },
            {
              "name": "text",
              "value": "={{ JSON.stringify({ body: '🟢 Nuevo lead de Guayaquil/otra ciudad:\\nNombre: ' + $json.nombre + '\\nTeléfono: ' + $json.telefono + '\\nCorreo: ' + $json.correo }) }}"
            }
          ]
        },
        "options": {}
      },
      "id": "a1b2c3d4-0001-4a06-b006-ef1234567006",
      "name": "WhatsApp - Vendedor Guayaquil",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1250, 400]
    }
  ],
  "connections": {
    "Facebook Lead Ads Trigger": {
      "main": [
        [
          {
            "node": "Formatear Datos del Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Formatear Datos del Lead": {
      "main": [
        [
          {
            "node": "Google Sheets - Guardar Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Guardar Lead": {
      "main": [
        [
          {
            "node": "IF Ciudad es Quito",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF Ciudad es Quito": {
      "main": [
        [
          {
            "node": "WhatsApp - Vendedor Quito",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "WhatsApp - Vendedor Guayaquil",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "lead-generation"
    },
    {
      "name": "facebook"
    },
    {
      "name": "whatsapp"
    }
  ]
}
