{
  "id": "aZaNX1XyxkN2AbBP",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Smart Email Triage & Auto-Reply Agent",
  "tags": [
    {
      "id": "2wZenwukQPbQUfP9",
      "name": "Email",
      "createdAt": "2026-05-16T07:46:05.804Z",
      "updatedAt": "2026-05-16T07:46:05.804Z"
    },
    {
      "id": "PnVycSeBp5G56OcQ",
      "name": "AI",
      "createdAt": "2026-05-16T07:46:05.730Z",
      "updatedAt": "2026-05-16T07:46:05.730Z"
    },
    {
      "id": "tIPFdZOrROXKwsDk",
      "name": "Automation",
      "createdAt": "2026-05-16T07:46:05.819Z",
      "updatedAt": "2026-05-16T07:46:05.819Z"
    }
  ],
  "nodes": [
    {
      "id": "6b9fcc7c-0d38-4ff7-8142-29a9bcc9369b",
      "name": "Gmail \u2013 New Email",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -1856,
        272
      ],
      "parameters": {
        "filters": {
          "q": "-label:AI-Processed",
          "labelIds": [
            "UNREAD"
          ]
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8b128913-9fc6-4a57-b801-299ceb99305c",
      "name": "Normalise Email",
      "type": "n8n-nodes-base.code",
      "position": [
        -1632,
        272
      ],
      "parameters": {
        "jsCode": "const email = $input.item.json;\nconst headers = email.payload?.headers || [];\nconst getHeader = name => headers.find(h => h.name.toLowerCase() === name.toLowerCase())?.value || '';\n\nconst fromRaw = getHeader('from');\nconst subject = getHeader('subject') || '(no subject)';\nconst body = email.snippet || '';\nconst messageId = email.id || '';\nconst threadId = email.threadId || '';\n\nconst nameMatch = fromRaw.match(/^\"?(.+?)\"?\\s*<(.+?)>$/);\nconst senderName = nameMatch ? nameMatch[1].trim() : fromRaw;\nconst from = nameMatch ? nameMatch[2].trim() : fromRaw;\n\nreturn [{\n  json: { messageId, from, senderName, subject, body, threadId,\n    receivedAt: email.internalDate || new Date().toISOString() }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "b12f34d5-544f-457f-8fd6-dd057e188128",
      "name": "Claude \u2013 Classify & Draft",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1408,
        272
      ],
      "parameters": {
        "url": "https://api.groq.com/openai/v1/chat/completions",
        "method": "POST",
        "options": {},
        "jsonBody": "={{ JSON.stringify({ \"model\": \"llama-3.3-70b-versatile\", \"temperature\": 0.1, \"messages\": [{ \"role\": \"user\", \"content\": \"You are an email triage AI. Respond ONLY with valid JSON, no markdown.\\n\\nEmail:\\nFrom: \" + $json.senderName + \" <\" + $json.from + \">\\nSubject: \" + $json.subject + \"\\nBody: \" + $json.body + \"\\n\\nReturn this exact JSON schema:\\n{\\\"urgency\\\":\\\"Critical|High|Medium|Low\\\",\\\"category\\\":\\\"Incident|Support|Sales|Contract|Internal|Newsletter|Spam|Other\\\",\\\"sentiment\\\":\\\"Positive|Neutral|Negative|Urgent\\\",\\\"intent\\\":\\\"To Respond|Notification|Marketing|FYI\\\",\\\"summary\\\":\\\"one sentence\\\",\\\"action\\\":\\\"max 5 words\\\",\\\"route\\\":\\\"slack-incidents|slack-sales|zendesk|reply-directly|archive\\\",\\\"slackChannel\\\":\\\"#incidents or #sales or null\\\",\\\"draft\\\":\\\"full reply or null\\\"}\" }] }) }}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "922b8177-c393-4a3d-99da-09df70d81977",
      "name": "Parse AI Response",
      "type": "n8n-nodes-base.code",
      "position": [
        -1184,
        272
      ],
      "parameters": {
        "jsCode": "const response = $input.item.json;\nconst raw = response.choices?.[0]?.message?.content || '{}';\nlet parsed;\ntry {\n  const clean = raw.replace(/```json|```/g, '').trim();\n  parsed = JSON.parse(clean);\n} catch (e) {\n  parsed = {\n    urgency: 'Medium',\n    category: 'Other',\n    sentiment: 'Neutral',\n    intent: 'To Respond',\n    summary: 'Could not parse AI response.',\n    action: 'Review manually',\n    route: 'reply-directly',\n    slackChannel: null,\n    draft: null\n  };\n}\n\nconst email = $('Normalise Email').item.json;\nreturn [{ json: { ...email, ...parsed } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "108f1550-029c-48fd-937b-649d035fb950",
      "name": "Route by Destination",
      "type": "n8n-nodes-base.switch",
      "position": [
        -960,
        224
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Slack Incidents",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "eb71fe20-2b7a-4ca9-b4ee-0a9e89d320a9",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.route }}",
                    "rightValue": "slack-incidents"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Slack Sales",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "a1b32189-145d-4228-a809-1ef50fd13977",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.route }}",
                    "rightValue": "slack-sales"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Gmail Labels",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "7183faa8-ff03-4934-977d-436e034eef57",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.route }}",
                    "rightValue": "zendesk"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Archive",
              "conditions": {
                "options": {
                  "version": 1,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "4f92275a-b760-4a08-abd8-f9a47374acbe",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.route }}",
                    "rightValue": "archive"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "1897623b-f26a-4a37-9b75-81fc7081b5b3",
      "name": "Slack \u2013 Post to #incidents",
      "type": "n8n-nodes-base.slack",
      "position": [
        -736,
        48
      ],
      "parameters": {
        "text": "=\ud83d\udea8 *{{ $json.urgency }} \u2014 {{ $json.category }}*  *From:* {{ $json.senderName }} <{{ $json.from }}> *Subject:* {{ $json.subject }} *Summary:* {{ $json.summary }} *Action:* {{ $json.action }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#incidents"
        },
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "a79899f6-13a6-414e-8048-eb4fcfb65141",
      "name": "Slack \u2013 Post to #sales",
      "type": "n8n-nodes-base.slack",
      "position": [
        -720,
        368
      ],
      "parameters": {
        "text": "=\ud83d\udcbc *{{ $json.urgency }} \u2014 {{ $json.category }}*  *From:* {{ $json.senderName }} <{{ $json.from }}> *Subject:* {{ $json.subject }} *Summary:* {{ $json.summary }} *Action:* {{ $json.action }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#sales"
        },
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "03d253ed-7ab5-44b8-80aa-8fc0fb97493b",
      "name": "Gmail \u2013 Apply Labels",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -416,
        192
      ],
      "parameters": {
        "labelIds": [
          "Label_3355745576137139509",
          "Label_8026540524616849896",
          "Label_9033218142994857404"
        ],
        "messageId": "={{ $('Normalise Email').item.json.messageId }}",
        "operation": "addLabels"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "2c7a44e0-6a19-455d-850d-024372d05fe0",
      "name": "Should Draft Reply?",
      "type": "n8n-nodes-base.if",
      "position": [
        -736,
        544
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "e7a3e290-db54-4b72-b0e7-221f1df9be37",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.draft }}",
              "rightValue": ""
            },
            {
              "id": "81372d1b-5ee9-42e0-9940-681734f69044",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              },
              "leftValue": "={{ $json.draft }}",
              "rightValue": "null"
            },
            {
              "id": "04febb21-85f7-4bb0-80a0-359cdbcca122",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $('Parse AI Response').item.json.draft }}  is not empty",
              "rightValue": "reply-directly"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "d12b3419-ce5a-42e6-aa0c-e441cf9f7071",
      "name": "Gmail \u2013 Send Draft Reply",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -416,
        368
      ],
      "parameters": {
        "sendTo": "={{ $('Gmail \u2013 New Email').item.json.From }}",
        "message": "=={{ $('Parse AI Response').item.json.draft }}",
        "options": {},
        "subject": "=={{ 'Re: ' + $('Normalise Email').item.json.subject }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "989b91cc-c695-498a-a495-652c5092328e",
      "name": "Sheets \u2013 Log Triage",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -160,
        352
      ],
      "parameters": {
        "columns": {
          "value": {
            "From": "={{ $('Parse AI Response').item.json.from }}",
            "Action": "={{ $('Parse AI Response').item.json.action }}",
            "Intent": "={{ $('Parse AI Response').item.json.intent }}",
            "Subject": "={{ $('Parse AI Response').item.json.subject }}",
            "Summary": "={{ $('Parse AI Response').item.json.summary }}",
            "Urgency": "={{ $('Parse AI Response').item.json.urgency }}",
            "Category": "={{ $('Parse AI Response').item.json.category }}",
            "Routed To": "={{ $('Parse AI Response').item.json.route }}",
            "Sentiment": "={{ $('Parse AI Response').item.json.sentiment }}",
            "Timestamp": "=={{ new Date().toISOString() }}",
            "Draft Sent": "={{ $('Parse AI Response').item.json.draft }}",
            "Message ID": "={{ $('Parse AI Response').item.json.messageId }}",
            "Sender Name": "={{ $('Parse AI Response').item.json.senderName }}"
          },
          "schema": [
            {
              "id": "Timestamp",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Timestamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "From",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "From",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Sender Name",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Sender Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Subject",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Subject",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Urgency",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Urgency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Category",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Category",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Sentiment",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Sentiment",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Intent",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Intent",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Summary",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Summary",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Action",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Action",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Routed To",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Routed To",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Draft Sent",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Draft Sent",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Message ID",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Message ID",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 236412740,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1xBhz-se1URnZQJPWQD2ad30F2-0jhUI0ALblhbrs8Xo/edit#gid=236412740",
          "cachedResultName": "Triage Log"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "1xBhz-se1URnZQJPWQD2ad30F2-0jhUI0ALblhbrs8Xo"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4
    },
    {
      "id": "71527828-0651-4808-b9ff-3cef67d20ef1",
      "name": "Is Critical?",
      "type": "n8n-nodes-base.if",
      "position": [
        48,
        368
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.urgency }}",
              "rightValue": "Critical"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "6788a143-6bf2-440a-8dc4-597e6aaf78c4",
      "name": "Gmail \u2013 Critical Alert Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        256,
        352
      ],
      "parameters": {
        "sendTo": "user@example.com",
        "message": "={{ '<h2>\ud83d\udea8 Critical Email Alert</h2><p><b>From:</b> ' + $json.senderName + ' &lt;' + $json.from + '&gt;</p><p><b>Subject:</b> ' + $json.subject + '</p><p><b>AI Summary:</b> ' + $json.summary + '</p><p><b>Action Required:</b> ' + $json.action + '</p><p><b>Category:</b> ' + $json.category + '</p><hr><p><i>This alert was auto-generated by the Email Triage Agent.</i></p>' }}",
        "options": {},
        "subject": "={{ '\ud83d\udea8 CRITICAL EMAIL: ' + $json.subject }}"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "bbc75258-7bd6-4a6c-abeb-de6f7d30ae83",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2416,
        176
      ],
      "parameters": {
        "width": 400,
        "height": 528,
        "content": "## \ud83d\udce7 Smart Email Triage & Auto-Reply Agent\n\nHOW IT WORKS\nReads Gmail every minute, classifies \nemails with Groq AI (free), routes to \nSlack, sends auto-replies and logs \neverything to Google Sheets.\n\nHOW TO SET UP\n1. Gmail OAuth2 credential\n2. Groq API key \u2192 console.groq.com\n3. Slack Bot Token \u2192 api.slack.com\n4. Google Sheets OAuth2\n5. Create #incidents and #sales channels\n6. Activate workflow\n\nCUSTOMIZATION\n- Edit routing rules in Route by Destination\n- Modify AI prompt in Claude \u2013 Classify & Draft\n- Add more Slack channels as needed\n"
      },
      "typeVersion": 1
    },
    {
      "id": "995c5191-c486-4593-9d5b-57d42553b476",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1904,
        176
      ],
      "parameters": {
        "color": "#36517D",
        "width": 880,
        "height": 272,
        "content": "   ## 1\ufe0f\u20e3 Email Ingestion & AI Processing\n"
      },
      "typeVersion": 1
    },
    {
      "id": "51506d7e-1ba3-40f0-9f92-5bc5832e12ab",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -992,
        -16
      ],
      "parameters": {
        "color": "#3A5083",
        "width": 496,
        "height": 752,
        "content": "## 2\ufe0f\u20e3 Routing"
      },
      "typeVersion": 1
    },
    {
      "id": "a16d22b9-3a0f-4145-8a91-5a67b2947356",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -464,
        128
      ],
      "parameters": {
        "color": "#4D51AC",
        "width": 928,
        "height": 448,
        "content": "## 3\ufe0f\u20e3 Actions & Logging"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "versionId": "73bbfba0-508d-40ad-962e-12c6e32725cb",
  "connections": {
    "Is Critical?": {
      "main": [
        [
          {
            "node": "Gmail \u2013 Critical Alert Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalise Email": {
      "main": [
        [
          {
            "node": "Claude \u2013 Classify & Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse AI Response": {
      "main": [
        [
          {
            "node": "Route by Destination",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail \u2013 New Email": {
      "main": [
        [
          {
            "node": "Normalise Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Should Draft Reply?": {
      "main": [
        [
          {
            "node": "Gmail \u2013 Send Draft Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Sheets \u2013 Log Triage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by Destination": {
      "main": [
        [
          {
            "node": "Slack \u2013 Post to #incidents",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Slack \u2013 Post to #sales",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Gmail \u2013 Apply Labels",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Gmail \u2013 Apply Labels",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Should Draft Reply?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sheets \u2013 Log Triage": {
      "main": [
        [
          {
            "node": "Is Critical?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail \u2013 Apply Labels": {
      "main": [
        [
          {
            "node": "Sheets \u2013 Log Triage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slack \u2013 Post to #sales": {
      "main": [
        [
          {
            "node": "Gmail \u2013 Send Draft Reply",
            "type": "main",
            "index": 0
          },
          {
            "node": "Gmail \u2013 Apply Labels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail \u2013 Send Draft Reply": {
      "main": [
        [
          {
            "node": "Sheets \u2013 Log Triage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude \u2013 Classify & Draft": {
      "main": [
        [
          {
            "node": "Parse AI Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slack \u2013 Post to #incidents": {
      "main": [
        [
          {
            "node": "Gmail \u2013 Send Draft Reply",
            "type": "main",
            "index": 0
          },
          {
            "node": "Gmail \u2013 Apply Labels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}