{
  "id": "Hsrb7cNISUzgpLet6p8mn",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Auto-triage Gmail inbox and draft replies using OpenAI GPT-4o",
  "tags": [],
  "nodes": [
    {
      "id": "eac0543d-c3ea-4114-91f0-0ae63b730431",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        784,
        1168
      ],
      "parameters": {
        "width": 480,
        "height": 816,
        "content": "## Auto-triage Gmail inbox and draft replies using OpenAI GPT-4o\n\n### How it works\n\nThis workflow monitors Gmail for new inbound email, extracts key message fields, and uses OpenAI GPT-4o to classify urgency. Urgent emails are routed to a GPT-generated reply draft that is saved in Gmail and logged to Google Sheets. Non-urgent emails bypass reply generation and are logged separately in Google Sheets.\n\n### Setup steps\n\n- Connect Gmail credentials for both the trigger and draft-creation node, ensuring the account has permission to read incoming messages and create drafts.\n- Configure OpenAI credentials and set the desired GPT-4o model/prompts for urgency classification and urgent reply drafting.\n- Connect Google Sheets credentials and select the target spreadsheet/sheet for urgent and non-urgent email logs.\n- Verify the extracted email field mappings, urgency label values, and IF condition so the urgent branch matches the classifier output.\n\n### Customization\n\nAdjust the urgency classification prompt, branch condition, reply-drafting tone, and Google Sheets columns to match your team\u2019s triage criteria and reporting needs."
      },
      "typeVersion": 1
    },
    {
      "id": "917c3698-9036-4135-adf4-d0c59b2c8915",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1344,
        1264
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 320,
        "content": "## Capture email details\n\nStarts when a new Gmail message arrives and extracts the sender, subject, body, and message ID needed for downstream triage."
      },
      "typeVersion": 1
    },
    {
      "id": "703bdd9c-76b9-4a5d-8f19-08dba4f79308",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1792,
        1264
      ],
      "parameters": {
        "color": 7,
        "width": 640,
        "height": 320,
        "content": "## Classify message urgency\n\nUses OpenAI to assess the email's urgency, stores the resulting urgency label, and routes the message through an urgent/non-urgent decision branch."
      },
      "typeVersion": 1
    },
    {
      "id": "10291caf-402e-4e84-90f1-f93eed3db4b8",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2480,
        1168
      ],
      "parameters": {
        "color": 7,
        "width": 640,
        "height": 304,
        "content": "## Handle urgent emails\n\nFor urgent messages, generates a GPT-4o reply draft, saves it back to Gmail as a draft, and logs the handled urgent email in Google Sheets."
      },
      "typeVersion": 1
    },
    {
      "id": "2b064546-ca8a-4fea-b968-de1e93f9af7a",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2480,
        1504
      ],
      "parameters": {
        "color": 7,
        "height": 320,
        "content": "## Log non-urgent emails\n\nRecords non-urgent messages in Google Sheets without generating a reply draft."
      },
      "typeVersion": 1
    },
    {
      "id": "55b062e6-8d15-40f1-b6d3-96a816ef2f9d",
      "name": "When New Email Arrives",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        1392,
        1424
      ],
      "parameters": {
        "filters": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "e410fbda-d6d2-4d5a-95a5-8f3a31d54319",
      "name": "Extract Email Details",
      "type": "n8n-nodes-base.set",
      "position": [
        1616,
        1424
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "07ee36ae-f8af-4c08-ba59-87d037fbb40c",
      "name": "AI Determines Urgency",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        1840,
        1424
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "options": {},
        "messages": {
          "values": [
            {
              "content": "You are an email triage assistant. Classify the following email as either URGENT or NOT_URGENT.\n\nRules for URGENT:\n- Requires a response within 24 hours\n- From a client, boss, or important stakeholder\n- Contains words like: urgent, ASAP, deadline, critical, payment, issue, problem, invoice, blocked, failure\n\nRules for NOT_URGENT:\n- Newsletters, promotions, or marketing emails\n- General FYI messages with no action required\n- Automated notifications\n\nRespond with ONLY one word: URGENT or NOT_URGENT\n\nEmail details:\nFrom: {{ $json.sender }}\nSubject: {{ $json.subject }}\nBody: {{ $json.body }}"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a733d9dc-2d62-4a5b-93c8-a9432ec425a0",
      "name": "Assign Urgency Label",
      "type": "n8n-nodes-base.set",
      "position": [
        2064,
        1424
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "99346c6c-3a2c-4221-960a-1701b991c8e6",
      "name": "Check Email Urgency",
      "type": "n8n-nodes-base.if",
      "position": [
        2272,
        1424
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": false,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "condition-urgent",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.urgency }}",
              "rightValue": "URGENT"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "id": "229a8c7f-1522-4f44-8b74-196f946c88df",
      "name": "Draft Urgent Reply with AI",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        2528,
        1296
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "options": {},
        "messages": {
          "values": [
            {
              "content": "You are a professional email assistant. Draft a polite, concise, and professional reply to the following email.\n\nGuidelines:\n- Keep the reply under 150 words\n- Acknowledge the sender's concern\n- Express that you have received their message and will follow up shortly\n- Use a professional but warm tone\n- Do NOT make up specific details or commitments \u2014 keep it general\n- Sign off with: [Your Name]\n\nOriginal Email:\nFrom: {{ $('Extract Email Details').item.json.sender }}\nSubject: {{ $('Extract Email Details').item.json.subject }}\nBody: {{ $('Extract Email Details').item.json.body }}\n\nWrite only the email reply body. No subject line needed."
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "62e21fea-7128-4792-bd0f-5aff0898531c",
      "name": "Create Draft in Gmail",
      "type": "n8n-nodes-base.gmail",
      "position": [
        2752,
        1296
      ],
      "parameters": {
        "operation": "create"
      },
      "typeVersion": 2
    },
    {
      "id": "88037991-eaf2-4503-9a51-7d05ca8c9255",
      "name": "Log Urgent Email to Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        2960,
        1296
      ],
      "parameters": {
        "columns": {
          "value": {
            "Sender": "={{ $('Extract Email Details').item.json.sender }}",
            "Subject": "={{ $('Extract Email Details').item.json.subject }}",
            "Urgency": "URGENT",
            "Timestamp": "={{ $now.toISO() }}",
            "Draft Created": "Yes"
          },
          "schema": [
            {
              "id": "Timestamp",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Timestamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Sender",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Sender",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Subject",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Subject",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Urgency",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Urgency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Draft Created",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Draft Created",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": []
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_GOOGLE_SHEET_ID"
        }
      },
      "typeVersion": 4
    },
    {
      "id": "5aaf4a6e-5030-4076-86ff-aa617698d63a",
      "name": "Log Non-Urgent Email to Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        2528,
        1664
      ],
      "parameters": {
        "columns": {
          "value": {
            "Sender": "={{ $('Extract Email Details').item.json.sender }}",
            "Subject": "={{ $('Extract Email Details').item.json.subject }}",
            "Urgency": "NOT_URGENT",
            "Timestamp": "={{ $now.toISO() }}",
            "Draft Created": "No"
          },
          "schema": [
            {
              "id": "Timestamp",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Timestamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Sender",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Sender",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Subject",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Subject",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Urgency",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Urgency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Draft Created",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Draft Created",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": []
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_GOOGLE_SHEET_ID"
        }
      },
      "typeVersion": 4
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "",
  "connections": {
    "Check Email Urgency": {
      "main": [
        [
          {
            "node": "Draft Urgent Reply with AI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Log Non-Urgent Email to Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assign Urgency Label": {
      "main": [
        [
          {
            "node": "Check Email Urgency",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Determines Urgency": {
      "main": [
        [
          {
            "node": "Assign Urgency Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Draft in Gmail": {
      "main": [
        [
          {
            "node": "Log Urgent Email to Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Email Details": {
      "main": [
        [
          {
            "node": "AI Determines Urgency",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When New Email Arrives": {
      "main": [
        [
          {
            "node": "Extract Email Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft Urgent Reply with AI": {
      "main": [
        [
          {
            "node": "Create Draft in Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}