{
  "name": "ClickUp to WhatsApp via Evolution API",
  "nodes": [
    {
      "parameters": {
        "path": "clickup-whatsapp",
        "options": {}
      },
      "id": "webhook-clickup",
      "name": "ClickUp Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.event }}",
              "operation": "equal",
              "value2": "taskStatusUpdated"
            },
            {
              "value1": "={{ $json.task_status.status }}",
              "operation": "contains",
              "value2": "aprova\u00e7\u00e3o cliente"
            }
          ]
        }
      },
      "id": "filter-status-change",
      "name": "Filter: Status Changed to Approval",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.clickup.com/api/v2/task/{{ $json.task.id }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "httpHeaderAuth": "clickup-api",
        "options": {}
      },
      "id": "get-task-details",
      "name": "Get Task Details",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Extrair e validar dados da tarefa ClickUp\nconst task = $input.first().json;\n\n// Buscar campos customizados\nfunction findCustomField(fields, names) {\n  if (!fields) return null;\n  return fields.find(field => \n    names.some(name => \n      field.name?.toLowerCase().includes(name.toLowerCase())\n    )\n  );\n}\n\n// Mapear campos\nconst clienteField = findCustomField(task.custom_fields, ['cliente', 'client']);\nconst legendaField = findCustomField(task.custom_fields, ['legenda', 'copy', 'texto']);\nconst tipoField = findCustomField(task.custom_fields, ['tipo de criativo', 'tipo criativo']);\n\n// Valida\u00e7\u00f5es\nconst errors = [];\n\n// 1. Verificar se tem arquivos\nif (!task.attachments || task.attachments.length === 0) {\n  errors.push('\u274c Nenhum arquivo anexado');\n}\n\n// 2. Verificar campo cliente\nif (!clienteField?.value) {\n  errors.push('\u274c Campo \"Cliente\" n\u00e3o preenchido');\n}\n\n// 3. Verificar campo legenda\nif (!legendaField?.value) {\n  errors.push('\u274c Campo \"Legenda\" n\u00e3o preenchido');\n}\n\n// Mapear cliente para grupo WhatsApp\nconst clienteNome = clienteField?.value;\nconst gruposWhatsApp = {\n  'Rafael Rosa Marketing Online': 'automacao clickup',\n  'Futurize': 'CLIENTE - Futurize',\n  '2TOK': 'PROJETO - 2TOK',\n  'ABS Baterias': 'ABS BATERIAS - Social',\n  'ZOQE': 'ZOQE - Marketing',\n  'Celm\u00e1quinas': 'Celm\u00e1quinas Marketing',\n  'Cromocil': 'Cromocil - Social',\n  'Esta\u00e7\u00e3o Zero Eventos': 'Esta\u00e7\u00e3o Zero'\n};\n\nconst grupoWhatsApp = gruposWhatsApp[clienteNome];\nif (!grupoWhatsApp) {\n  errors.push(`\u274c Cliente \"${clienteNome}\" n\u00e3o tem grupo WhatsApp mapeado`);\n}\n\n// Se tem erros, preparar para reverter status\nif (errors.length > 0) {\n  return [{\n    json: {\n      hasErrors: true,\n      errors: errors,\n      taskId: task.id,\n      taskName: task.name\n    }\n  }];\n}\n\n// Se n\u00e3o tem erros, preparar dados para WhatsApp\nconst arquivos = task.attachments.map(att => ({\n  nome: att.title || att.name || 'arquivo',\n  url: att.url,\n  size: att.size\n}));\n\nconst mensagem = `\ud83d\udccb *MATERIAL PARA APROVA\u00c7\u00c3O*\\n\\n` +\n  `\ud83c\udfaf *Projeto:* ${task.name}\\n\\n` +\n  `\ud83d\udcc2 *Tipo:* ${tipoField?.value || 'N\u00e3o especificado'}\\n\\n` +\n  `\ud83d\udcdd *Legenda/Copy:*\\n${legendaField.value}\\n\\n` +\n  `\ud83d\udcce *Arquivos:* ${arquivos.length} arquivo(s)\\n` +\n  arquivos.map(a => `\u2022 ${a.nome}`).join('\\n') + '\\n\\n' +\n  `\u2705 Por favor, analisem o material e retornem com aprova\u00e7\u00e3o ou ajustes.\\n\\n` +\n  `\ud83d\udcbc *Equipe Rafael Rosa Marketing*`;\n\nreturn [{\n  json: {\n    hasErrors: false,\n    taskId: task.id,\n    taskName: task.name,\n    cliente: clienteNome,\n    grupoWhatsApp: grupoWhatsApp,\n    mensagem: mensagem,\n    arquivos: arquivos,\n    legenda: legendaField.value,\n    tipo: tipoField?.value\n  }\n}];"
      },
      "id": "process-task-data",
      "name": "Process Task Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.hasErrors }}",
              "operation": "equal",
              "value2": true
            }
          ]
        }
      },
      "id": "check-errors",
      "name": "Has Errors?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1120,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.clickup.com/api/v2/task/{{ $json.taskId }}/comment",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "comment_text",
              "value": "\ud83e\udd16 **AUTOMA\u00c7\u00c3O - ERRO DE VALIDA\u00c7\u00c3O**\\n\\n{{ $json.errors.join('\\n') }}\\n\\n\u26a0\ufe0f Corrija os problemas acima e mude o status novamente para \"APROVA\u00c7\u00c3O CLIENTE\"."
            }
          ]
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "httpHeaderAuth": "clickup-api",
        "options": {}
      },
      "id": "add-error-comment",
      "name": "Add Error Comment",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1340,
        200
      ]
    },
    {
      "parameters": {
        "url": "https://api.clickup.com/api/v2/task/{{ $json.taskId }}",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "status",
              "value": "em andamento"
            }
          ]
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "httpHeaderAuth": "clickup-api",
        "options": {}
      },
      "id": "revert-status",
      "name": "Revert Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1560,
        200
      ]
    },
    {
      "parameters": {
        "url": "https://evolution-api-rafael-rosa.onrender.com/message/sendText/rafael-rosa-main",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "RafaelRosa2025_API_WhatsApp_Secure_Key"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "number",
              "value": "{{ $json.grupoWhatsApp }}"
            },
            {
              "name": "text",
              "value": "{{ $json.mensagem }}"
            }
          ]
        },
        "options": {}
      },
      "id": "send-whatsapp-message",
      "name": "Send WhatsApp Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1340,
        400
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "id": "split-files",
      "name": "Split Files",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        1560,
        400
      ]
    },
    {
      "parameters": {
        "url": "https://evolution-api-rafael-rosa.onrender.com/message/sendMedia/rafael-rosa-main",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "RafaelRosa2025_API_WhatsApp_Secure_Key"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "number",
              "value": "{{ $('Process Task Data').item.json.grupoWhatsApp }}"
            },
            {
              "name": "media",
              "value": "{{ $json.url }}"
            },
            {
              "name": "caption",
              "value": "{{ $json.nome }}"
            }
          ]
        },
        "options": {}
      },
      "id": "send-whatsapp-file",
      "name": "Send WhatsApp File",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1780,
        400
      ]
    },
    {
      "parameters": {
        "url": "https://api.clickup.com/api/v2/task/{{ $('Process Task Data').item.json.taskId }}/comment",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "comment_text",
              "value": "\ud83e\udd16 **MATERIAL ENVIADO AUTOMATICAMENTE**\\n\\n\u2705 Material enviado para o cliente via WhatsApp\\n\ud83d\udcf1 Grupo: {{ $('Process Task Data').item.json.grupoWhatsApp }}\\n\ud83d\udcc5 Data: {{ new Date().toLocaleString('pt-BR') }}\\n\ud83d\udcce Arquivos: {{ $('Process Task Data').item.json.arquivos.length }}\\n\\n\u23f0 Aguardando aprova\u00e7\u00e3o do cliente..."
            }
          ]
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "httpHeaderAuth": "clickup-api",
        "options": {}
      },
      "id": "add-success-comment",
      "name": "Add Success Comment",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        2000,
        400
      ]
    }
  ],
  "connections": {
    "ClickUp Webhook": {
      "main": [
        [
          {
            "node": "Filter: Status Changed to Approval",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter: Status Changed to Approval": {
      "main": [
        [
          {
            "node": "Get Task Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Task Details": {
      "main": [
        [
          {
            "node": "Process Task Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Task Data": {
      "main": [
        [
          {
            "node": "Has Errors?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Errors?": {
      "main": [
        [
          {
            "node": "Add Error Comment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send WhatsApp Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Error Comment": {
      "main": [
        [
          {
            "node": "Revert Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp Message": {
      "main": [
        [
          {
            "node": "Split Files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Files": {
      "main": [
        [
          {
            "node": "Send WhatsApp File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send WhatsApp File": {
      "main": [
        [
          {
            "node": "Add Success Comment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1.0.0",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "clickup-whatsapp-evolution",
  "tags": []
}