{
  "name": "Mursilat — receive webhook events (signed)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "mursilat",
        "responseMode": "responseNode",
        "options": { "rawBody": true }
      },
      "id": "7a2e3b50-0002-4c1a-9a01-000000000001",
      "name": "Mursilat Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 300],
      "webhookId": "5b8f1c7e-mursilat-0000-0000-000000000001"
    },
    {
      "parameters": {
        "jsCode": "// Verifies X-Wasla-Signature: t=<unix>,v1=<hex HMAC-SHA256(secret, \"<t>.<raw body>\")>\n// on the RAW body (Webhook node option \"Raw Body\" must stay ON).\nconst crypto = require('crypto');\n\n// Self-hosted: set the env var MURSILAT_WEBHOOK_SECRET. n8n Cloud: paste the secret below.\nlet SECRET = '';\ntry { SECRET = $env.MURSILAT_WEBHOOK_SECRET || ''; } catch (e) {}\nif (!SECRET) SECRET = 'whsec_PASTE_YOUR_SECRET_HERE';\n\nconst items = $input.all();\nconst out = [];\nfor (let i = 0; i < items.length; i++) {\n  const item = items[i];\n  const headers = item.json.headers || {};\n  let raw = '';\n  const bin = item.binary && item.binary.data;\n  if (bin) {\n    try {\n      raw = (await this.helpers.getBinaryDataBuffer(i, 'data')).toString('utf8');\n    } catch (e) {\n      raw = bin.data ? Buffer.from(bin.data, 'base64').toString('utf8') : '';\n    }\n  }\n  const parts = {};\n  for (const p of String(headers['x-wasla-signature'] || '').split(',')) {\n    const j = p.indexOf('=');\n    if (j > 0) parts[p.slice(0, j).trim()] = p.slice(j + 1).trim();\n  }\n  const t = Number(parts.t);\n  const fresh = Number.isFinite(t) && Math.abs(Date.now() / 1000 - t) <= 300;\n  const expected = crypto.createHmac('sha256', SECRET).update(`${t}.${raw}`).digest('hex');\n  const valid =\n    raw.length > 0 && fresh && /^[a-f0-9]{64}$/.test(parts.v1 || '') &&\n    crypto.timingSafeEqual(Buffer.from(expected, 'hex'), Buffer.from(parts.v1, 'hex'));\n  let event = null;\n  try { event = JSON.parse(raw); } catch (e) {}\n  out.push({\n    json: {\n      valid,\n      reason: valid ? null : !raw ? 'no raw body (turn on Raw Body)' : !fresh ? 'timestamp older than 5 minutes' : 'bad signature (wrong secret?)',\n      deliveryId: headers['x-wasla-delivery'] || null,\n      eventType: headers['x-wasla-event'] || null,\n      event,\n    },\n  });\n}\nreturn out;"
      },
      "id": "7a2e3b50-0002-4c1a-9a01-000000000002",
      "name": "Verify signature",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [
            {
              "id": "c0a1b2c3-0000-4000-8000-000000000001",
              "leftValue": "={{ $json.valid }}",
              "rightValue": true,
              "operator": { "type": "boolean", "operation": "true", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "7a2e3b50-0002-4c1a-9a01-000000000003",
      "name": "Signature OK?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [480, 300]
    },
    {
      "parameters": { "respondWith": "noData", "options": { "responseCode": 200 } },
      "id": "7a2e3b50-0002-4c1a-9a01-000000000004",
      "name": "Answer 200",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [720, 200]
    },
    {
      "parameters": { "respondWith": "noData", "options": { "responseCode": 401 } },
      "id": "7a2e3b50-0002-4c1a-9a01-000000000005",
      "name": "Answer 401",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [720, 420]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "e1", "name": "eventId", "value": "={{ $('Verify signature').item.json.event.id }}", "type": "string" },
            { "id": "e2", "name": "type", "value": "={{ $('Verify signature').item.json.event.type }}", "type": "string" },
            { "id": "e3", "name": "numberId", "value": "={{ $('Verify signature').item.json.event.sessionId }}", "type": "string" },
            { "id": "e4", "name": "customerPhone", "value": "={{ $('Verify signature').item.json.event.data.from ? $('Verify signature').item.json.event.data.from.phone : ($('Verify signature').item.json.event.data.to || null) }}", "type": "string" },
            { "id": "e5", "name": "text", "value": "={{ $('Verify signature').item.json.event.data.text || null }}", "type": "string" },
            { "id": "e6", "name": "status", "value": "={{ $('Verify signature').item.json.event.data.status || null }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "id": "7a2e3b50-0002-4c1a-9a01-000000000006",
      "name": "Event fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [960, 200]
    }
  ],
  "connections": {
    "Mursilat Webhook": { "main": [[{ "node": "Verify signature", "type": "main", "index": 0 }]] },
    "Verify signature": { "main": [[{ "node": "Signature OK?", "type": "main", "index": 0 }]] },
    "Signature OK?": {
      "main": [
        [{ "node": "Answer 200", "type": "main", "index": 0 }],
        [{ "node": "Answer 401", "type": "main", "index": 0 }]
      ]
    },
    "Answer 200": { "main": [[{ "node": "Event fields", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "pinData": {}
}
