{
  "name": "SUPORTE",
  "nodes": [
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "80350bc7-6ae6-4659-abeb-fb99e9331f60",
              "name": "url",
              "value": "={{ $json.body.server_url }}",
              "type": "string"
            },
            {
              "id": "e4a15ec4-1aed-492d-afda-790c38c41e52",
              "name": "INSTANCIA",
              "value": "={{ $json.body.instance }}",
              "type": "string"
            },
            {
              "id": "e4696b1d-8965-48b8-97c9-267adbdc6cf5",
              "name": "APIKEY",
              "value": "={{ $json.body.apikey }}",
              "type": "string"
            },
            {
              "id": "dc2f6409-50ab-4bfb-8bf9-5eed7b749939",
              "name": "NUMERO_LEAD",
              "value": "={{ $json.body.data.key.remoteJid }}",
              "type": "string"
            },
            {
              "id": "1cb22a0c-0865-43b9-bc75-b875195650f6",
              "name": "MENSAGEM_TIPO",
              "value": "={{ $json.body.data.messageType }}",
              "type": "string"
            },
            {
              "id": "089ded7a-f817-4691-80e4-f2282f488dd6",
              "name": "MENSAGEM",
              "value": "={{ $json.body.data.message.conversation }}",
              "type": "string"
            },
            {
              "id": "1f6e49c9-7600-427f-a093-5d41e0d4fe15",
              "name": "message_id",
              "value": "={{ $json.body.data.key.id }}",
              "type": "string"
            },
            {
              "id": "72b77371-eca1-4654-9c08-c9e881f4b64a",
              "name": "hora",
              "value": "={{ $json.body.date_time }}",
              "type": "string"
            },
            {
              "id": "2cbf8083-4e4e-4195-a4a0-91657e7742d0",
              "name": "Base64",
              "value": "={{ $json.body.data.message.base64 }}",
              "type": "string"
            },
            {
              "id": "3e6d26bc-2a50-4682-94b8-78860d98595e",
              "name": "Mensagem_ID",
              "value": "={{ $json.body.data.key.id }}",
              "type": "string"
            },
            {
              "id": "875eb6c0-f23d-4bb5-9ae0-c6bb02cd297c",
              "name": "hora",
              "value": "={{ $json.body.date_time }}",
              "type": "string"
            },
            {
              "id": "6228bbab-3861-46ed-a097-6f8b76f561d7",
              "name": "fromMe",
              "value": "={{ $json.body.data.key.fromMe }}",
              "type": "boolean"
            }
          ]
        },
        "options": {}
      },
      "id": "111db4ac-12be-4d89-83ed-f12d035a9fd1",
      "name": "Tratamento de Dados",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -5760,
        380
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "7b1fb8c5-39a9-4c9c-a6be-c59c03037a49",
              "name": "Mensagem",
              "value": "={{ $json.MENSAGEM }}{{ $('Tratamento de Dados').item.json.MENSAGEM }}{{ $json.content }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "b0114539-d340-443a-8b49-9eae316e0f7d",
      "name": "Mensagem Principal",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -3860,
        400
      ]
    },
    {
      "parameters": {
        "resource": "audio",
        "operation": "transcribe",
        "binaryPropertyName": "=data2",
        "options": {}
      },
      "id": "5b34c415-a08e-49e5-b65c-fc3fbc701c65",
      "name": "OpenAI1",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.4,
      "position": [
        -4300,
        600
      ],
      "credentials": {
        "openAiApi": {
          "id": "enKhHqNKibYUkzcO",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "aab56c59-e5ab-4a6a-a90f-52e8546a2a6a",
              "name": "MENSAGEM",
              "value": "={{ $json.text }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "bb9b3902-8327-49e3-856c-126784af08b0",
      "name": "DEFINIR MENSAGEM",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -4080,
        600
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "cac09de4-58a9-43bb-aa94-b0bd16094a57",
              "leftValue": "={{ $('Evolution API').item.json.base64 }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "9835dc3e-c8e7-4815-9c6c-1cd5d196d866",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -5180,
        600
      ]
    },
    {
      "parameters": {
        "operation": "toBinary",
        "sourceProperty": "base64",
        "binaryPropertyName": "data2",
        "options": {
          "fileName": "foto.jpeg",
          "mimeType": "image/jpeg"
        }
      },
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        -4300,
        240
      ],
      "id": "a8f26d8e-906b-4c58-93f0-52c98945b49d",
      "name": "Convert to File1"
    },
    {
      "parameters": {
        "operation": "toBinary",
        "sourceProperty": "=data",
        "binaryPropertyName": "data2",
        "options": {
          "fileName": "audio.mp3",
          "mimeType": "audio/mpeg"
        }
      },
      "id": "5a0a5567-3b06-4cac-8f7a-12841085b638",
      "name": "Convert to File",
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        -4520,
        600
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "5840679c-7016-46e5-92d3-7179c11b62a6",
              "name": "base64",
              "value": "={{ $('Tratamento de Dados').item.json.Base64 }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -4520,
        240
      ],
      "id": "202fee27-3447-497b-9a09-8fbec1371d76",
      "name": "Imagem/Base64"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "40192510-4f2a-4a6d-adf7-eaf614a321f3",
              "name": "data",
              "value": "={{ $json.base64 }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "555fa55e-0f16-494f-bc26-5dcc376d6ec8",
      "name": "BASE",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -4740,
        600
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "7ba9b235-4f0b-4e29-8adb-0cd73113e41b",
              "leftValue": "={{ $json.id }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -2880,
        300
      ],
      "id": "5cf6bccf-b83d-4c25-8430-e178c875c21d",
      "name": "If1"
    },
    {
      "parameters": {
        "url": "=https://api.openai.com/v1/threads/{{ $json.thread_id }}/messages?order=desc",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "limit",
              "value": "1"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "OpenAI-Beta",
              "value": "assistants=v2"
            }
          ]
        },
        "options": {}
      },
      "id": "289495b5-d37c-4181-a946-ae897c399eaf",
      "name": "Verifica Última Mensagem - Post Retrieve Messages",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        -2660,
        240
      ],
      "credentials": {
        "openAiApi": {
          "id": "enKhHqNKibYUkzcO",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "10529067-5a89-4ca6-9b6a-bbf057eab11a",
                    "leftValue": "={{ $json.MENSAGEM_TIPO }}",
                    "rightValue": "imageMessage",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "IMAGEM"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.MENSAGEM_TIPO }}",
                    "rightValue": "conversation",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "26b52b69-f4b9-4246-b706-f27616c62023"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "TEXTO"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "6e37cd84-587f-49b7-b2ed-78d0666c1a9e",
                    "leftValue": "={{ $json.MENSAGEM_TIPO }}",
                    "rightValue": "audioMessage",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "AUDIO"
            }
          ]
        },
        "options": {}
      },
      "id": "d7ab50e4-132e-4ab2-af98-9094269cd0bd",
      "name": "Tipo de Mensagem",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        -5400,
        400
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "27089e4f-07d2-4889-aa44-4896517d7511",
        "options": {}
      },
      "id": "10f926f9-987f-4006-8a89-8cb9529d100f",
      "name": "Evolution API",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -5960,
        380
      ],
      "webhookId": "27089e4f-07d2-4889-aa44-4896517d7511"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/threads",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "OpenAI-Beta",
              "value": "assistants=v2"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"messages\": [\n    {\n      \"role\": \"assistant\",\n      \"content\": \"Responda a mensagem do lead com uma saudacao (Uma unica vez) e se necessario tambem a pergunta que ele fez, se apresente como [nome] e pergunte como você pode ajudar, saudação ideal: Olá tudo bem? Eu sou a [nome]. Qual o seu nome?\"\n    }\n  ]\n}",
        "options": {}
      },
      "id": "5315584e-c65f-4980-be90-98b2b942cec9",
      "name": "Cria Thread - Banco de Dados",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -2660,
        560
      ],
      "credentials": {
        "openAiApi": {
          "id": "enKhHqNKibYUkzcO",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.openai.com/v1/threads/{{ $('Supabase').item.json.thread_id }}/messages",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "OpenAI-Beta",
              "value": "assistants=v2"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "role",
              "value": "user"
            },
            {
              "name": "content",
              "value": "={{ $('Mensagem Principal').item.json.Mensagem }}"
            }
          ]
        },
        "options": {}
      },
      "id": "8ee7a52d-471e-4d04-ad62-da97f91e7e18",
      "name": "Puxa Histórico de Mensagens",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -2220,
        360
      ],
      "credentials": {
        "openAiApi": {
          "id": "enKhHqNKibYUkzcO",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "method": "=POST",
        "url": "={{ $json.url }}/chat/getBase64FromMediaMessage/{{ $('Tratamento de Dados').item.json.INSTANCIA }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{ $('Tratamento de Dados').item.json.APIKEY }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"message\": {\n    \"key\": {\n      \"id\": \"{{ $json.message_id }}\"\n    }\n  },\n  \"convertToMp4\": true\n} ",
        "options": {
          "redirect": {
            "redirect": {}
          }
        }
      },
      "id": "4e6016da-4cd9-422b-b951-65229592798f",
      "name": "HTTP Request1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -4960,
        660
      ]
    },
    {
      "parameters": {
        "operation": "get",
        "tableId": "SUPORTE",
        "filters": {
          "conditions": [
            {
              "keyName": "number",
              "keyValue": "={{ $('Tipo de Mensagem').item.json.NUMERO_LEAD }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        -3100,
        380
      ],
      "id": "f0382d63-9ab6-4609-a201-56d5408d2b5e",
      "name": "Supabase",
      "alwaysOutputData": true,
      "credentials": {
        "supabaseApi": {
          "id": "j2dHliCnAbaNqRCg",
          "name": "Supabase account"
        }
      }
    },
    {
      "parameters": {
        "tableId": "SUPORTE",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "number",
              "fieldValue": "={{ $('Tratamento de Dados').item.json.NUMERO_LEAD }}"
            },
            {
              "fieldId": "thread_id",
              "fieldValue": "={{ $json.id }}"
            },
            {
              "fieldId": "created_at",
              "fieldValue": "={{ $('Tratamento de Dados').item.json.hora }}"
            },
            {
              "fieldId": "timeout",
              "fieldValue": "={{ $('Tratamento de Dados').item.json.hora }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        -2440,
        560
      ],
      "id": "37cf9770-24d4-4fd5-8435-b676272a6c40",
      "name": "Supabase1",
      "retryOnFail": true,
      "maxTries": 5,
      "waitBetweenTries": 5000,
      "credentials": {
        "supabaseApi": {
          "id": "j2dHliCnAbaNqRCg",
          "name": "Supabase account"
        }
      }
    },
    {
      "parameters": {
        "amount": 22
      },
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        -1900,
        360
      ],
      "id": "6b03e694-e90c-4918-b08a-4a7f54064d71",
      "name": "Wait1",
      "webhookId": "b3538c62-cab3-4aaa-b1e8-b1d7ea45836a"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://api.openai.com/v1/threads/{{ $('Supabase').item.json.thread_id }}/messages",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "OpenAI-Beta",
              "value": "assistants=v2"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "role",
              "value": "user"
            },
            {
              "name": "content",
              "value": "={{ $('Mensagem Principal').item.json.Mensagem }}"
            }
          ]
        },
        "options": {}
      },
      "id": "297c9679-01e7-49c7-9561-fb401848b7f1",
      "name": "CREATE A THREAD2",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -2220,
        180
      ],
      "credentials": {
        "openAiApi": {
          "id": "enKhHqNKibYUkzcO",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "94a4c81d-969a-47b1-9f11-9fe2f596c65c",
              "leftValue": "={{ $json.data[0].role }}",
              "rightValue": "assistant",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -2440,
        240
      ],
      "id": "17675013-d8a4-4875-ab2a-e08341c956f4",
      "name": "Última mensagem foi do Assistente?1"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -100,
        360
      ],
      "id": "c35effe5-734c-43a1-9cb5-2fce8fdaf43e",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "jsCode": "// Obtém todos os itens do nó \"Code\"\nconst codeNode = $('Loop Over Items').all();\n\n// Verifica se há itens no nó \"Code\"\nif (!codeNode || codeNode.length === 0) {\n  return [{ json: { erro: \"Nenhum dado encontrado no nó 'Code'.\" } }];\n}\n\n// Array para armazenar os resultados\nconst output = [];\n\n// Processa cada item do nó \"Code\"\ncodeNode.forEach(item => {\n  // Obtém o texto corretamente\n  const text = item.json.result || \"\";\n\n  // Verifica se o texto é válido\n  if (!text || text.trim() === \"\") {\n    output.push({ json: { erro: \"Texto ausente ou inválido\" } });\n    return;\n  }\n\n  // Conta o número de caracteres na mensagem\n  const characterCount = text.length;\n\n  // Calcula os milissegundos (100 caracteres = 10000 ms)\n  const milliseconds = Math.floor((characterCount / 100) * 10000);\n\n  // Adiciona o resultado ao array de saída\n  output.push({ json: { result: text, characterCount, milliseconds } });\n});\n\n// Retorna os resultados\nreturn output;\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        360
      ],
      "id": "2d7ab751-ec56-492c-9236-d00e84726a97",
      "name": "Contabilizar Caracteres"
    },
    {
      "parameters": {
        "jsCode": "// Obtém a saída do nó \"BDR\"\nconst output = $('SUPORTE').first().json?.output || '';\n\n// Remove espaços extras e conta os caracteres\nconst texto = output.trim();\nconst totalCaracteres = texto.length;\n\n// Se o texto for menor que 200 caracteres, retorna ele inteiro\nif (totalCaracteres <= 200) {\n    return [{ json: { result: texto } }];\n}\n\n// Expressão regular para encontrar os pontos de corte corretos\nconst regex = /(?<=[?.!])\\s+/g;\n\n// Divide o texto **mas não separa frases com menos de 200 caracteres**\nlet partes = [];\nlet buffer = \"\";\n\n// Percorre cada frase e agrupa até atingir o mínimo de 200 caracteres\ntexto.split(regex).forEach((frase) => {\n    if ((buffer + \" \" + frase).trim().length < 200) {\n        buffer += \" \" + frase;\n    } else {\n        partes.push(buffer.trim());\n        buffer = frase;\n    }\n});\n\n// Adiciona a última parte ao array\nif (buffer) {\n    partes.push(buffer.trim());\n}\n\n// Retorna cada bloco como um item separado para o n8n processar corretamente\nreturn partes.map(part => ({ json: { result: part } }));\n"
      },
      "id": "712d5f9f-3216-4d6e-b110-6d082932ffab",
      "name": "Code4",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -420,
        360
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://evo.agentevende.store/message/sendText/AGENTEVENDECURSO",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apiKey ",
              "value": "={{ $('Tratamento de Dados').item.json.APIKEY }}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "number",
              "value": "={{ $('Tipo de Mensagem').item.json.NUMERO_LEAD }}"
            },
            {
              "name": "text",
              "value": "={{ $('Loop Over Items').item.json.result }}"
            },
            {
              "name": "delay",
              "value": "={{ $json.milliseconds }}"
            }
          ]
        },
        "options": {
          "allowUnauthorizedCerts": true
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        520,
        360
      ],
      "id": "415d0272-7fcc-4e49-b66a-ae2821e791ba",
      "name": "Enviar Texto1"
    },
    {
      "parameters": {
        "content": "# FLUXO 1 \n## Esse Fluxo é responsável por receber toda a mensagem e entender se ela é uma mensagem de áudio, de texto, ou uma imagem.\n",
        "height": 820,
        "width": 2580
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -6020,
        60
      ],
      "typeVersion": 1,
      "id": "54715502-156b-4319-8ac7-9f7f4d5c2186",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "content": "## FLUXO 2\n### Esse fluxo é responsável pelo cadastro do lead na nossa tabela do Supabase e também pela criação da nossa Threads (Memória do Agente). É exatamente aqui que o fluxo armazena não só as mensagens que o lead manda, mas também as mensagens que o próprio agente envia; gerando mais contexto e mais assertividade no fluxo,",
        "height": 820,
        "width": 1680,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3280,
        60
      ],
      "typeVersion": 1,
      "id": "4f46d44f-17c3-415a-8ed4-186dae1fb20a",
      "name": "Sticky Note5"
    },
    {
      "parameters": {
        "content": "# AGENTE DE I.A\n\n## Responsável pelo cérebro do Agente, é aqui que ajustamos toda a parte de interação do fluxo. É importante que para mudar o prompt do seu agente você acesse o Playground da OpenAI. ",
        "height": 700,
        "width": 820,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1500,
        120
      ],
      "typeVersion": 1,
      "id": "bb148a8b-b22e-4a14-9d09-0014b8374c0d",
      "name": "Sticky Note6"
    },
    {
      "parameters": {
        "content": "# Retorno da Mensagem e envio da Mensagem por texto\n\n## É aqui que fazemos a repartição das mensagens para elas não irem grande demais; criamos o delay para dar aquele \"sentido\" digitando, e enviamos a mensagem por texto via Evolution API.",
        "height": 760,
        "width": 1360,
        "color": 2
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -580,
        0
      ],
      "typeVersion": 1,
      "id": "e26d5fef-5af4-4110-aed2-e81d35cd7301",
      "name": "Sticky Note8"
    },
    {
      "parameters": {
        "content": "# SUPORTE "
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -6380,
        420
      ],
      "typeVersion": 1,
      "id": "baaf165b-1fd4-4097-8d2e-636e76f96477",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "10b122b1-23d1-4175-afa3-2f7617e63e27",
              "leftValue": "={{ $json.fromMe }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        -5580,
        380
      ],
      "id": "17addb4e-239b-4305-ab55-0b79f0ccc51b",
      "name": "If2"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        -5400,
        220
      ],
      "id": "d1533f38-53bf-4df2-b677-3ea7ed9f821b",
      "name": "No Operation, do nothing"
    },
    {
      "parameters": {
        "resource": "image",
        "operation": "analyze",
        "modelId": {
          "__rl": true,
          "value": "chatgpt-4o-latest",
          "mode": "list",
          "cachedResultName": "CHATGPT-4O-LATEST"
        },
        "text": "Descreva exatamente o que está nesta imagem, o que ela representa, e fatores que podem ajudar a identificar o significado dela.",
        "inputType": "base64",
        "binaryPropertyName": "data2",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        -4080,
        240
      ],
      "id": "98654fd9-3e19-483d-9ad1-1630bc0163f6",
      "name": "OpenAI",
      "credentials": {
        "openAiApi": {
          "id": "enKhHqNKibYUkzcO",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1g8gqOrHTOGYEvx73gB9Tvh4J_M5jnejC8a27JLzl-rk",
          "mode": "list",
          "cachedResultName": "MODELO DE FAQ",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1g8gqOrHTOGYEvx73gB9Tvh4J_M5jnejC8a27JLzl-rk/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Página1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1g8gqOrHTOGYEvx73gB9Tvh4J_M5jnejC8a27JLzl-rk/edit#gid=0"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.5,
      "position": [
        -1080,
        600
      ],
      "id": "820e2d89-13df-4253-86be-919814f34cf5",
      "name": "Google Sheets",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "fET52qCdePXdO2Hy",
          "name": "Google Sheets account"
        }
      },
      "disabled": true
    },
    {
      "parameters": {
        "resource": "assistant",
        "assistantId": {
          "__rl": true,
          "value": "asst_37epYjmTip16GJXAHo86thkg",
          "mode": "list",
          "cachedResultName": "SUPORTE [Alunos]"
        },
        "prompt": "define",
        "text": "=Dia e Horário da Mensagem: {{ $now }}\n\n#NOTAS:\n\n-NÃO RETORNE UMA RESPOSTA EM MARKDOWN\n-NÃO RETORNE RESPOSTAS COM CARACTERES ESPECIAIS.",
        "memory": "threadId",
        "threadId": "={{ $('Supabase').item.json.thread_id }}",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.7,
      "position": [
        -1260,
        380
      ],
      "id": "99155f45-5ab5-460d-a0a8-2d4afccaf7c2",
      "name": "SUPORTE",
      "retryOnFail": true,
      "credentials": {
        "openAiApi": {
          "id": "enKhHqNKibYUkzcO",
          "name": "OpenAi account"
        }
      }
    }
  ],
  "pinData": {
    "Evolution API": [
      {
        "json": {
          "headers": {
            "host": "n8neditor.agentevende.store",
            "user-agent": "axios/1.7.7",
            "content-length": "1013",
            "accept-encoding": "gzip, compress, deflate, br",
            "content-type": "application/json",
            "x-forwarded-for": "172.18.0.1",
            "x-forwarded-host": "n8neditor.agentevende.store",
            "x-forwarded-port": "443",
            "x-forwarded-proto": "https",
            "x-forwarded-server": "6ebe9615d8b2",
            "x-real-ip": "172.18.0.1"
          },
          "params": {},
          "query": {},
          "body": {
            "event": "messages.upsert",
            "instance": "AGENTEVENDECURSO",
            "data": {
              "key": {
                "remoteJid": "5522998030398@s.whatsapp.net",
                "fromMe": false,
                "id": "3EB0B742425D46C69E589D"
              },
              "pushName": "Quézia Demetrio",
              "status": "DELIVERY_ACK",
              "message": {
                "conversation": "Olá! Tudo bem por ai? Tenho dúvidas",
                "messageContextInfo": {
                  "deviceListMetadata": {
                    "senderKeyHash": "b93UqP1AbclHCQ==",
                    "senderTimestamp": "1742513696",
                    "senderAccountType": "E2EE",
                    "receiverAccountType": "E2EE",
                    "recipientKeyHash": "YlQcGCn6o99Osw==",
                    "recipientTimestamp": "1743082893"
                  },
                  "deviceListMetadataVersion": 2,
                  "messageSecret": "JRYUnGstZ+ZhWSePGZdV/pP1Lr1uzThFT+CvWlGvVPI="
                }
              },
              "messageType": "conversation",
              "messageTimestamp": 1743082997,
              "instanceId": "125ab9aa-8898-481a-a23f-7531add15bd0",
              "source": "web"
            },
            "destination": "https://n8neditor.agentevende.store/webhook-test/68db7bec-7f2f-4948-be67-54fd27fb0770",
            "date_time": "2025-03-27T10:43:17.380Z",
            "sender": "5519933001431@s.whatsapp.net",
            "server_url": "https://evolution-evolution.q0rndc.easypanel.host",
            "apikey": "C3E4F71BDFE4-4A7A-922F-70C87D151E75"
          },
          "webhookUrl": "https://n8nwebhook.agentevende.store/webhook-test/68db7bec-7f2f-4948-be67-54fd27fb0770",
          "executionMode": "test"
        }
      }
    ]
  },
  "connections": {
    "Tratamento de Dados": {
      "main": [
        [
          {
            "node": "If2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI1": {
      "main": [
        [
          {
            "node": "DEFINIR MENSAGEM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "DEFINIR MENSAGEM": {
      "main": [
        [
          {
            "node": "Mensagem Principal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "BASE",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File": {
      "main": [
        [
          {
            "node": "OpenAI1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Imagem/Base64": {
      "main": [
        [
          {
            "node": "Convert to File1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "BASE": {
      "main": [
        [
          {
            "node": "Convert to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File1": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mensagem Principal": {
      "main": [
        [
          {
            "node": "Supabase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If1": {
      "main": [
        [
          {
            "node": "Verifica Última Mensagem - Post Retrieve Messages",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Cria Thread - Banco de Dados",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verifica Última Mensagem - Post Retrieve Messages": {
      "main": [
        [
          {
            "node": "Última mensagem foi do Assistente?1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tipo de Mensagem": {
      "main": [
        [
          {
            "node": "Imagem/Base64",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mensagem Principal",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Evolution API": {
      "main": [
        [
          {
            "node": "Tratamento de Dados",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cria Thread - Banco de Dados": {
      "main": [
        [
          {
            "node": "Supabase1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Puxa Histórico de Mensagens": {
      "main": [
        [
          {
            "node": "Wait1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request1": {
      "main": [
        [
          {
            "node": "BASE",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Supabase": {
      "main": [
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Supabase1": {
      "main": [
        [
          {
            "node": "Supabase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait1": {
      "main": [
        [
          {
            "node": "SUPORTE",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Última mensagem foi do Assistente?1": {
      "main": [
        [
          {
            "node": "CREATE A THREAD2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Puxa Histórico de Mensagens",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Contabilizar Caracteres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Contabilizar Caracteres": {
      "main": [
        [
          {
            "node": "Enviar Texto1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code4": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviar Texto1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If2": {
      "main": [
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Tipo de Mensagem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "main": [
        [
          {
            "node": "Mensagem Principal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets": {
      "ai_tool": [
        [
          {
            "node": "SUPORTE",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "SUPORTE": {
      "main": [
        [
          {
            "node": "Code4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "9618acf0-f1cc-4a10-a3cb-0a5480ece5f1",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "08f792c88d6c17a03adaef9e98c81858795697599d5d63dc53224d9c2a9c8495"
  },
  "id": "a8TLHsPIqt2qk21x",
  "tags": [
    {
      "createdAt": "2025-03-04T23:06:10.476Z",
      "updatedAt": "2025-03-04T23:06:10.476Z",
      "id": "g3dqTIVg7CbBQcra",
      "name": "AGENTEVENDE"
    },
    {
      "createdAt": "2025-03-14T21:59:07.076Z",
      "updatedAt": "2025-03-14T21:59:07.076Z",
      "id": "ceapzG9CCHRBioq7",
      "name": "VENDEDOR"
    },
    {
      "createdAt": "2025-03-14T21:59:11.651Z",
      "updatedAt": "2025-03-14T21:59:11.651Z",
      "id": "GDbFOLYPZHxoIFWM",
      "name": "AULA"
    }
  ]
}