{
  "name": "CRPRO Hub — resposta automática",
  "nodes": [
    {
      "id": "b7c1c3a2-0001-4a7e-9a10-000000000001",
      "name": "Webhook do CRPRO Hub",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "webhookId": "b7c1c3a2-0001-4a7e-9a10-0000000000aa",
      "parameters": {
        "httpMethod": "POST",
        "path": "crprohub",
        "responseMode": "responseNode",
        "options": {
          "rawBody": true
        }
      }
    },
    {
      "id": "b7c1c3a2-0001-4a7e-9a10-000000000002",
      "name": "Verificar assinatura",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ],
      "parameters": {
        "jsCode": "const { createHmac, timingSafeEqual } = require('crypto')\n\nconst entrada = $input.first()\nconst headers = entrada.json.headers\n\n// A opcao Raw Body do no Webhook precisa estar ligada. Sem ela o n8n entrega\n// o corpo ja convertido em objeto, e reserializar esse objeto nao devolve os\n// mesmos bytes -- espacamento, ordem das chaves e escapes de Unicode mudam.\n// getBinaryDataBuffer le o binario em qualquer modo de armazenamento do n8n.\nconst rawBody = (await this.helpers.getBinaryDataBuffer(0, 'data')).toString('utf8')\n\nconst timestamp = headers['x-hub-timestamp']\nconst deliveryId = headers['x-hub-delivery-id']\nconst assinado = `${timestamp}.${deliveryId}.${rawBody}`\n\nconst esperada = createHmac('sha256', $env.CRPRO_WEBHOOK_SECRET).update(assinado).digest('hex')\nconst recebida = (headers['x-hub-signature-256'] ?? '').replace('sha256=', '')\n\nconst valido =\n  /^[0-9a-f]{64}$/.test(recebida) &&\n  timingSafeEqual(Buffer.from(recebida, 'hex'), Buffer.from(esperada, 'hex'))\n\nreturn [{ json: { valido, deliveryId, evento: JSON.parse(rawBody) } }]"
      }
    },
    {
      "id": "b7c1c3a2-0001-4a7e-9a10-000000000003",
      "name": "Assinatura válida?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "b7c1c3a2-0001-4a7e-9a10-0000000000c1",
              "leftValue": "={{ $json.valido }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "b7c1c3a2-0001-4a7e-9a10-000000000004",
      "name": "Responder 401",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        660,
        180
      ],
      "parameters": {
        "respondWith": "noData",
        "options": {
          "responseCode": 401
        }
      }
    },
    {
      "id": "b7c1c3a2-0001-4a7e-9a10-000000000005",
      "name": "Responder 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        660,
        0
      ],
      "parameters": {
        "respondWith": "noData",
        "options": {
          "responseCode": 200
        }
      }
    },
    {
      "id": "b7c1c3a2-0001-4a7e-9a10-000000000006",
      "name": "Ignorar reenvio",
      "type": "n8n-nodes-base.removeDuplicates",
      "typeVersion": 2,
      "position": [
        880,
        0
      ],
      "parameters": {
        "operation": "removeItemsSeenInPreviousExecutions",
        "dedupeValue": "={{ $json.evento.id }}",
        "options": {}
      }
    },
    {
      "id": "b7c1c3a2-0001-4a7e-9a10-000000000007",
      "name": "É mensagem recebida?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1100,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "b7c1c3a2-0001-4a7e-9a10-0000000000c2",
              "leftValue": "={{ $json.evento.event }}",
              "rightValue": "message.received",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "b7c1c3a2-0001-4a7e-9a10-000000000008",
      "name": "Responder no WhatsApp",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "https://crprohub.com/api/v1/channels/COLE_O_ID_DO_CANAL/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "=resposta-{{ $json.evento.data.message_id }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"to\": \"{{ $json.evento.data.from }}\",\n  \"type\": \"text\",\n  \"text\": { \"body\": \"Recebemos sua mensagem. Já respondemos por aqui.\" }\n}",
        "options": {}
      }
    }
  ],
  "connections": {
    "Webhook do CRPRO Hub": {
      "main": [
        [
          {
            "node": "Verificar assinatura",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verificar assinatura": {
      "main": [
        [
          {
            "node": "Assinatura válida?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assinatura válida?": {
      "main": [
        [
          {
            "node": "Responder 200",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Responder 401",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Responder 200": {
      "main": [
        [
          {
            "node": "Ignorar reenvio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ignorar reenvio": {
      "main": [
        [
          {
            "node": "É mensagem recebida?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "É mensagem recebida?": {
      "main": [
        [
          {
            "node": "Responder no WhatsApp",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}