{
  "nodes": [
    {
      "parameters": {
        "promptType": "define",
        "text": "=Voc\u00ea \u00e9 um assistente de classifica\u00e7\u00e3o de e-mails integrado a um fluxo de automa\u00e7\u00e3o no n8n. Sua tarefa \u00e9 analisar o assunto, o remetente e o conte\u00fado de e-mails n\u00e3o lidos recebidos via Microsoft Outlook e classific\u00e1-los em uma das seguintes categorias:\n\n- SPAM: E-mails n\u00e3o solicitados, golpes, phishing, propagandas indesejadas.\n- NEWSLETTER: Assinaturas, promo\u00e7\u00f5es, atualiza\u00e7\u00f5es n\u00e3o urgentes de empresas ou servi\u00e7os.\n- IMPORTANTE: E-mails pessoais, relacionados ao trabalho ou que exigem a\u00e7\u00e3o urgente.\n- CONTAS: Contas a pagar, faturas, lembretes de pagamento, extratos banc\u00e1rios.\n\nLembre-se: Voc\u00ea tem como mem\u00f3ria o banco MongoDB, use ele para aperfei\u00e7oar seu trabalho (salvando e analisando dados)\n\nAnalise cuidadosamente o assunto, o remetente e o conte\u00fado para determinar a categoria mais apropriada. Se o e-mail puder se encaixar em mais de uma categoria, priorize a que melhor reflete o objetivo principal do conte\u00fado. A resposta deve estar em portugu\u00eas.\n\nRetorne o resultado no formato JSON, com:\n- \"classificacao\": o nome da categoria em MAI\u00daSCULAS (SPAM, NEWSLETTER, IMPORTANTE ou CONTAS).\n- \"justificativa\": uma explica\u00e7\u00e3o breve (m\u00e1ximo de 50 palavras) da escolha.\n\nExemplos:\n1. Assunto: \"Ganhe 1 milh\u00e3o agora!\"\n   De: \"Promo\u00e7\u00f5es Incr\u00edveis\" <promocoes@desconhecido.com>\n   Conte\u00fado: \"Clique aqui para reivindicar seu pr\u00eamio!\"\n   Resposta: {\"classificacao\": \"SPAM\", \"justificativa\": \"E-mail n\u00e3o solicitado com promessa suspeita de ganho.\"}\n\n2. Assunto: \"Reuni\u00e3o urgente amanh\u00e3\"\n   De: \"Jo\u00e3o Silva\" <joao.silva@empresa.com>\n   Conte\u00fado: \"Precisamos discutir o projeto \u00e0s 9h.\"\n   Resposta: {\"classificacao\": \"IMPORTANTE\", \"justificativa\": \"E-mail de trabalho exigindo a\u00e7\u00e3o urgente.\"}\n\nClassifique o e-mail abaixo:\n\nAssunto: {{ $json.subject }}\nDe: {{ $json.sender.emailAddress.name }} <{{ $json.sender.emailAddress.address }}>\nConte\u00fado: {{ $json.body.content }}",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.9,
      "position": [
        -1600,
        -1180
      ],
      "id": "eaa7b0f5-c338-4a0f-bdc3-c0f6291751d0",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $json.id }}",
        "databaseName": "mydb"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryMongoDbChat",
      "typeVersion": 1,
      "position": [
        -1440,
        -840
      ],
      "id": "b1574c2f-97d6-4f3d-9fb9-1c42b19f4c5e",
      "name": "MongoDB Chat Memory",
      "credentials": {
        "mongoDb": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.classificacao }}",
                    "rightValue": "IMPORTANTE",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    },
                    "id": "99ded96c-1fe4-4d7b-abd8-b42f25711388"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "=IMPORTANTE"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "e9782f3a-6e65-4cda-884d-de4607a6afdd",
                    "leftValue": "={{ $json.classificacao }}",
                    "rightValue": "NEWSLETTER",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "NEWSLETTER"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "0621279d-1515-4154-9057-1d475af614b7",
                    "leftValue": "={{ $json.classificacao }}",
                    "rightValue": "CONTAS",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "CONTAS"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "c552ec95-23d3-4eb9-a892-d4030e76d789",
                    "leftValue": "={{ $json.classificacao }}",
                    "rightValue": "=SPAM",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "SPAM"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        -920,
        -1180
      ],
      "id": "fc426d84-ba5e-4866-9444-0096cd1c8d58",
      "name": "Switch"
    },
    {
      "parameters": {
        "operation": "move",
        "messageId": {
          "__rl": true,
          "value": "= {{ $('Microsoft Outlook Trigger').item.json.id }}",
          "mode": "id"
        },
        "folderId": {
          "__rl": true,
          "value": "AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh-Fj0RpQIb1FRH-MybTAAf8Ifp_AAAA",
          "mode": "list",
          "cachedResultName": "Importante",
          "cachedResultUrl": "https://outlook.office365.com/mail/AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh%2FFj0RpQIb1FRH%2FMybTAAf8Ifp_AAAA"
        }
      },
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        -280,
        -1220
      ],
      "id": "ddf5c179-24b0-4d36-980d-b41c15439b0a",
      "name": "Mover para Importante",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "move",
        "messageId": {
          "__rl": true,
          "value": "= {{ $('Microsoft Outlook Trigger').item.json.id }}",
          "mode": "id"
        },
        "folderId": {
          "__rl": true,
          "value": "AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh-Fj0RpQIb1FRH-MybTAAf8Ifp-AAAA",
          "mode": "list",
          "cachedResultName": "Newsletter",
          "cachedResultUrl": "https://outlook.office365.com/mail/AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh%2FFj0RpQIb1FRH%2FMybTAAf8Ifp%2FAAAA"
        }
      },
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        -280,
        -1060
      ],
      "id": "52b1a202-8a7a-467f-9590-c05ab74da883",
      "name": "Mover para Newsletter",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "move",
        "messageId": {
          "__rl": true,
          "value": "= {{ $('Microsoft Outlook Trigger').item.json.id }}",
          "mode": "id"
        },
        "folderId": {
          "__rl": true,
          "value": "AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh-Fj0RpQIb1FRH-MybTAAf8IfqAAAAA",
          "mode": "list",
          "cachedResultName": "Contas",
          "cachedResultUrl": "https://outlook.office365.com/mail/AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh%2FFj0RpQIb1FRH%2FMybTAAf8IfqAAAAA"
        }
      },
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        -280,
        -900
      ],
      "id": "575985d0-9234-492a-822f-93add1ab7d11",
      "name": "Mover para Contas",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "move",
        "messageId": {
          "__rl": true,
          "value": "= {{ $('Microsoft Outlook Trigger').item.json.id }}",
          "mode": "id"
        },
        "folderId": {
          "__rl": true,
          "value": "AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh-Fj0RpQIb1FRH-MybTAAf8Ifp9AAAA",
          "mode": "list",
          "cachedResultName": "Spam_N8N",
          "cachedResultUrl": "https://outlook.office365.com/mail/AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh%2FFj0RpQIb1FRH%2FMybTAAf8Ifp9AAAA"
        }
      },
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        -280,
        -740
      ],
      "id": "63515cc2-c445-47fd-bf8d-b1f1080213d0",
      "name": "Mover para Spam",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.0-flash",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        -1620,
        -880
      ],
      "id": "c5642346-5b3e-4879-b7cc-4bff3dea0ebb",
      "name": "Google Gemini Chat Model",
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const jsonString = $input.first().json.output\nconst cleanedString = jsonString.replace(/```json\\n?|\\n?```/g, '').trim();\nconst jsonObject = JSON.parse(cleanedString);\nreturn jsonObject;\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -1160,
        -1160
      ],
      "id": "3bcddd90-f167-40b9-8ac0-4a79ac8f7963",
      "name": "Code"
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "output": "raw",
        "filters": {
          "foldersToInclude": [
            "AQMkADAwATMwMAItOWYyMgAtZTZhMi0wMAItMDAKAC4AAAMF1jQzOK9VS4xL6XS9la3xAQCwHh-Fj0RpQIb1FRH-MybTAAACAQwAAAA="
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.microsoftOutlookTrigger",
      "typeVersion": 1,
      "position": [
        -1900,
        -1180
      ],
      "id": "7766466e-051d-4827-b169-be23ffad2bfa",
      "name": "Microsoft Outlook Trigger",
      "credentials": {
        "microsoftOutlookOAuth2Api": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MongoDB Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Mover para Importante",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mover para Newsletter",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mover para Contas",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mover para Spam",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Microsoft Outlook Trigger": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": true
  }
}