{
  "name": "Meteor IA: recibir mensajes con la firma verificada",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "meteor-ia",
        "options": {
          "rawBody": true
        }
      },
      "id": "00000000-0000-4000-8000-000000000011",
      "name": "Mensajes de Meteor IA",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "00000000-0000-4000-8000-000000000019"
    },
    {
      "parameters": {
        "jsCode": "const crypto = require('crypto');\n\nconst secreto = 'PEGA_AQUI_TU_WHSEC'; // el secreto que devolvió POST /webhook-endpoints\nconst cabecera = $input.first().json.headers['x-met-signature'] ?? '';\nconst crudo = (await this.helpers.getBinaryDataBuffer(0, 'data')).toString('utf8');\n\nconst partes = cabecera.split(',').map((p) => p.trim().split('='));\nconst t = Number(partes.find(([k]) => k === 't')?.[1]);\nif (!t || Math.abs(Date.now() / 1000 - t) > 300) throw new Error('Firma de Meteor IA ausente o vencida');\n\nconst esperada = Buffer.from(\n  crypto.createHmac('sha256', secreto).update(`${t}.${crudo}`).digest('hex'),\n);\nconst valida = partes\n  .filter(([k]) => k === 'v1')\n  .map(([, v]) => Buffer.from(v ?? ''))\n  .some((v) => v.length === esperada.length && crypto.timingSafeEqual(v, esperada));\nif (!valida) throw new Error('Firma de Meteor IA inválida');\n\nreturn [{ json: JSON.parse(crudo) }];"
      },
      "id": "00000000-0000-4000-8000-000000000012",
      "name": "Verificar firma X-Met-Signature",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        0
      ]
    },
    {
      "parameters": {},
      "id": "00000000-0000-4000-8000-000000000013",
      "name": "Tu lógica aquí",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        480,
        0
      ]
    },
    {
      "parameters": {},
      "id": "00000000-0000-4000-8000-000000000014",
      "name": "Ejecutar una vez para suscribir",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        260
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.met.meteor.com.co/api/v1/webhook-endpoints",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"url\": \"PEGA_AQUI_LA_URL_DE_PRODUCCION_DEL_WEBHOOK\",\n  \"enabled_events\": [\n    \"contact.message.received\"\n  ]\n}",
        "options": {}
      },
      "id": "00000000-0000-4000-8000-000000000015",
      "name": "Suscribir esta URL en Meteor IA",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        240,
        260
      ]
    },
    {
      "parameters": {
        "content": "## Recibir mensajes de Meteor IA\n\n1. Publica el flujo y copia la URL de producción del nodo Webhook.\n2. Pégala en el nodo «Suscribir esta URL en Meteor IA» y ejecútalo una vez con tu credencial Bearer Auth (scope `webhooks:manage`; para recibir el evento, `conversations:read`).\n3. La respuesta trae `data.secret` (`whsec_…`) una sola vez: pégalo en el nodo Code.\n4. En n8n Cloud el módulo `crypto` está disponible; si alojas n8n tú mismo, habilita los módulos del nodo Code.\n5. El secreto queda escrito en el nodo Code: no compartas ni exportes este flujo con el secreto pegado.",
        "height": 300,
        "width": 460
      },
      "id": "00000000-0000-4000-8000-000000000016",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        440
      ]
    }
  ],
  "connections": {
    "Mensajes de Meteor IA": {
      "main": [
        [
          {
            "node": "Verificar firma X-Met-Signature",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verificar firma X-Met-Signature": {
      "main": [
        [
          {
            "node": "Tu lógica aquí",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ejecutar una vez para suscribir": {
      "main": [
        [
          {
            "node": "Suscribir esta URL en Meteor IA",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
