{
  "name": "Email Triage & Auto-Response",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute",
              "minute": 5
            }
          ]
        },
        "filters": {
          "readStatus": "unread"
        },
        "simple": false
      },
      "id": "gmail-trigger",
      "name": "New Email",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const email = $input.first().json;\nconst fromAddress = email.from?.value?.[0]?.address || '';\nconst subject = email.subject || '';\nconst body = (email.text || '').substring(0, 2000);\n\nconst requestBody = {\n  model: 'claude-sonnet-4-20250514',\n  max_tokens: 1024,\n  messages: [\n    {\n      role: 'user',\n      content: `Classify this email and draft a response if appropriate.\n\nFrom: ${fromAddress}\nSubject: ${subject}\nBody: ${body}\n\nRespond with JSON only:\n{\n  \"category\": \"reply_needed\" | \"auto_reply\" | \"newsletter\" | \"spam\",\n  \"priority\": \"high\" | \"medium\" | \"low\",\n  \"summary\": \"one sentence summary\",\n  \"draft_reply\": \"draft reply if category is auto_reply, otherwise null\"\n}\n\nCategories:\n- reply_needed: requires personal attention\n- auto_reply: routine messages with standard responses\n- newsletter: marketing, newsletters, product updates\n- spam: junk mail`\n    }\n  ]\n};\n\nreturn [{\n  json: {\n    requestBody,\n    original_from: fromAddress,\n    original_subject: subject,\n    original_id: email.id\n  }\n}];"
      },
      "id": "build-classify-request",
      "name": "Build Claude Request",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "url": "https://api.anthropic.com/v1/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "={{ $env.ANTHROPIC_API_KEY }}"
            },
            {
              "name": "anthropic-version",
              "value": "2023-06-01"
            },
            {
              "name": "content-type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json.requestBody) }}",
        "options": {}
      },
      "id": "call-claude",
      "name": "Classify with Claude",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const response = $input.first().json;\nconst content = response.content[0].text;\nconst prev = $('Build Claude Request').first().json;\n\nlet parsed;\ntry {\n  const jsonMatch = content.match(/\\{[\\s\\S]*\\}/);\n  parsed = JSON.parse(jsonMatch[0]);\n} catch (e) {\n  parsed = {\n    category: 'reply_needed',\n    priority: 'medium',\n    summary: 'Could not parse classification',\n    draft_reply: null\n  };\n}\n\nreturn [{\n  json: {\n    ...parsed,\n    original_from: prev.original_from,\n    original_subject: prev.original_subject,\n    original_id: prev.original_id\n  }\n}];"
      },
      "id": "parse-classification",
      "name": "Parse Classification",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "rules": {
          "rules": [
            {
              "outputIndex": 0,
              "value": "auto_reply"
            },
            {
              "outputIndex": 1,
              "value": "reply_needed"
            },
            {
              "outputIndex": 2,
              "value": "newsletter"
            },
            {
              "outputIndex": 3,
              "value": "spam"
            }
          ]
        },
        "fallbackOutput": 1,
        "mode": "rules",
        "output": "rules",
        "value": "={{ $json.category }}"
      },
      "id": "route-email",
      "name": "Route by Category",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        1120,
        300
      ],
      "outputs": [
        "Auto Reply",
        "Needs Attention",
        "Newsletter",
        "Spam"
      ]
    },
    {
      "parameters": {
        "sendTo": "={{ $json.original_from }}",
        "subject": "=Re: {{ $json.original_subject }}",
        "message": "={{ $json.draft_reply }}",
        "options": {}
      },
      "id": "send-auto-reply",
      "name": "Send Auto Reply",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        1360,
        140
      ]
    },
    {
      "parameters": {
        "jsCode": "const item = $input.first().json;\nconst payload = {\n  text: `New email needs your attention\\nFrom: ${item.original_from}\\nSubject: ${item.original_subject}\\nPriority: ${item.priority}\\nSummary: ${item.summary}`\n};\n\nreturn [{ json: payload }];"
      },
      "id": "build-slack-msg",
      "name": "Build Slack Message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1360,
        300
      ]
    },
    {
      "parameters": {
        "url": "={{ $env.SLACK_WEBHOOK_URL }}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}",
        "options": {}
      },
      "id": "notify-slack",
      "name": "Notify on Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1560,
        300
      ]
    },
    {
      "parameters": {
        "resource": "messageLabel",
        "messageId": "={{ $json.original_id }}",
        "labelIds": [
          "TRASH"
        ]
      },
      "id": "trash-spam",
      "name": "Trash Spam",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        1360,
        620
      ]
    },
    {
      "parameters": {
        "resource": "messageLabel",
        "messageId": "={{ $json.original_id }}",
        "labelIds": [
          "Label_newsletter"
        ]
      },
      "id": "label-newsletter",
      "name": "Label Newsletter",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        1360,
        460
      ]
    }
  ],
  "connections": {
    "New Email": {
      "main": [
        [
          {
            "node": "Build Claude Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Claude Request": {
      "main": [
        [
          {
            "node": "Classify with Claude",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify with Claude": {
      "main": [
        [
          {
            "node": "Parse Classification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Classification": {
      "main": [
        [
          {
            "node": "Route by Category",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by Category": {
      "main": [
        [
          {
            "node": "Send Auto Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Slack Message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label Newsletter",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Trash Spam",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Slack Message": {
      "main": [
        [
          {
            "node": "Notify on Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "loomiq-template"
  },
  "tags": [
    "loomiq",
    "email",
    "ai"
  ]
}