#!/usr/bin/env python3 import json import uuid import psycopg2 WORKFLOW_ID = "fa489bed-a0ac-4856-a38e-e78bed2adf9d" WEBHOOK_ID = "whatsapp-messages" PROJECT_ID = "NHIYHwVpHURSGRFV" nodes = [ { "parameters": {"httpMethod": "POST", "path": "whatsapp-messages", "responseMode": "lastNode", "options": {}}, "id": "wh1", "name": "Webhook WhatsApp", "type": "n8n-nodes-base.webhook", "typeVersion": 2, "position": [250, 300], "webhookId": WEBHOOK_ID }, { "parameters": {"jsCode": "const d=$input.item.json;const e=d.event||'';const m=d.data||{};const t=m.hasMedia?'media':m.body?'text':e.includes('session')?'session':'other';return[{json:{...d,messageType:t}}];"}, "id": "cl1", "name": "Classificar Mensagem", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [500, 300] }, { "parameters": {"jsCode": "console.log(JSON.stringify($input.item.json));return $input.all();"}, "id": "db1", "name": "Log Mensagem", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [750, 300] }, { "parameters": {"jsCode": "return[{json:{received:true,timestamp:new Date().toISOString()}}];"}, "id": "ok1", "name": "Responder 200", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [1000, 300] } ] connections = { "Webhook WhatsApp": {"main": [[{"node": "Classificar Mensagem", "type": "main", "index": 0}]]}, "Classificar Mensagem": {"main": [[{"node": "Log Mensagem", "type": "main", "index": 0}]]}, "Log Mensagem": {"main": [[{"node": "Responder 200", "type": "main", "index": 0}]]} } settings = {"executionOrder": "v1", "saveManualExecutions": True, "callerPolicy": "workflowsFromSameOwner"} conn = psycopg2.connect( host="m5_postgres", dbname="n8n", user="m5user", password="YXnJMMqAZ5uZrFj2lMo_AnhEmQq4uVoCfOuXaG7pU0Y", port=5432 ) cur = conn.cursor() # Insert workflow cur.execute(""" INSERT INTO workflow_entity ( name, active, nodes, connections, settings, staticData, pinData, versionId, triggerCount, id, meta, parentFolderId, isArchived, versionCounter, description, activeVersionId, createdAt, updatedAt ) VALUES ( %s, %s, %s, %s, %s, NULL, NULL, %s, %s, %s, NULL, NULL, %s, %s, %s, NULL, NOW(), NOW() ) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name, active = EXCLUDED.active, nodes = EXCLUDED.nodes, connections = EXCLUDED.connections, settings = EXCLUDED.settings, updatedAt = NOW() """, ( "WhatsApp Messages - OpenWA", True, json.dumps(nodes), json.dumps(connections), json.dumps(settings), "1", 1, WORKFLOW_ID, False, 1, "Recebe webhooks do OpenWA e salva no banco" )) # Insert webhook entity wh_entity_id = str(uuid.uuid4()) cur.execute(""" INSERT INTO webhook_entity ( id, webhookId, workflowId, method, path, node, conditions, position, retries, timeout, keywords, redactUrl, redactHeaders, redactBody, createdAt ) VALUES ( %s, %s, %s, %s, %s, %s, NULL, %s, 3, NULL, NULL, false, NULL, false, NOW() ) ON CONFLICT (webhookId) DO UPDATE SET workflowId = EXCLUDED.workflowId, updatedAt = NOW() """, ( wh_entity_id, WEBHOOK_ID, WORKFLOW_ID, "POST", "/webhook/whatsapp-messages", "Webhook WhatsApp", json.dumps([250, 300]) )) # Insert shared_workflow cur.execute(""" INSERT INTO shared_workflow (workflowId, projectId, role, createdAt, updatedAt) VALUES (%s, %s, %s, NOW(), NOW()) ON CONFLICT (workflowId, projectId) DO NOTHING """, (WORKFLOW_ID, PROJECT_ID, "workflow")) conn.commit() cur.close() conn.close() print(f"Workflow criado: {WORKFLOW_ID}") print(f"Webhook ID: {WEBHOOK_ID}") print("Pronto!")