{
  "name": "Respuesta Automática a DMs de Instagram con CRM",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "instagram-dm-webhook",
        "options": {}
      },
      "id": "b2c3d4e5-0002-4a01-b001-ef1234567001",
      "name": "Webhook - Instagram DM",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 300]
    },
    {
      "parameters": {
        "functionCode": "const message = items[0].json.entry[0].messaging[0].message.text.toLowerCase();\nconst senderId = items[0].json.entry[0].messaging[0].sender.id;\n\nlet intent = 'general';\nlet autoReply = 'Gracias por escribirnos. Un asesor te contactará pronto.';\n\nif (message.includes('precio') || message.includes('costo') || message.includes('cuánto')) {\n  intent = 'precio';\n  autoReply = '¡Hola! Gracias por tu interés. Te enviamos nuestro catálogo con precios actualizados. ¿Hay algún producto específico que te interese?';\n} else if (message.includes('disponible') || message.includes('stock') || message.includes('hay')) {\n  intent = 'disponibilidad';\n  autoReply = '¡Hola! Déjanos verificar disponibilidad. ¿Podrías indicarnos qué producto o talla necesitas?';\n} else if (message.includes('envío') || message.includes('entrega') || message.includes('delivery')) {\n  intent = 'envio';\n  autoReply = 'Realizamos envíos a todo Ecuador. Quito y Guayaquil: 24-48h. Otras ciudades: 3-5 días. ¿A qué ciudad necesitas el envío?';\n}\n\nreturn [{\n  json: {\n    senderId,\n    message: items[0].json.entry[0].messaging[0].message.text,\n    intent,\n    autoReply,\n    timestamp: new Date().toISOString()\n  }\n}];"
      },
      "id": "b2c3d4e5-0002-4a02-b002-ef1234567002",
      "name": "Analizar Intención del Mensaje",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [500, 300]
    },
    {
      "parameters": {
        "rules": {
          "rules": [
            {
              "value": "precio",
              "output": 0
            },
            {
              "value": "disponibilidad",
              "output": 1
            },
            {
              "value": "envio",
              "output": 2
            }
          ]
        },
        "fallbackOutput": 3,
        "dataType": "string",
        "value1": "={{ $json.intent }}"
      },
      "id": "b2c3d4e5-0002-4a03-b003-ef1234567003",
      "name": "Switch - Tipo de Consulta",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 2,
      "position": [750, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v17.0/me/messages?access_token=PAGE_ACCESS_TOKEN",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "recipient",
              "value": "={{ JSON.stringify({ id: $json.senderId }) }}"
            },
            {
              "name": "message",
              "value": "={{ JSON.stringify({ text: $json.autoReply }) }}"
            }
          ]
        },
        "options": {}
      },
      "id": "b2c3d4e5-0002-4a04-b004-ef1234567004",
      "name": "Enviar Respuesta Instagram",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [1000, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "byName",
          "value": "CRM Instagram"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "ID_Instagram": "={{ $json.senderId }}",
            "Mensaje": "={{ $json.message }}",
            "Intención": "={{ $json.intent }}",
            "Fecha": "={{ $json.timestamp }}",
            "Estado": "Nuevo"
          }
        },
        "options": {}
      },
      "id": "b2c3d4e5-0002-4a05-b005-ef1234567005",
      "name": "Google Sheets - Registrar Contacto",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [1250, 300],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "",
          "name": "Google Sheets account"
        }
      }
    }
  ],
  "connections": {
    "Webhook - Instagram DM": {
      "main": [
        [
          {
            "node": "Analizar Intención del Mensaje",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analizar Intención del Mensaje": {
      "main": [
        [
          {
            "node": "Switch - Tipo de Consulta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch - Tipo de Consulta": {
      "main": [
        [
          {
            "node": "Enviar Respuesta Instagram",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Enviar Respuesta Instagram",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Enviar Respuesta Instagram",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Enviar Respuesta Instagram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviar Respuesta Instagram": {
      "main": [
        [
          {
            "node": "Google Sheets - Registrar Contacto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "lead-generation"
    },
    {
      "name": "instagram"
    },
    {
      "name": "crm"
    }
  ]
}
