{
  "name": "AI Daily Email Digest Creator",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 17 * * 1-5"
            }
          ]
        }
      },
      "id": "b1b2c3d4-2005-4000-8000-000000000001",
      "name": "End of Day Schedule",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "getAll",
        "returnAll": false,
        "limit": 50,
        "filters": {
          "receivedAfter": "={{ new Date(Date.now() - 24*60*60*1000).toISOString() }}",
          "readStatus": "both"
        },
        "options": {}
      },
      "id": "b1b2c3d4-2005-4000-8000-000000000002",
      "name": "Fetch Today's Emails",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const emails = $input.all().map(item => ({\n  from: item.json.from,\n  subject: item.json.subject,\n  snippet: item.json.snippet,\n  date: item.json.date,\n  isRead: item.json.labelIds ? !item.json.labelIds.includes('UNREAD') : true\n}));\n\nreturn [{\n  json: {\n    emailCount: emails.length,\n    unreadCount: emails.filter(e => !e.isRead).length,\n    emailSummaries: JSON.stringify(emails.slice(0, 20))\n  }\n}];"
      },
      "id": "b1b2c3d4-2005-4000-8000-000000000003",
      "name": "Prepare Email Data",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "model": "gpt-4",
        "messages": {
          "values": [
            {
              "content": "Create a concise daily email digest summary. Group emails by category (work, personal, newsletters, etc.) and highlight the most important ones.\n\nTotal emails today: {{ $json.emailCount }}\nUnread: {{ $json.unreadCount }}\n\nEmails:\n{{ $json.emailSummaries }}\n\nReturn JSON with keys:\n- headline (catchy one-line summary of the day)\n- categories (array of {name, count, highlights: [string]})\n- actionItems (array of emails needing response)\n- canIgnore (array of low-priority items)"
            }
          ]
        },
        "options": {
          "temperature": 0.5,
          "maxTokens": 2000
        }
      },
      "id": "b1b2c3d4-2005-4000-8000-000000000004",
      "name": "OpenAI Summarize",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.3,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const response = JSON.parse($input.first().json.message.content);\nconst data = $('Prepare Email Data').first().json;\n\nlet slackMessage = `*Daily Email Digest*\\n${response.headline}\\n\\n`;\nslackMessage += `*Total:* ${data.emailCount} emails | *Unread:* ${data.unreadCount}\\n\\n`;\n\nfor (const cat of response.categories) {\n  slackMessage += `*${cat.name}* (${cat.count})\\n`;\n  for (const h of cat.highlights) {\n    slackMessage += `  - ${h}\\n`;\n  }\n}\n\nif (response.actionItems.length > 0) {\n  slackMessage += `\\n:warning: *Action Required:*\\n`;\n  for (const item of response.actionItems) {\n    slackMessage += `  - ${item}\\n`;\n  }\n}\n\nreturn [{ json: { slackMessage } }];"
      },
      "id": "b1b2c3d4-2005-4000-8000-000000000005",
      "name": "Format Slack Message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "channel": "#daily-digest",
        "text": "={{ $json.slackMessage }}",
        "otherOptions": {}
      },
      "id": "b1b2c3d4-2005-4000-8000-000000000006",
      "name": "Post to Slack",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1500,
        300
      ]
    }
  ],
  "connections": {
    "End of Day Schedule": {
      "main": [
        [
          {
            "node": "Fetch Today's Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Today's Emails": {
      "main": [
        [
          {
            "node": "Prepare Email Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Email Data": {
      "main": [
        [
          {
            "node": "OpenAI Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Summarize": {
      "main": [
        [
          {
            "node": "Format Slack Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Slack Message": {
      "main": [
        [
          {
            "node": "Post to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}