{
  "name": "AI Email Assistant: Prioritize Gmail with ChatGPT Summaries and Slack Digests",
  "nodes": [
    {
      "id": "19658d53-51a2-4566-a57e-da51db7b68d1",
      "name": "Schedule Trigger1",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -800,
        1344
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 4
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "5379bec6-cb2c-4974-ae39-f35871d797ab",
      "name": "Get many messages",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -400,
        1344
      ],
      "parameters": {
        "filters": {
          "receivedAfter": "={{ new Date(Date.now() - 4 * 60 * 60 * 1000).toISOString() }}"
        },
        "operation": "getAll",
        "returnAll": true
      },
      "executeOnce": true,
      "typeVersion": 2.1
    },
    {
      "id": "f25dadff-b33b-4be1-aac3-1c695b39ef93",
      "name": "Get many labels",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -608,
        1344
      ],
      "parameters": {
        "resource": "label",
        "returnAll": true
      },
      "typeVersion": 2.1
    },
    {
      "id": "8508ce2a-7394-4ed4-88a1-31f1e21963dd",
      "name": "Message a model1",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -208,
        1344
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "GPT-4O-MINI"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "content": "=You're an email assistant for a busy startup founder. Classify the email content into the following fields and return **only raw JSON** (no markdown):\n\n{\n  \"summary\": [Short summary of the email in 1 sentence],\n  \"urgency\": [High, Medium, Low],\n  \"category\": [Investor, Customer, Support, Spam, Other],\n  \"intent\": [\"To respond\", \"Awaiting reply\", \"FYI\", \"Notification\", \"Meeting update\", \"Marketing\"]\n}\n\nThe descriptions of the intents are as follows:\n1. To respond: Emails you need to respond to\n\n2.FYI: Emails that don't require your response, but are important\n\n3. Notification: Automated updates from tools you use or Team chats in tools like Google Docs or Microsoft Office\n\n4. Meeting update: Calendar updates from Zoom, Google Meet, etc\n\n5. Awaiting reply: Emails you're expecting a reply to\n\n6. Actioned: Email threads that have been resolved\n\n7. Marketing: Marketing or cold emails\n\nAnalyze the following email:\n\nSubject: {{ $json.Subject }}\nBody: {{ $json.snippet }}\n\nReturn only valid JSON with no extra formatting.\n"
            }
          ]
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "e24a7261-6f06-4c2e-949b-37f3b4b78347",
      "name": "If1",
      "type": "n8n-nodes-base.if",
      "position": [
        416,
        1344
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "1ec24de2-2e64-4386-aa00-62603a63aef5",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.urgency }}",
              "rightValue": "High"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "892e956a-c199-42fa-a20c-d7302f0e515b",
      "name": "Send a message2",
      "type": "n8n-nodes-base.slack",
      "position": [
        624,
        1248
      ],
      "parameters": {
        "text": "=*Siddhant, Important Email Detected*  \nFrom: {{ $('Get many messages').item.json.From }}\n*Summary:* {{ $('Calculate Intent').item.json.summary }} \n*Urgency:*  {{ $('Calculate Intent').item.json.urgency }} , *Category:* {{ $('Calculate Intent').item.json.category }}, *Intent:* {{ $('Calculate Intent').item.json.intent }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C0981ERQ2SD",
          "cachedResultName": "jarvis"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "typeVersion": 2.3
    },
    {
      "id": "bd4eb9b3-93a1-4a89-8926-892b75cda489",
      "name": "Add label to message2",
      "type": "n8n-nodes-base.gmail",
      "position": [
        624,
        1424
      ],
      "parameters": {
        "labelIds": "={{ $json.label_id }}",
        "messageId": "={{ $('Get many messages').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "aeb25e82-853f-4138-a587-354e6153bb07",
      "name": "Add label to message3",
      "type": "n8n-nodes-base.gmail",
      "position": [
        880,
        1248
      ],
      "parameters": {
        "labelIds": "={{ $('Calculate Intent').item.json.label_id }}",
        "messageId": "={{ $('Get many messages').item.json.id }}",
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "id": "601a7bea-42cb-44c2-9196-8304ef472f28",
      "name": "Send a message3",
      "type": "n8n-nodes-base.slack",
      "position": [
        464,
        1680
      ],
      "parameters": {
        "text": "=🧠 *Inbox Assistant – Daily Digest*  \n📅 *{{ $now.format(\"DD : HH:mm\") }}*  \n━━━━━━━━━━━━━━━━━━━\n\n{{ $json.text }}\n",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C0981ERQ2SD",
          "cachedResultName": "jarvis"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "typeVersion": 2.3
    },
    {
      "id": "cd80d190-f7de-4931-8b06-62cbe4957a47",
      "name": "Calculate Intent",
      "type": "n8n-nodes-base.code",
      "position": [
        176,
        1344
      ],
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "let raw = $json.message?.content || $json.message || \"\";\n\n// Remove triple backticks (```json ... ```)\nraw = raw.replace(/```json|```/g, '').trim();\n\n// Fix smart quotes and other formatting problems\nraw = raw.replace(/[“”]/g, '\"').replace(/[‘’]/g, \"'\");\n\ntry {\n  const parsed = JSON.parse(raw);\n\n\n  return {\n    summary: parsed.summary || \"No summary provided\",\n    urgency: parsed.urgency || \"Unknown\",\n    category: parsed.category || \"Unknown\",\n    intent: parsed.intent || \"Unknown\",\n    messgae_id: $('Get many messages').item.json.id,\n    label_id: $(\"Get many labels\").all().find((item) => item.json.name === `AI Agent/${parsed.intent}`).json.id\n  };\n} catch (err) {\n  return {\n    summary: \"⚠️ Failed to parse GPT output\",\n    urgency: \"Unknown\",\n    category: \"Unknown\",\n    intent: \"Unknown\",\n    error: err.message,\n    original: raw\n  };\n}\n"
      },
      "typeVersion": 2
    },
    {
      "id": "a3acbae8-087a-47c5-9eb6-90a69ccedf8e",
      "name": "Append row in sheet1",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1152,
        1248
      ],
      "parameters": {
        "columns": {
          "value": {
            "From": "={{ $('Get many messages').item.json.From }}",
            "Intent": "={{ $('Calculate Intent').item.json.intent }}",
            "Summary": "={{ $('Calculate Intent').item.json.summary }}",
            "Urgency": "={{ $('Calculate Intent').item.json.urgency }}",
            "Category": "={{ $('Calculate Intent').item.json.category }}",
            "TimeStamp": "={{ $now }}"
          },
          "schema": [
            {
              "id": "From",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "From",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Summary",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Summary",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Intent",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Intent",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Category",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Category",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "TimeStamp",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "TimeStamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Urgency",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Urgency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "id"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs/edit?usp=drivesdk",
          "cachedResultName": "N8N - Emails"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "ab2a0b67-a248-42ea-b18c-81b658aaf9fa",
      "name": "Append row in sheet2",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        896,
        1424
      ],
      "parameters": {
        "columns": {
          "value": {
            "From": "={{ $('Get many messages').item.json.From }}",
            "Intent": "={{ $('Calculate Intent').item.json.intent }}",
            "Summary": "={{ $('Calculate Intent').item.json.summary }}",
            "Urgency": "={{ $('Calculate Intent').item.json.urgency }}",
            "Category": "={{ $('Calculate Intent').item.json.category }}",
            "TimeStamp": "={{ $now }}"
          },
          "schema": [
            {
              "id": "From",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "From",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Summary",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Summary",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Intent",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Intent",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Category",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Category",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "TimeStamp",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "TimeStamp",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Urgency",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Urgency",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 215199590,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs/edit#gid=215199590",
          "cachedResultName": "Sheet2"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs/edit?usp=drivesdk",
          "cachedResultName": "N8N - Emails"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "c361b887-c8e0-4644-96df-ce6cf8b35ef2",
      "name": "Get row(s) from sheet2",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -320,
        1680
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 215199590,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs/edit#gid=215199590",
          "cachedResultName": "Sheet2"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1YYOaQw07tTObBbY0GyP3zULru6cs15MF7PPWby8pZXs/edit?usp=drivesdk",
          "cachedResultName": "N8N - Emails"
        }
      },
      "executeOnce": true,
      "typeVersion": 4.6,
      "alwaysOutputData": true
    },
    {
      "id": "a534c64b-b747-44b5-8f03-483c6408dd02",
      "name": "Filter timestamp",
      "type": "n8n-nodes-base.code",
      "position": [
        -48,
        1680
      ],
      "parameters": {
        "jsCode": "const HOURS = 0.5; // hours window\nconst now = new Date();\n\nreturn items.filter(item => {\n  const timestamp = item.json.TimeStamp || item.json.timestamp || \"\";\n  const urgency = (item.json.Urgency || item.json.urgency || \"\").toLowerCase();\n\n  if (!timestamp) return false;\n\n  const itemDate = new Date(timestamp);\n  const hoursDiff = Math.abs((now - itemDate) / 36e5); // 36e5 = 60*60*1000\n\n  console.log(hoursDiff)\n\n  return hoursDiff <= HOURS && (urgency === \"low\" || urgency === \"medium\");\n});"
      },
      "typeVersion": 2
    },
    {
      "id": "4d788dea-84fc-41e4-a7a7-880a58b68bbf",
      "name": "Daily Digest Preparation",
      "type": "n8n-nodes-base.code",
      "position": [
        240,
        1680
      ],
      "parameters": {
        "jsCode": "let text = ``;\n\nconst medium = [];\nconst low = [];\n\nitems.forEach(item => {\n  const urgency = (item.json.Urgency || \"\").toLowerCase();\n  const intent = (item.json.Intent || \"\").toLowerCase();\n  if (urgency === \"medium\") {\n    medium.push(item);\n  } else if (\n    urgency === \"low\" &&\n    intent !== \"Marketing\"\n  ) {\n    low.push(item);\n  }\n});\n\nif (medium.length === 0 && low.length === 0) {\n  text += \"_No important messages today._\";\n} else {\n  if (medium.length > 0) {\n    text += \"*Medium Urgency:*\\n\";\n    medium.forEach((item, index) => {\n      text += `*${index + 1}.* ${item.json.Summary || \"No summary\"}\\nFrom: ${item.json.From || \"Unknown\"}\\nUrgency: ${item.json.Urgency || \"Unknown\"}, Category: ${item.json.Category || \"Unknown\"}, Intent: ${item.json.Intent || \"N/A\"}\\n\\n`;\n    });\n  }\n  if (low.length > 0) {\n    text += \"*Low Urgency:*\\n\";\n    low.forEach((item, index) => {\n      text += `*${index + 1}.* ${item.json.Summary || \"No summary\"}\\nFrom: ${item.json.From || \"Unknown\"}, Intent: ${item.json.Intent || \"N/A\"}\\n\\n`;\n    });\n  }\n}\n\nreturn [{ json: { text } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "6dddd9fa-fd59-4df0-9741-fea2382bb982",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "position": [
        -560,
        1680
      ],
      "parameters": {
        "amount": 30
      },
      "typeVersion": 1.1
    },
    {
      "id": "7e0d1871-1844-4d85-b512-6cb3fec2a5c1",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1520,
        1200
      ],
      "parameters": {
        "width": 640,
        "height": 736,
        "content": "✅ Steps to Use\n\nConnect Your Accounts: Add your Gmail, Slack, Google Sheets, and OpenAI credentials inside n8n.\n\nCreate Gmail Labels (Required): Go to your Gmail settings and create labels matching the following format:\nAI Agent/To Respond, AI Agent/Awaiting Reply, AI Agent/Notification, AI Agent/Marketing, etc.\nThese are used by the workflow to auto-tag emails based on intent.\n\nCreate a Google Sheet with 2 Tabs\n• Name the sheet something like “N8N - Emails”.\n• Add two sheets/tabs inside:\n• Sheet1 → stores all processed emails\n• Sheet2 → stores only the latest batch for digest view\n• In both sheets, add these columns (first row): From | Summary | Intent | Category | TimeStamp | Urgency\n\nImport the Workflow: Upload or paste the .json file into your n8n instance. Make sure each node is linked to your active credentials.\n\nConfigure Slack Channel: In the Slack node, select the channel where you want urgent alerts and digest summaries to be posted.\n\nAdjust Schedule (Optional): Default: runs every hour. You can tweak this to suit your preference (e.g., every 30 min or 2 hours).\n\nRun a Test: Execute manually once to check:\n• Emails are getting processed\n• Labels are added correctly in Gmail\n• Slack notifications are triggered\n• Data is logged in Google Sheets\n\nTurn It On\n• Once everything looks good, activate the workflow.\n• Your inbox will now be triaged in real-time — sorted, labeled, summarized.\n"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "If1": {
      "main": [
        [
          {
            "node": "Send a message2",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Add label to message2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Get row(s) from sheet2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many labels": {
      "main": [
        [
          {
            "node": "Get many messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a message2": {
      "main": [
        [
          {
            "node": "Add label to message3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Intent": {
      "main": [
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter timestamp": {
      "main": [
        [
          {
            "node": "Daily Digest Preparation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message a model1": {
      "main": [
        [
          {
            "node": "Calculate Intent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many messages": {
      "main": [
        [
          {
            "node": "Message a model1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger1": {
      "main": [
        [
          {
            "node": "Get many labels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row in sheet2": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add label to message2": {
      "main": [
        [
          {
            "node": "Append row in sheet2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add label to message3": {
      "main": [
        [
          {
            "node": "Append row in sheet1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get row(s) from sheet2": {
      "main": [
        [
          {
            "node": "Filter timestamp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Digest Preparation": {
      "main": [
        [
          {
            "node": "Send a message3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {},
  "active": false
}
