{
  "id": "MrpwcvtbOqYeyXZf",
  "meta": {
    "builderVariant": "mcp",
    "aiBuilderAssisted": true
  },
  "name": "Triage Your Gmail Inbox into a Prioritized Daily Slack Briefing with Groq Llama 3.3",
  "tags": [],
  "nodes": [
    {
      "id": "f45a19e9-aab9-475f-ac58-e03c8c5d571f",
      "name": "Run Daily at 8 AM",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        0,
        0
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "triggerAtHour": 8
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "044d7e46-ac27-4027-b27a-7743eb573c69",
      "name": "Fetch Last 24h of Emails",
      "type": "n8n-nodes-base.gmail",
      "maxTries": 3,
      "position": [
        272,
        80
      ],
      "parameters": {
        "simple": false,
        "filters": {
          "q": "newer_than:1d category:primary"
        },
        "options": {
          "downloadAttachments": false
        },
        "operation": "getAll",
        "returnAll": true
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 2.2,
      "alwaysOutputData": true,
      "waitBetweenTries": 3000
    },
    {
      "id": "ec33a3d3-4eb8-477c-bf27-adec3248e761",
      "name": "Any New Emails?",
      "type": "n8n-nodes-base.if",
      "position": [
        528,
        80
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "f8399ac2-270f-4778-94c2-ff3b3973d1ef",
      "name": "Combine Emails Into Digest Input",
      "type": "n8n-nodes-base.code",
      "position": [
        784,
        64
      ],
      "parameters": {
        "jsCode": "const items = $input.all().filter(i => i.json && i.json.id);\nconst emails = items.map((it, idx) => {\n  const j = it.json;\n  const from = (j.from && (j.from.value?.[0]?.address || j.from.text)) || j.From || j.from || 'unknown';\n  const subject = j.subject || j.Subject || '(no subject)';\n  const raw = (j.text || j.textHtml || j.html || j.snippet || '').toString();\n  const body = raw.replace(/<[^>]+>/g, ' ').replace(/\\s+/g, ' ').trim().slice(0, 1500);\n  return { index: idx + 1, sender: from, subject, body };\n});\nlet digest = emails.map(e => `#${e.index} | From: ${e.sender} | Subject: ${e.subject}\\n${e.body}`).join('\\n\\n---\\n\\n');\nif (digest.length > 30000) digest = digest.slice(0, 30000);\nreturn [{ json: { email_count: emails.length, digest_text: digest } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "76bfad85-0aed-4cc0-b163-3be4292c7b6e",
      "name": "Triage Inbox with AI",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "maxTries": 2,
      "position": [
        1024,
        64
      ],
      "parameters": {
        "text": "=You are an executive assistant. Below are emails from the last 24 hours, each marked with #index.\nFor EACH email: classify it, score urgency, summarise it in one sentence, and extract any action items and deadlines.\nThen write a one-paragraph TL;DR of the whole inbox.\n\nCategory must be one of: \"Urgent\", \"Action needed\", \"FYI\", \"Newsletter\".\nurgency is an integer 1-5 (5 = needs a response today).\nIf there are no action items, use an empty array. If no deadline, use an empty string.\n\nEmails:\n{{ $json.digest_text }}",
        "batching": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "retryOnFail": true,
      "typeVersion": 1.9,
      "waitBetweenTries": 5000
    },
    {
      "id": "3c5f6942-2b8e-4c13-8fc7-aa8dae350f31",
      "name": "Groq Llama 3.3 Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "position": [
        1024,
        304
      ],
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {
          "temperature": 0.2
        }
      },
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a6185619-7390-443a-b556-0fe5e2cac62a",
      "name": "Parse Triage JSON",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1200,
        304
      ],
      "parameters": {
        "jsonSchemaExample": "{\n  \"tldr\": \"Two emails need a reply today; the rest are informational.\",\n  \"urgent_count\": 1,\n  \"items\": [\n    { \"index\": 1, \"sender\": \"user@example.com\", \"subject\": \"Contract review\", \"category\": \"Urgent\", \"urgency\": 5, \"summary\": \"Needs your signature before end of day.\", \"action_items\": [\"Sign and return the contract\"], \"deadline\": \"today 5pm\" }\n  ]\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "2d9b78c9-6a11-48a7-932c-0d2cc60d8621",
      "name": "Post Inbox Zero to Slack",
      "type": "n8n-nodes-base.slack",
      "maxTries": 3,
      "position": [
        528,
        304
      ],
      "parameters": {
        "text": "\ud83d\udced Inbox zero \u2014 no new emails in the last 24 hours. Enjoy your morning!",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 2.5,
      "waitBetweenTries": 2000
    },
    {
      "id": "25034e19-8115-4206-8526-2a0e01e77be0",
      "name": "Test Manually",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        0,
        192
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "9ef9198a-a794-4d23-8db8-c6bd6dbc2bb2",
      "name": "Any Urgent Emails?",
      "type": "n8n-nodes-base.if",
      "position": [
        1392,
        -144
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 1,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $json.output.urgent_count }}",
              "rightValue": 0
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "5001157c-79f7-4163-afb7-59bdd87a6266",
      "name": "Build Urgent Alert",
      "type": "n8n-nodes-base.code",
      "position": [
        1600,
        -144
      ],
      "parameters": {
        "jsCode": "const o = $input.first().json.output || $input.first().json;\nconst urgent = (o.items || []).filter(i => i.category === 'Urgent' || Number(i.urgency) >= 4);\nconst lines = urgent.map(i => {\n  const ai = (i.action_items && i.action_items.length) ? `\\n   \\u27a4 ${i.action_items.join('; ')}` : '';\n  const dl = i.deadline ? ` (due ${i.deadline})` : '';\n  return `\\u2022 *${i.subject}* \\u2014 ${i.sender}${dl}\\n   ${i.summary}${ai}`;\n});\nconst text = `\\ud83d\\udea8 *${urgent.length} urgent email${urgent.length === 1 ? '' : 's'} need your attention*\\n\\n` + lines.join('\\n\\n');\nreturn [{ json: { urgent_text: text, urgent_count: urgent.length } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "dfc27d7b-5f42-4852-b4a8-e5c64cf758d9",
      "name": "Post Urgent Alert to Slack",
      "type": "n8n-nodes-base.slack",
      "maxTries": 3,
      "position": [
        1824,
        -144
      ],
      "parameters": {
        "text": "={{ $json.urgent_text }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 2.5,
      "waitBetweenTries": 2000
    },
    {
      "id": "8f6bdaf0-55cc-4efe-b409-6e835a562a2f",
      "name": "Build Slack Block Kit Digest",
      "type": "n8n-nodes-base.code",
      "position": [
        1392,
        80
      ],
      "parameters": {
        "jsCode": "const o = $input.first().json.output || $input.first().json;\nconst items = o.items || [];\nconst cats = ['Urgent', 'Action needed', 'FYI', 'Newsletter'];\nconst blocks = [];\nblocks.push({ type: 'header', text: { type: 'plain_text', text: '\\ud83d\\udcec Daily Inbox Briefing', emoji: true } });\nblocks.push({ type: 'section', text: { type: 'mrkdwn', text: `*TL;DR:* ${o.tldr || ''}\\n*${items.length}* emails \\u00b7 *${o.urgent_count || 0}* urgent` } });\nblocks.push({ type: 'divider' });\nfor (const c of cats) {\n  const group = items.filter(i => i.category === c);\n  if (!group.length) continue;\n  blocks.push({ type: 'section', text: { type: 'mrkdwn', text: `*${c}* (${group.length})` } });\n  for (const i of group) {\n    const ai = (i.action_items && i.action_items.length) ? `\\n   \\u27a4 ${i.action_items.join('; ')}` : '';\n    const dl = i.deadline ? ` _(due ${i.deadline})_` : '';\n    blocks.push({ type: 'section', text: { type: 'mrkdwn', text: `\\u2022 *${i.subject}* \\u2014 ${i.sender}${dl}\\n   ${i.summary}${ai}` } });\n  }\n  blocks.push({ type: 'divider' });\n}\nreturn [{ json: { blocks_json: JSON.stringify(blocks) } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "63d168f4-7f6c-41cc-90c7-cba0a2a45768",
      "name": "Post Briefing to Slack",
      "type": "n8n-nodes-base.slack",
      "maxTries": 3,
      "position": [
        1600,
        80
      ],
      "parameters": {
        "text": "Daily inbox briefing",
        "select": "channel",
        "blocksUi": "={{ $json.blocks_json }}",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "messageType": "block",
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 2.5,
      "waitBetweenTries": 2000
    },
    {
      "id": "23ebe526-9bf6-4aea-82dc-23767f963092",
      "name": "Split Out Triaged Emails",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1392,
        304
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "output.items"
      },
      "typeVersion": 1
    },
    {
      "id": "5c3cb1f5-0ac0-497f-b637-20a7f064261a",
      "name": "Log Emails to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "maxTries": 3,
      "position": [
        1600,
        304
      ],
      "parameters": {
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "retryOnFail": true,
      "typeVersion": 4.7,
      "waitBetweenTries": 2000
    },
    {
      "id": "fe3150e1-760c-4073-8fda-42c287ff6559",
      "name": "Sticky Note 1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -848,
        -320
      ],
      "parameters": {
        "width": 760,
        "height": 560,
        "content": "## \ud83d\udcec Triage Your Gmail Inbox into a Daily Slack Briefing\n\n**Who's it for:** Busy professionals who want their inbox read, prioritised and summarised every morning \u2014 instead of scrolling through dozens of emails.\n\n**What it does:** Fetches the last 24h of Gmail (Primary), uses Groq's Llama 3.3 to triage every email (category, urgency, one-line summary, action items, deadline) with a structured output parser, then posts a grouped Slack Block Kit briefing, fires a separate alert for urgent mail, and logs every email to a Google Sheet.\n\n**How it works:**\n1. Schedule (8 AM) or Manual trigger\n2. Gmail fetches the last 24h (Primary)\n3. Empty inbox \u2192 a friendly \"inbox zero\" Slack message\n4. Otherwise emails are bundled and triaged by the AI (validated JSON)\n5. A Block Kit digest is posted, grouped by category\n6. Urgent emails trigger a separate highlighted alert\n7. Each email is appended to a Google Sheet\n\n**How to use:** Connect the 4 credentials, pick your Slack channel + Sheet, run manually once to verify, then activate."
      },
      "typeVersion": 1
    },
    {
      "id": "3ca4dce0-3a39-4e49-8336-a80ce3d5685a",
      "name": "Sticky Note 2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -848,
        256
      ],
      "parameters": {
        "color": 4,
        "width": 760,
        "height": 460,
        "content": "### \u2699\ufe0f Setup (\u22488 min)\n\n1. **Gmail** \u2014 connect a Gmail OAuth2 credential\n2. **Groq** \u2014 free API key at console.groq.com\n3. **Slack** \u2014 connect a credential and pick the channel in the three Slack nodes\n4. **Google Sheets** \u2014 connect OAuth2; create a sheet with columns:\n   `Date, Sender, Subject, Category, Urgency, Summary, Action Items, Deadline`\n\n**Customize**\n- Gmail query (e.g. `newer_than:1d is:unread`)\n- Categories / urgency rules in *Triage Inbox with AI*\n- Schedule hour + instance timezone\n\n**Error handling:** network nodes retry on fail (3\u00d7); the empty-inbox branch skips the AI call."
      },
      "typeVersion": 1
    },
    {
      "id": "06cba57f-c682-4200-88b7-91d82f33691c",
      "name": "Sticky Note 3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -64,
        -320
      ],
      "parameters": {
        "color": 7,
        "width": 760,
        "height": 844,
        "content": "### 1\ufe0f\u20e3 Fetch & gate\nPull the last 24h of Primary mail; an empty inbox is routed to a friendly Slack message instead of running the AI."
      },
      "typeVersion": 1
    },
    {
      "id": "3f279caf-67be-4e37-ab20-d94a63d59f5a",
      "name": "Sticky Note 4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        720,
        -320
      ],
      "parameters": {
        "color": 7,
        "width": 600,
        "height": 844,
        "content": "### 2\ufe0f\u20e3 AI triage (structured output)\nGroq Llama 3.3 classifies every email and returns validated JSON via the Structured Output Parser."
      },
      "typeVersion": 1
    },
    {
      "id": "12937bbe-5596-4feb-9164-6503a1c4f055",
      "name": "Sticky Note 5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1344,
        -320
      ],
      "parameters": {
        "color": 7,
        "width": 740,
        "height": 848,
        "content": "### 3\ufe0f\u20e3 Deliver: digest \u00b7 urgent alert \u00b7 log\nBlock Kit briefing to Slack, a separate alert when urgent items exist, and a per-email Google Sheets log."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "availableInMCP": true,
    "executionOrder": "v1"
  },
  "versionId": "a15ff5bb-9cf1-4368-b4d4-a8332701637a",
  "nodeGroups": [],
  "connections": {
    "Test Manually": {
      "main": [
        [
          {
            "node": "Fetch Last 24h of Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Any New Emails?": {
      "main": [
        [
          {
            "node": "Combine Emails Into Digest Input",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Post Inbox Zero to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Triage JSON": {
      "ai_outputParser": [
        [
          {
            "node": "Triage Inbox with AI",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Run Daily at 8 AM": {
      "main": [
        [
          {
            "node": "Fetch Last 24h of Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Any Urgent Emails?": {
      "main": [
        [
          {
            "node": "Build Urgent Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Urgent Alert": {
      "main": [
        [
          {
            "node": "Post Urgent Alert to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Groq Llama 3.3 Model": {
      "ai_languageModel": [
        [
          {
            "node": "Triage Inbox with AI",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Triage Inbox with AI": {
      "main": [
        [
          {
            "node": "Any Urgent Emails?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Build Slack Block Kit Digest",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split Out Triaged Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Last 24h of Emails": {
      "main": [
        [
          {
            "node": "Any New Emails?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out Triaged Emails": {
      "main": [
        [
          {
            "node": "Log Emails to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Slack Block Kit Digest": {
      "main": [
        [
          {
            "node": "Post Briefing to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Emails Into Digest Input": {
      "main": [
        [
          {
            "node": "Triage Inbox with AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}