{
  "name": "WATI Pro — WhatsApp Inbound + Send",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "wati-incoming",
        "responseMode": "responseNode",
        "options": { "rawBody": true }
      },
      "id": "WebhookReceive",
      "name": "Tenant Webhook (WATI → n8n)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 320],
      "webhookId": "wati-incoming"
    },
    {
      "parameters": {
        "functionCode": "// Verify x-webhook-signature: sha256=<hex(hmac_sha256(secret, rawBody))>\nconst crypto = require('crypto');\nconst secret = $env.WATI_WEBHOOK_SECRET || '';\nconst header = ($json.headers && ($json.headers['x-webhook-signature'] || $json.headers['X-Webhook-Signature'])) || '';\nconst raw = $binary && $binary.data ? Buffer.from($binary.data.data, 'base64').toString('utf8') : JSON.stringify($json.body || {});\nconst expected = crypto.createHmac('sha256', secret).update(raw).digest('hex');\nconst got = header.replace(/^sha256=/, '');\nlet ok = false;\ntry { ok = got.length === expected.length && crypto.timingSafeEqual(Buffer.from(got,'hex'), Buffer.from(expected,'hex')); } catch (_) { ok = false; }\nif (!ok) { throw new Error('Invalid WATI webhook signature'); }\nconst payload = JSON.parse(raw);\nreturn [{ json: payload }];\n"
      },
      "id": "VerifySig",
      "name": "Verify HMAC signature",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [500, 320]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            { "value1": "={{$json.text || ''}}", "operation": "regex", "value2": "(?i)^\\s*(menu|help|hi|hello)\\b" }
          ]
        }
      },
      "id": "IfGreeting",
      "name": "Is greeting?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [760, 320]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://wati.pro/api/public/v1/send",
        "authentication": "none",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "=Bearer {{$env.WATI_API_KEY}}" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"to\": \"{{$json.from}}\",\n  \"message\": \"👋 Hi! Reply with PRICE for our catalog, or HUMAN to reach an agent.\"\n}",
        "options": { "timeout": 15000 }
      },
      "id": "SendText",
      "name": "WATI → Send text",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1020, 200]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://wati.pro/api/public/v1/send-media",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            { "name": "Authorization", "value": "=Bearer {{$env.WATI_API_KEY}}" },
            { "name": "Content-Type", "value": "application/json" }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"to\": \"{{$json.from}}\",\n  \"type\": \"document\",\n  \"url\": \"https://example.com/catalog.pdf\",\n  \"fileName\": \"catalog.pdf\",\n  \"caption\": \"Here's our latest catalog 📒\"\n}",
        "options": { "timeout": 20000 }
      },
      "id": "SendMedia",
      "name": "WATI → Send media",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1020, 440]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": true }"
      },
      "id": "Respond",
      "name": "Respond 200 OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [1280, 320]
    }
  ],
  "connections": {
    "Tenant Webhook (WATI → n8n)": { "main": [[{ "node": "Verify HMAC signature", "type": "main", "index": 0 }]] },
    "Verify HMAC signature": { "main": [[{ "node": "Is greeting?", "type": "main", "index": 0 }]] },
    "Is greeting?": {
      "main": [
        [{ "node": "WATI → Send text", "type": "main", "index": 0 }],
        [{ "node": "WATI → Send media", "type": "main", "index": 0 }]
      ]
    },
    "WATI → Send text": { "main": [[{ "node": "Respond 200 OK", "type": "main", "index": 0 }]] },
    "WATI → Send media": { "main": [[{ "node": "Respond 200 OK", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "Receives WATI Pro tenant webhook (HMAC-verified), branches on message text, and replies via WATI send / send-media REST APIs.",
    "envVars": ["WATI_WEBHOOK_SECRET", "WATI_API_KEY"]
  }
}
