{
  "name": "Financial",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "70d4d45f-b4c4-4b95-97e2-5b3e25446465",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -672,
        160
      ],
      "id": "16591560-e347-4d44-a7db-ca25888f7b92",
      "name": "Webhook"
    },
    {
      "parameters": {
        "jsCode": "const output = [];\n\n$\n\nconst TARGET_GROUP_ID = \"120363407108176682@g.us\"; \nconst transactionPattern = /^(gastei|recebi)\\s*-\\s*([0-9.,]+)\\s*-\\s*([^-]+)(?:\\s*-\\s*(cr\u00e9dito))?$/i;\nconst commandPattern = /^#(saldo|ajuda|ultimos)$/i;\n\nconst payload = $input.first().json.body;\n\nif (payload.event !== \"messages.upsert\") {\n    return [{ json: { skip: true, reason: \"Only accepts messages.upsert event\", number: payload.data.key.remoteJid } }];\n}\n\nconst key = payload.data?.key;\nconst message = payload.data?.message;\n\nif (key?.fromMe === true && key?.remoteJid === TARGET_GROUP_ID) {\n    const messageText = message?.conversation || message?.extendedTextMessage?.text || \"\";\n    const cleanText = messageText.trim().toLowerCase();\n    \n    const commandMatch = cleanText.match(commandPattern);\n    if (commandMatch) {\n        return [{\n            json: {\n                ...payload,\n                triggerCommand: true,\n                command: commandMatch[1].toLowerCase(),\n                group_id: TARGET_GROUP_ID\n            }\n        }];\n    }\n\n    const transactionMatch = cleanText.match(transactionPattern);\n    if (transactionMatch) {\n        return [{\n            json: {\n                ...payload,\n                triggerCommand: false,\n                extractedData: {\n                    tipo: transactionMatch[1].toLowerCase(),\n                    valor: transactionMatch[2].trim(),\n                    descricao: transactionMatch[3].trim(),\n                    isCredito: !!transactionMatch[4]\n                }\n            }\n        }];\n    }\n    \n    return [{ json: { skip: true, reason: \"*Comando incorreto*\\n\\n*Use:*\\n`(gastei/recebi) - valor - descri\u00e7\u00e3o`\\n\\nOu envie `#ajuda`\", number: payload.data.key.remoteJid } }];\n}\n\nreturn output;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -448,
        160
      ],
      "id": "28cb926e-1787-41c7-a417-bf0a6ed64e46",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "83e18dee-232c-4e76-bc3b-c795d1364093",
              "leftValue": "={{ $json.skip }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -240,
        160
      ],
      "id": "32b0bd81-d4a5-4171-a29b-4fb2944eb8b4",
      "name": "If"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://evolution:8080/message/sendText/fabricio",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "akd\u00e7sfjpoiwrhna\u00e7sdfkljasflkhjasdfl"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"number\":\"{{ $('Code in JavaScript').item.json.number }}\",\n  \"options\":{\n    \"delay\":1200,\n    \"presence\":\"composing\"\n  },\n  \"text\": \"{{ JSON.stringify($json.reason).slice(1, -1) }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        0,
        0
      ],
      "id": "b0ed6b8e-3752-46c6-bd4c-208f5921ab11",
      "name": "HTTP Request"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://app:8000/api/transactions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "API_TOKEN",
              "value": "asdfasdfasdf"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n\"type\": \"{{ $json.extractedData.tipo }}\",\n\"amount\": \"{{ $json.extractedData.valor }}\",\n\"description\": \"{{ $json.extractedData.descricao }}\",\n\"is_credit\": {{ $json.extractedData.isCredito }},\n\"whatsapp_msg_id\": \"{{ $json.data.key.id }}\"\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        240,
        192
      ],
      "id": "686995ac-b881-4dbd-8864-821f263334ec",
      "name": "HTTP Request1"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "a4daa3a6-5b00-4c2b-b9be-44c4f603d16b",
              "leftValue": "={{ $json.triggerCommand }}",
              "rightValue": false,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -32,
        368
      ],
      "id": "7d762502-982b-40d7-adf5-2568600c40f0",
      "name": "If1"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://app:8000/api/commands",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "API_TOKEN",
              "value": "asdfasdfasdf"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n\"command\": \"{{ $json.command }}\"\n}\n",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        240,
        480
      ],
      "id": "653c455f-bde5-4463-9b73-2fe5309d74fe",
      "name": "HTTP Request3"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://evolution:8080/message/sendText/fabricio",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "akd\u00e7sfjpoiwrhna\u00e7sdfkljasflkhjasdfl"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"number\":\"{{ $('If1').item.json.data.key.remoteJid }}\",\n  \"options\":{\n    \"delay\":1200,\n    \"presence\":\"composing\"\n  },\n  \"text\": \"{{ JSON.stringify($json.text).slice(1, -1) }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        448,
        480
      ],
      "id": "1461418c-c61f-4128-acd9-fa7820fdb6c9",
      "name": "HTTP Request4"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If1": {
      "main": [
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request3": {
      "main": [
        [
          {
            "node": "HTTP Request4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "e62089e3-f659-4691-a16a-007c45c8a77c",
  "nodeGroups": [],
  "id": "sZSjFEx5ZohoZA7p",
  "tags": []
}