{
  "name": "Hermes IA - Parte 1",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "agentehermes",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        60,
        220
      ],
      "id": "0b16d610-5800-4ec9-943f-6b3db419c2bc",
      "name": "Webhook",
      "webhookId": "a4f05576-6bb9-42d1-b125-b34f1907e41e"
    },
    {
      "parameters": {
        "content": "## Une mensagens do Usuário",
        "height": 600,
        "width": 960,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1380,
        0
      ],
      "id": "79656f9c-afcc-4f23-a466-12076484a2e6",
      "name": "Sticky Note5"
    },
    {
      "parameters": {
        "operation": "set",
        "key": "={{ $json.idChat }}_timertemp",
        "value": "sim",
        "keyType": "string",
        "expire": true,
        "ttl": 1200
      },
      "id": "1b298e1a-69e7-41d1-bfcc-3d6f5d60bb12",
      "name": "Timer Parar Agente",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        780,
        120
      ],
      "credentials": {
        "redis": {
          "id": "60dJ86T12oXNlXOO",
          "name": "Redis - Hermes IA"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "8f16b1bf-1a3e-4029-8d7a-1bccb919ee43",
              "name": "idMensagem",
              "value": "={{ $json.body.data.key.id || '' }}",
              "type": "string"
            },
            {
              "id": "11800d83-ecca-4f9c-a878-a2419db0c8e9",
              "name": "idChat",
              "value": "={{ $json.body.data.key.remoteJid.split('@')[0] || '' }}",
              "type": "string"
            },
            {
              "id": "06eba1c9-cff0-4f68-b6da-6bb0092466b7",
              "name": "conteudoMensagem",
              "value": "={{ $('Webhook').item.json.body.data.message.extendedTextMessage?.text || '' }}\n{{ $('Webhook').item.json.body.data.message.imageMessage?.caption || '' }}\n{{ $('Webhook').item.json.body.data.message.conversation || '' }}\n{{ $('Webhook').item.json.body.data.message.reactionMessage.text }}",
              "type": "string"
            },
            {
              "id": "c33f9527-e661-49e5-8e5e-64f3b430928a",
              "name": "tipoConteudo",
              "value": "={{ $('Webhook').item.json.body.data.message.extendedTextMessage ? 'text' : '' }}\n{{ $('Webhook').item.json.body.data.message.conversation ? 'text' : '' }}\n{{ $('Webhook').item.json.body.data.message.audioMessage ? 'audio' : '' }}\n{{ $('Webhook').item.json.body.data.message.imageMessage ? 'image' : '' }}",
              "type": "string"
            },
            {
              "id": "b97f1af3-5361-46fc-9303-d644921231d8",
              "name": "timestamp",
              "value": "={{ $('Webhook').item.json.body.data.messageTimestamp.toDateTime('s').toISO() }}",
              "type": "string"
            },
            {
              "id": "dc3dc59c-90a3-4a45-bea2-de092c91083b",
              "name": "urlConteudo",
              "value": "={{ $('Webhook').item.json.body.data.message.audioMessage?.url || '' }}{{ $('Webhook').item.json.body.data.message.imageMessage?.url || '' }}",
              "type": "string"
            },
            {
              "id": "8b01a818-a456-476e-bace-adefe2f04eb4",
              "name": "intervencaoMensagem",
              "value": "={{ $('Webhook').item.json.body.data.key.fromMe ? 'meunumero' : 'outro' }}",
              "type": "string"
            },
            {
              "id": "b2f1f6b5-292f-4695-9e41-be200c6d7053",
              "name": "nomeInstancia",
              "value": "={{ $json.body.instance }}",
              "type": "string"
            },
            {
              "id": "572fcce5-8a26-4e8f-a48a-ef0bee569dcd",
              "name": "apikeyInstancia",
              "value": "={{ $json.body.apikey }}",
              "type": "string"
            },
            {
              "id": "e90043db-657b-461c-b040-2d6089abfbdb",
              "name": "urlServidor",
              "value": "={{ $json.body.server_url }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "f32c364f-3f65-4129-8cbe-868baf034dd2",
      "name": "Organiza Dados",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        300,
        220
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.intervencaoMensagem }}",
                    "rightValue": "meunumero",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "a8c6cd1b-3d25-4d7c-9d7d-7aca1aa019c0"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "sim"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "d7b42536-638f-4128-b51b-6aa913e9d9bc",
                    "leftValue": "={{ $json.intervencaoMensagem }}",
                    "rightValue": "outro",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "continue"
            }
          ]
        },
        "options": {}
      },
      "id": "2f4d3bf8-85d2-462a-9395-d3aeeb97d05d",
      "name": "Intervenção Humana",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        520,
        220
      ]
    },
    {
      "parameters": {
        "content": "## Intervenção Humana",
        "height": 600,
        "width": 900,
        "color": 3
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        480,
        0
      ],
      "id": "3ad9edc9-9f37-4765-a0ff-7e01aeefee80",
      "name": "Sticky Note4"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ \n  $json.mensagens.length > 7 \n    ? $('Organiza Dados').item.json.idMensagem\n    : JSON.parse($json.mensagens.first()).idMensagem\n}}",
                    "rightValue": "={{ $('Organiza Dados').item.json.idMensagem }}",
                    "operator": {
                      "type": "string",
                      "operation": "notEquals"
                    },
                    "id": "0d6e4120-5e0f-452e-a7ae-84466747d4ef"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Parar"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "1585bc24-0b58-4179-8919-0e9aabc0e35e",
                    "leftValue": "={{ JSON.parse($json.mensagens.last()).timestamp }}",
                    "rightValue": "={{ $now.minus(10.'seconds') }}",
                    "operator": {
                      "type": "dateTime",
                      "operation": "before"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": "={{ true }}",
              "outputKey": "Continuar"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra",
          "renameFallbackOutput": "Aguardar"
        }
      },
      "id": "cc090629-c7a3-4e05-800e-f1cafccf65fc",
      "name": "Decisão Mensagem",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        1700,
        200
      ],
      "notes": "Primeira regra:\n\nVerifica se a mensagem atual é uma nova mensagem, ou uma repetição de algo que já foi processado, para evitar reprocessar mensagens antigas que possam ter voltado à fila, erros, loops infinitos e ataques."
    },
    {
      "parameters": {
        "amount": 2
      },
      "id": "86650d45-57a5-4b71-b957-f69d2b4b30fc",
      "name": "Aguardar",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1940,
        380
      ],
      "webhookId": "6c8805f2-7556-4c9c-aab7-49468bb07d81"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "db5cfe0a-7f43-4a61-8b27-bfd3a95deb8d",
              "name": "MensagensUnidas",
              "value": "={{ $json.mensagens.slice().reverse().map(historico => \n    JSON.parse(historico).conteudoMensagem.trim().replace(/\\s+/g, ' ')\n  ).join('\\n') }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "bc5f8e0d-73c5-489b-bb79-8442e94db228",
      "name": "Unifica Texto",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2180,
        420
      ]
    },
    {
      "parameters": {
        "operation": "get",
        "propertyName": "bloqueado",
        "key": "={{ $json.idChat }}_timertemp",
        "options": {}
      },
      "id": "0d120005-4585-4640-b668-5064b1ac3c75",
      "name": "Verifica Status",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        780,
        340
      ],
      "credentials": {
        "redis": {
          "id": "60dJ86T12oXNlXOO",
          "name": "Redis - Hermes IA"
        }
      },
      "notes": "Consulta se existe timeout para esse chat ou não."
    },
    {
      "parameters": {
        "content": "## Organiza os Dados",
        "height": 600,
        "width": 480,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "8b0d0d6e-ea40-4f2a-9766-b9abb8e848b9",
      "name": "Sticky Note6"
    },
    {
      "parameters": {},
      "id": "79c24f42-b3ea-4b89-8585-1860b44742c0",
      "name": "Sem ação",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1940,
        40
      ]
    },
    {
      "parameters": {
        "operation": "delete",
        "key": "=colocarnumero_memoria"
      },
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        2540,
        420
      ],
      "id": "7bba2525-5648-472b-aee8-d0cf8a70102f",
      "name": "Deletar memória",
      "credentials": {
        "redis": {
          "id": "60dJ86T12oXNlXOO",
          "name": "Redis - Hermes IA"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.bloqueado }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "empty",
                      "singleValue": true
                    },
                    "id": "39cad820-8aaf-4e74-a5a6-1aaf77e11b5f"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Continue"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 1
                },
                "conditions": [
                  {
                    "id": "3ef0e01c-cc14-4663-bb4d-2905b350c3ab",
                    "leftValue": "={{ $json.bloqueado }}",
                    "rightValue": "sim",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Pare"
            }
          ]
        },
        "options": {}
      },
      "id": "1a92a56a-1393-4a10-91ba-25b255998e40",
      "name": "Intervenção Resposta",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        960,
        240
      ]
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1180,
        360
      ],
      "id": "849810b7-718a-4170-bda1-fa68120f8fbc",
      "name": "Pare"
    },
    {
      "parameters": {
        "operation": "push",
        "list": "={{ $('Organiza Dados').item.json.idChat }}_historico",
        "messageData": "={{ JSON.stringify({\n   \"conteudoMensagem\": $('Organiza Dados').item.json.conteudoMensagem,\n   \"timestamp\": $now,\n   \"idMensagem\": $('Organiza Dados').item.json.idMensagem\n}) }}"
      },
      "id": "3b73b5be-ea16-466b-b0b8-b96b54d9808b",
      "name": "Adicionar ao Histórico",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        1180,
        120
      ],
      "credentials": {
        "redis": {
          "id": "60dJ86T12oXNlXOO",
          "name": "Redis - Hermes IA"
        }
      },
      "notes": "Adiciona o idChat a um histórico."
    },
    {
      "parameters": {
        "operation": "get",
        "propertyName": "mensagens",
        "key": "={{ $('Organiza Dados').item.json.idChat.toString() }}_historico",
        "options": {}
      },
      "id": "99824b1c-1ead-4fd7-9b88-8f09a64ae718",
      "name": "Busca Histórico",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        1460,
        200
      ],
      "credentials": {
        "redis": {
          "id": "60dJ86T12oXNlXOO",
          "name": "Redis - Hermes IA"
        }
      }
    },
    {
      "parameters": {
        "content": "## Deletar Memória Redis",
        "height": 260,
        "width": 280
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        2460,
        340
      ],
      "id": "3ba4bda1-3ed4-48bb-bbfa-01c467c0e8a9",
      "name": "Sticky Note8"
    },
    {
      "parameters": {
        "operation": "delete",
        "key": "={{ $('Organiza Dados').item.json.idChat.toString() }}_historico"
      },
      "id": "53c09ebd-6b48-4d81-a5e7-b80d966fe181",
      "name": "Extrair e Limpar",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [
        1940,
        200
      ],
      "credentials": {
        "redis": {
          "id": "60dJ86T12oXNlXOO",
          "name": "Redis - Hermes IA"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Organiza Dados",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Organiza Dados": {
      "main": [
        [
          {
            "node": "Intervenção Humana",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Intervenção Humana": {
      "main": [
        [
          {
            "node": "Timer Parar Agente",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Verifica Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decisão Mensagem": {
      "main": [
        [
          {
            "node": "Sem ação",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extrair e Limpar",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Aguardar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aguardar": {
      "main": [
        [
          {
            "node": "Busca Histórico",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verifica Status": {
      "main": [
        [
          {
            "node": "Intervenção Resposta",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Intervenção Resposta": {
      "main": [
        [
          {
            "node": "Adicionar ao Histórico",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Pare",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Adicionar ao Histórico": {
      "main": [
        [
          {
            "node": "Busca Histórico",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Busca Histórico": {
      "main": [
        [
          {
            "node": "Decisão Mensagem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extrair e Limpar": {
      "main": [
        [
          {
            "node": "Unifica Texto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Unifica Texto": {
      "main": [
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "fe06de54-88a9-4abb-8013-ba7948249cda",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f5b709a51eef55fdb50451098d32d7d32a1efe5b8d3e2116811bcebec831c6be"
  },
  "id": "nA7C99sbby0qVfYX",
  "tags": [
    {
      "createdAt": "2025-04-19T10:53:42.430Z",
      "updatedAt": "2025-04-19T10:53:42.430Z",
      "id": "5KxWyRVz8oBIvLaF",
      "name": "Agente WhatsApp"
    },
    {
      "createdAt": "2025-04-19T10:53:42.432Z",
      "updatedAt": "2025-04-19T10:53:42.432Z",
      "id": "uIe2OblGvhWW6Sy7",
      "name": "IA Revolution"
    }
  ]
}