{
  "id": "kKdyG8BnQcQ6re5y",
  "name": "Gmail-Slack",
  "tags": [],
  "nodes": [
    {
      "id": "254da1f8-b8e6-44fb-b526-fa8f4181e3fd",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -1248,
        144
      ],
      "parameters": {
        "filters": {
          "readStatus": "unread"
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "a6929e80-8db0-4c8a-83b1-345f68b5d2bc",
      "name": "Send a message",
      "type": "n8n-nodes-base.slack",
      "position": [
        0,
        0
      ],
      "parameters": {
        "text": "=New support request received from  {{ $('Gmail Trigger').item.json.From }}\nSubject: {{ $('Gmail Trigger').item.json.Subject }}\nContent: {{ $('Gmail Trigger').item.json.snippet }}\n",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C09EVCZR4P6",
          "cachedResultName": "support"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "typeVersion": 2.3
    },
    {
      "id": "5e546936-0738-4260-9d7a-cabdf09869bf",
      "name": "Message a model",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -560,
        160
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "chatgpt-4o-latest",
          "cachedResultName": "CHATGPT-4O-LATEST"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "=You are an email triage classifier. Return ONLY a JSON object:\n{\"category\":\"support|new-request|enquiry|general|spam|other\"}"
            },
            {
              "content": "=Subject:  {{ $('Gmail Trigger').item.json.Subject }}\nContent: {{ $('Gmail Trigger').item.json.snippet }}"
            }
          ]
        },
        "jsonOutput": true
      },
      "typeVersion": 1.8
    },
    {
      "id": "bcbca08b-7d32-4f62-b543-bfb4c15e260c",
      "name": "Create a row",
      "type": "n8n-nodes-base.supabase",
      "position": [
        256,
        160
      ],
      "parameters": {
        "tableId": "threads",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "thread_id",
              "fieldValue": "={{ $('Gmail Trigger').item.json.threadId }}"
            },
            {
              "fieldId": "first_message_id",
              "fieldValue": "={{ $('Gmail Trigger').item.json.id }}"
            },
            {
              "fieldId": "from_addr",
              "fieldValue": "={{ $('Gmail Trigger').item.json.From }}"
            },
            {
              "fieldId": "subject",
              "fieldValue": "={{ $('Gmail Trigger').item.json.Subject }}"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "a3a48ec5-5578-43f7-ae99-3c6785f090f7",
      "name": "Get a row",
      "type": "n8n-nodes-base.supabase",
      "position": [
        -1040,
        144
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "thread_id",
              "keyValue": "={{ $json.threadId }}"
            }
          ]
        },
        "tableId": "threads",
        "operation": "get"
      },
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "fcae3538-c417-4f84-ac3b-f95bcc7fb4e5",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        -832,
        144
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "7ff63d14-3ae6-4fa1-bfc4-dfa9df41b74f",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.thread_id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "6c8fbfa6-e0e1-461f-8c53-c2741e36090b",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        -208,
        160
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "7f361c76-9d64-4100-92f2-3176a77539ed",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.message.content.category }}",
                    "rightValue": "support"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "0996814e-5fd0-480f-92e1-9bb8b52d6649",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.message.content.category }}",
                    "rightValue": "new-request"
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "34441930-fdd6-49dd-8b04-f43f9f6831c9",
      "name": "Mark a message as read",
      "type": "n8n-nodes-base.gmail",
      "position": [
        464,
        160
      ],
      "parameters": {
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "markAsRead"
      },
      "typeVersion": 2.1
    },
    {
      "id": "92d379d7-81e2-41d5-a927-1a8d3ad314ab",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1328,
        -80
      ],
      "parameters": {
        "color": 4,
        "width": 224,
        "height": 192,
        "content": "## 1. Gmail Trigger\n\ud83d\udcec Triggers whenever a new unread email arrives in the connected Gmail inbox.\n\n\u27a1\ufe0f Sends email data (From, Subject, Snippet, Thread ID, etc.) to the next node."
      },
      "typeVersion": 1
    },
    {
      "id": "14d334bf-cc9d-414a-a026-833ad0e00bd1",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1088,
        -48
      ],
      "parameters": {
        "content": "## 2. Get a row (Supabase) \n\ud83d\udd0d Checks Supabase table \u201cthreads\u201d to see if this email thread already exists."
      },
      "typeVersion": 1
    },
    {
      "id": "d941485f-c582-450d-a55e-364dbeda6127",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -832,
        -48
      ],
      "parameters": {
        "color": 2,
        "content": "## 3. IF Node\n\u2753Decision point:\n\t\u2022\tIf thread exists, stop (no duplicate processing).\n\t\u2022\tIf thread does not exist, continue workflow."
      },
      "typeVersion": 1
    },
    {
      "id": "ad5020dc-fe2a-46b1-b179-4f5dd0f20668",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -576,
        -80
      ],
      "parameters": {
        "color": 4,
        "width": 256,
        "height": 192,
        "content": "## 4. Message a Model (OpenAI)\n\ud83e\udde0 Sends the email subject and snippet to ChatGPT (4o) to classify the email into a category:\nsupport | new-request | enquiry | general | spam | other"
      },
      "typeVersion": 1
    },
    {
      "id": "4cf815f4-70ca-4bd9-a2ee-af021df944a6",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -304,
        -80
      ],
      "parameters": {
        "color": 6,
        "height": 192,
        "content": "## 5. Switch\n\ud83d\udd00 Routes the email based on the category returned by ChatGPT:\n\t\u2022\tIf \u201csupport\u201d \u2192 send to #support channel.\n\t\u2022\tIf \u201cnew-request\u201d \u2192 send to #new-requests channel.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "b8f9c6ea-61a9-4be2-a4a0-29d90caa8cf6",
      "name": "Send a message 2",
      "type": "n8n-nodes-base.slack",
      "position": [
        0,
        256
      ],
      "parameters": {
        "text": "=New request received from  {{ $('Gmail Trigger').item.json.From }} Subject: {{ $('Gmail Trigger').item.json.Subject }} Content: {{ $('Gmail Trigger').item.json.snippet }}",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C09EQ9Q1Z3R",
          "cachedResultName": "new-requests"
        },
        "otherOptions": {},
        "authentication": "oAuth2"
      },
      "typeVersion": 2.3
    },
    {
      "id": "3fe1a77a-3feb-49a6-8941-1ab666bf6cc5",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -48,
        -240
      ],
      "parameters": {
        "color": 4,
        "height": 208,
        "content": "## 6. Send a Message (Slack)\n\ud83d\udcac Posts formatted message in the #support Slack channel:\n\n\u201cNew support request received from [email]. Subject: [\u2026]. Content: [\u2026]\u201d"
      },
      "typeVersion": 1
    },
    {
      "id": "3f4a8b97-9b38-4de8-8a51-e023b43a493f",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -32,
        416
      ],
      "parameters": {
        "color": 4,
        "width": 256,
        "height": 208,
        "content": "## 7. Send a Message 2 (Slack)\n\ud83d\udcac Posts formatted message in the #new-requests Slack channel:\n\n\u201cNew request received from [email]. Subject: [\u2026]. Content: [\u2026]\u201d"
      },
      "typeVersion": 1
    },
    {
      "id": "5a3324e6-ab62-465d-8854-54c2cf59098d",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        208,
        -64
      ],
      "parameters": {
        "color": 5,
        "height": 192,
        "content": "## 8. Create a Row (Supabase)\n\ud83d\uddc3\ufe0f Adds a new record in the \u201cthreads\u201d table:\nStores thread_id, first_message_id, from_addr, subject for future deduplication."
      },
      "typeVersion": 1
    },
    {
      "id": "665325de-a9fb-4440-95b0-a1c197d79053",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        464,
        -32
      ],
      "parameters": {
        "color": 7,
        "content": "## 9. Mark a Message as Read (Gmail)\n\u2705 Marks the email as read in Gmail once it\u2019s processed and saved."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "04bdb1d8-234b-4623-a56c-f4c3593910c6",
  "connections": {
    "If": {
      "main": [
        [],
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Send a message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a message 2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a row": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a row": {
      "main": [
        [
          {
            "node": "Mark a message as read",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Get a row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a message": {
      "main": [
        [
          {
            "node": "Create a row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message a model": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a message 2": {
      "main": [
        [
          {
            "node": "Create a row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}