{
  "nodes": [
    {
      "id": "0bc65b77-b74c-4d4a-8b70-36b0c7aacbac",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        16,
        -256
      ],
      "parameters": {
        "color": 3,
        "width": 440,
        "height": 792,
        "content": "### Route incoming emails to Slack channels by intent using OpenAI\n\nIf your team is drowning in a shared inbox and someone has to manually read every email to figure out where it should go, this takes that job off your plate. Emails come in, AI reads them, and the right Slack channel gets notified.\n\n### How it works\n1. Gmail checks for new emails every minute\n2. Each email gets sent to OpenAI with a prompt that classifies it as sales, support, billing, or spam\n3. The AI also writes a one-line summary and assigns a priority (low/medium/high)\n4. A Switch node routes the message to the right Slack channel. Spam gets quietly dropped.\n5. Every email is logged to a Google Sheet so you have a full record of what came in and how it was categorized\n\n### Setup\n1. Connect your Gmail, OpenAI, Slack, and Google Sheets credentials in n8n\n2. Create a Google Sheet with columns: Timestamp, From, Subject, Category, Priority, Summary\n3. Open \"Configure Settings\" and add your Slack channel IDs and Sheet ID\n4. Activate the workflow and let it run\n\n### Customization\n- Want more categories? Edit the system prompt in the \"Classify Email Intent\" node and add a new output to the Switch node\n- Change how often it checks by editing the Gmail trigger (every 5 minutes instead of every minute, for example)\n- Swap gpt-4o-mini for a different model if you need more accuracy"
      },
      "typeVersion": 1
    },
    {
      "id": "4692fa09-93d5-4af3-9143-49f116f4b2ea",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        480,
        -160
      ],
      "parameters": {
        "width": 280,
        "content": "## 1. New email comes in\nGmail trigger polls every minute. You can change this to whatever interval you want."
      },
      "typeVersion": 1
    },
    {
      "id": "9bfec79e-5603-4600-b479-00ca907ffb55",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        688,
        224
      ],
      "parameters": {
        "width": 300,
        "content": "## 2. Settings and extraction\nPulls out the sender, subject, and body. Also holds your Slack channel IDs and Sheet ID."
      },
      "typeVersion": 1
    },
    {
      "id": "57281ada-b74b-4673-97ec-666737a84304",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        944,
        -160
      ],
      "parameters": {
        "width": 320,
        "content": "## 3. AI classifies the email\nOpenAI reads the email and decides: is this sales, support, billing, or spam? Also gives you a summary and priority level."
      },
      "typeVersion": 1
    },
    {
      "id": "1185463b-07c2-4682-829d-903137a39d81",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1296,
        -160
      ],
      "parameters": {
        "width": 968,
        "height": 716,
        "content": "## 4. Route to the right channel\nSales emails go to #sales, support goes to #support, billing goes to #billing. Spam gets dropped entirely."
      },
      "typeVersion": 1
    },
    {
      "id": "cb4ffded-6fd1-4b20-aac5-03b6d95a6eba",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2288,
        -64
      ],
      "parameters": {
        "width": 300,
        "content": "## 5. Log everything\nEvery email gets recorded to Google Sheets with the AI's classification. Handy for reporting and catching anything the AI gets wrong."
      },
      "typeVersion": 1
    },
    {
      "id": "b9147c92-f84f-4ba0-9b81-43dba3018676",
      "name": "Watch for New Emails",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        544,
        32
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8adb6be0-5c32-4e2b-8ba8-7b42158f5069",
      "name": "Configure Settings",
      "type": "n8n-nodes-base.set",
      "position": [
        768,
        32
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "config-1",
              "name": "slackChannel_sales",
              "type": "string",
              "value": "YOUR_SALES_CHANNEL_ID"
            },
            {
              "id": "config-2",
              "name": "slackChannel_support",
              "type": "string",
              "value": "YOUR_SUPPORT_CHANNEL_ID"
            },
            {
              "id": "config-3",
              "name": "slackChannel_billing",
              "type": "string",
              "value": "YOUR_BILLING_CHANNEL_ID"
            },
            {
              "id": "config-4",
              "name": "googleSheetId",
              "type": "string",
              "value": "YOUR_GOOGLE_SHEET_ID"
            },
            {
              "id": "config-5",
              "name": "emailFrom",
              "type": "string",
              "value": "={{ $json.from?.emailAddress || $json.from }}"
            },
            {
              "id": "config-6",
              "name": "emailSubject",
              "type": "string",
              "value": "={{ $json.subject }}"
            },
            {
              "id": "config-7",
              "name": "emailBody",
              "type": "string",
              "value": "={{ $json.snippet || $json.text || '' }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "f4b994e7-4c0b-479a-8110-2678cdcde227",
      "name": "Classify Email Intent",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        992,
        32
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "GPT-4O-MINI"
        },
        "options": {
          "maxTokens": 150,
          "temperature": 0.1
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are an email classifier. Classify the following email into exactly one category: sales, support, billing, or spam.\n\nRules:\n- sales: inquiries about pricing, services, partnerships, new business\n- support: help requests, bug reports, technical issues, feature questions\n- billing: invoices, payments, refunds, subscription changes\n- spam: promotional, unsolicited, irrelevant\n\nRespond with a JSON object only. No other text.\n{\"category\": \"<category>\", \"summary\": \"<one sentence summary of the email>\", \"priority\": \"<low|medium|high>\"}"
            },
            {
              "content": "=From: {{ $json.emailFrom }}\nSubject: {{ $json.emailSubject }}\nBody: {{ $json.emailBody }}"
            }
          ]
        },
        "jsonOutput": true
      },
      "typeVersion": 1.8
    },
    {
      "id": "d39a73d8-fcf4-454d-993c-beb8667a8cb6",
      "name": "Parse AI Response",
      "type": "n8n-nodes-base.code",
      "position": [
        1344,
        32
      ],
      "parameters": {
        "jsCode": "const config = $('Configure Settings').first().json;\nconst aiResponse = $input.first().json;\n\nlet parsed;\ntry {\n  if (aiResponse.category) {\n    parsed = aiResponse;\n  } else {\n    const content = aiResponse.text || aiResponse.message?.content || aiResponse.choices?.[0]?.message?.content || JSON.stringify(aiResponse);\n    parsed = typeof content === 'string' ? JSON.parse(content) : content;\n  }\n} catch (e) {\n  parsed = { category: 'support', summary: 'Could not classify', priority: 'medium' };\n}\n\nreturn [{\n  json: {\n    ...config,\n    category: parsed.category || 'support',\n    summary: parsed.summary || 'No summary available',\n    priority: parsed.priority || 'medium',\n    timestamp: new Date().toISOString()\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "32d9dee1-fe9d-4c09-862c-b31714fae5f5",
      "name": "Route by Category",
      "type": "n8n-nodes-base.switch",
      "position": [
        1552,
        208
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "sales",
              "conditions": {
                "options": {
                  "caseSensitive": false
                },
                "combinator": "and",
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.category }}",
                    "rightValue": "sales"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "support",
              "conditions": {
                "options": {
                  "caseSensitive": false
                },
                "combinator": "and",
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.category }}",
                    "rightValue": "support"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "billing",
              "conditions": {
                "options": {
                  "caseSensitive": false
                },
                "combinator": "and",
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.category }}",
                    "rightValue": "billing"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "typeVersion": 3.2
    },
    {
      "id": "bdfe3556-6aa3-448c-82be-e5d0205f7e6b",
      "name": "Send to Sales Channel",
      "type": "n8n-nodes-base.slack",
      "position": [
        1824,
        64
      ],
      "parameters": {
        "text": "=:envelope: *New Sales Inquiry* ({{ $json.priority }} priority)\n*From:* {{ $json.emailFrom }}\n*Subject:* {{ $json.emailSubject }}\n*Summary:* {{ $json.summary }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#general"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "typeVersion": 2.4
    },
    {
      "id": "9942f989-3390-4414-9323-d66a18ef9903",
      "name": "Send to Support Channel",
      "type": "n8n-nodes-base.slack",
      "position": [
        1824,
        224
      ],
      "parameters": {
        "text": "=:wrench: *New Support Request* ({{ $json.priority }} priority)\n*From:* {{ $json.emailFrom }}\n*Subject:* {{ $json.emailSubject }}\n*Summary:* {{ $json.summary }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#general"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "typeVersion": 2.4
    },
    {
      "id": "7744412e-a6ee-4b45-b0ee-5d7de4054f43",
      "name": "Send to Billing Channel",
      "type": "n8n-nodes-base.slack",
      "position": [
        1824,
        384
      ],
      "parameters": {
        "text": "=:receipt: *New Billing Email* ({{ $json.priority }} priority)\n*From:* {{ $json.emailFrom }}\n*Subject:* {{ $json.emailSubject }}\n*Summary:* {{ $json.summary }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#general"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "typeVersion": 2.4
    },
    {
      "id": "dbf11939-86b5-4ec0-b5b3-d9c8b09d9a1b",
      "name": "Log to Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        2048,
        -48
      ],
      "parameters": {
        "columns": {
          "value": {
            "From": "={{ $json.emailFrom }}",
            "Subject": "={{ $json.emailSubject }}",
            "Summary": "={{ $json.summary }}",
            "Category": "={{ $json.category }}",
            "Priority": "={{ $json.priority }}",
            "Timestamp": "={{ $json.timestamp }}"
          },
          "mappingMode": "defineBelow"
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Configure Settings').first().json.googleSheetId }}"
        }
      },
      "typeVersion": 4.5
    }
  ],
  "connections": {
    "Parse AI Response": {
      "main": [
        [
          {
            "node": "Route by Category",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log to Google Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by Category": {
      "main": [
        [
          {
            "node": "Send to Sales Channel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send to Support Channel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send to Billing Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Configure Settings": {
      "main": [
        [
          {
            "node": "Classify Email Intent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Watch for New Emails": {
      "main": [
        [
          {
            "node": "Configure Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify Email Intent": {
      "main": [
        [
          {
            "node": "Parse AI Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}