{
  "name": "AI Inbox Triage and Filing",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 2,
      "position": [
        -340,
        -240
      ],
      "id": "e58aa6a7-dd8b-451e-9a14-b0366ba47096",
      "name": "Email Trigger (IMAP)"
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o",
          "mode": "list",
          "cachedResultName": "GPT-4O"
        },
        "messages": {
          "values": [
            {
              "content": "You are my helpful, intelligent administrative assistant.",
              "role": "system"
            },
            {
              "content": "=Your task is to categorize incoming emails into the following labels:\n\nSponsorship Requests\nPeople Selling Me Stuff\nInvoices & Receipts\nWorthwhile\n\nReturn your answer in JSON as follows:\n\n{\"category\":\"category goes here\"}\n\nRules:\n- I am a well-known AI consultant. I have dozens of people reaching out to me every day. I am looking to label and categorize emails to make my life easier, and to avoid having to rife through sponsorship requests/salespeople on a daily basis.\n- So if somebody is reaching out about a sponsorship/affiliate partnership, categorize it as Sponsorship Requests.\n- And if somebody is reaching out to try and sell me (they're pitching me something) categorize it as People Selling Me Stuff.\n- If it's a receipt for something, categorize it as \"Invoices & Receipts\"\n- If the email doesn't fit into the above three categories, categorize it as \"Worthwhile\". These are ones I will actually look at."
            },
            {
              "content": "Partnership opportunity for your channel\n\nHi there,\n\nI work with a project management app and we'd love to sponsor an upcoming video on your channel. We pay per integration and can share our rate card. Would you be open to a quick chat?\n\nThanks,\nAlex"
            },
            {
              "content": "{\"category\":\"Sponsorship Requests\"}",
              "role": "assistant"
            },
            {
              "content": "Quick question about your growth\n\nHi,\n\nMy team helps coaches sign 15 to 20 new clients a month, and you only pay on results. Do you have 15 minutes this week for a call?\n\nCheers,\nJamie"
            },
            {
              "content": "{\"category\":\"People Selling Me Stuff\"}",
              "role": "assistant"
            },
            {
              "content": "Thank you\n\nHi,\n\nI've been working through your free tutorials and finally automated my invoicing last week. It's saved me hours. I'd love to work with you on the next step for my business when you have capacity.\n\nBest,\nChris"
            },
            {
              "content": "{\"category\":\"Worthwhile\"}",
              "role": "assistant"
            },
            {
              "content": "=subject: {{ $json.subject }}\n\ntext: {{ $json.textPlain }}"
            }
          ]
        },
        "jsonOutput": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        -120,
        -240
      ],
      "id": "b8109a66-fb6b-4093-be6b-4e045ec653bc",
      "name": "Categorize Email"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "fe013229-2567-4904-b321-fce8844aec93",
              "leftValue": "={{ $json.message.content.category }}",
              "rightValue": "Worthwhile",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.2,
      "position": [
        260,
        -240
      ],
      "id": "e741f442-da65-4ecc-ad3a-69f3302242af",
      "name": "Not Worthwhile"
    },
    {
      "parameters": {
        "operation": "getAll",
        "filters": {
          "q": "=subject:\"{{ $('Email Trigger (IMAP)').item.json.subject }}\" ",
          "sender": "={{ $('Email Trigger (IMAP)').item.json.from }}"
        }
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        480,
        -240
      ],
      "id": "21c36463-053d-4ffd-97a0-71e4aed9c876",
      "name": "Get Email",
      "webhookId": "effb8c4c-38a1-43e9-8878-0226f5fa6002"
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $json.id }}",
        "labelIds": "={{ $if(\n    $('Categorize Email').item.json.message.content.category = \"People Selling Me Stuff\",\n    \"YOUR_GMAIL_LABEL_ID\",\n    $if(\n        $('Categorize Email').item.json.message.content.category = \"Sponsorship Requests\",\n        \"YOUR_GMAIL_LABEL_ID\",\n        \"YOUR_GMAIL_LABEL_ID\"\n    )\n) }}"
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        920,
        -240
      ],
      "id": "fbf01656-9411-4888-adb4-624df3127b3a",
      "name": "Add to folder",
      "webhookId": "f346cce7-0688-4b25-be2a-d50735d83a5d"
    },
    {
      "parameters": {
        "operation": "removeLabels",
        "messageId": "={{ $json.id }}",
        "labelIds": [
          "INBOX"
        ]
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        700,
        -240
      ],
      "id": "bda28220-d883-4cae-a754-8ff4c18b42ac",
      "name": "Remove from Inbox",
      "webhookId": "fd0b7780-9849-4ee0-9be3-0c9ae5a17419"
    }
  ],
  "connections": {
    "Email Trigger (IMAP)": {
      "main": [
        [
          {
            "node": "Categorize Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Categorize Email": {
      "main": [
        [
          {
            "node": "Not Worthwhile",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Not Worthwhile": {
      "main": [
        [
          {
            "node": "Get Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Email": {
      "main": [
        [
          {
            "node": "Remove from Inbox",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add to folder": {
      "main": [
        []
      ]
    },
    "Remove from Inbox": {
      "main": [
        [
          {
            "node": "Add to folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "active": false
}
