{
  "id": "peskids-wacrm-inbound",
  "name": "Peskids wacrm inbound \u2192 Opsly webhook",
  "active": true,
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "wacrm-peskids-inbound",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "Webhook",
      "name": "wacrm inbound",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "const item = $input.first().json;\nconst raw = item.body ?? item;\nconst body = typeof raw === 'string' ? JSON.parse(raw) : raw;\nconst phone = String(body.phone || body.from || body.sender || body.wa_id || '').trim();\nconst text = String(body.body || body.text || body.message || '').trim();\nconst externalMessageId = String(body.external_message_id || body.message_id || body.id || `wacrm-${Date.now()}`).trim();\nconst externalConversationId = String(body.external_conversation_id || body.conversation_id || body.thread_id || phone || externalMessageId).trim();\nif (!phone || !text) {\n  throw new Error(`Normalize payload: missing phone or body (keys: ${Object.keys(body).join(',')})`);\n}\nreturn [{\n  json: {\n    tenant_slug: 'peskids',\n    provider: 'wacrm',\n    event_type: body.event_type || 'inbound_message',\n    external_conversation_id: externalConversationId,\n    external_message_id: externalMessageId,\n    phone,\n    contact_name: body.contact_name || body.name || body.pushName || 'Contacto WhatsApp',\n    body: text,\n    direction: body.direction || 'inbound',\n    timestamp: body.timestamp || new Date().toISOString(),\n    metadata: body.metadata || {}\n  }\n}];"
      },
      "id": "Normalize",
      "name": "Normalize payload",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.PESKIDS_APP_URL || 'https://peskids.op-sly.com'}}/api/webhooks/wacrm",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-wacrm-webhook-secret",
              "value": "={{$env.WACRM_PESKIDS_WEBHOOK_SECRET}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json }}"
      },
      "id": "HttpRequest",
      "name": "POST Peskids wacrm webhook",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        0
      ]
    }
  ],
  "connections": {
    "wacrm inbound": {
      "main": [
        [
          {
            "node": "Normalize payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize payload": {
      "main": [
        [
          {
            "node": "POST Peskids wacrm webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": false
  }
}