{
  "name": "Email Assistent",
  "nodes": [
    {
      "parameters": {
        "mailbox": "INBOX",
        "postProcessAction": "read",
        "options": {
          "forceReconnect": 3600
        }
      },
      "id": "imap-trigger",
      "name": "Neue E-Mail",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 2,
      "position": [0, 300],
      "credentials": {
        "imap": {
          "id": "",
          "name": "Dein IMAP Account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "/*\n * Klassifiziert E-Mail und speichert fuer Digest\n * Passe die Sender-Listen an deine Beduerfnisse an!\n */\nconst email = $input.first().json;\nconst staticData = $getWorkflowStaticData('global');\n\n// Initialisiere Speicher\nif (!staticData.emails) staticData.emails = [];\n\nconst from = (email.from?.text || email.from || '').toLowerCase();\nconst subject = (email.subject || '').toLowerCase();\n\n// Passe diese Listen an!\nconst spamSenders = ['newsletter', 'noreply', 'no-reply', 'marketing', 'promo', 'deals', 'advertising', 'mailchimp', 'sendgrid', 'campaign'];\nconst spamSubjects = ['unsubscribe', 'abmelden', 'newsletter', 'rabatt', 'discount', 'sale', 'angebot', 'gutschein', 'gratis', 'kostenlos', 'werbung'];\nconst importantSenders = ['schule', 'kita', 'gymnasium', 'gemeinde', 'stadt', 'rathaus', 'finanzamt', 'sparkasse', 'volksbank', 'arzt', 'praxis', 'versicherung'];\n\nlet classification = 'info';\nfor (const s of importantSenders) { if (from.includes(s)) { classification = 'important'; break; } }\nif (classification === 'info') {\n  for (const s of spamSenders) { if (from.includes(s)) { classification = 'spam'; break; } }\n}\nif (classification === 'info') {\n  for (const k of spamSubjects) { if (subject.includes(k)) { classification = 'spam'; break; } }\n}\n\n// Speichere fuer Digest\nstaticData.emails.push({\n  from: email.from?.text || email.from || 'Unbekannt',\n  subject: email.subject || 'Kein Betreff',\n  classification: classification,\n  date: new Date().toISOString()\n});\n\n// Max 100 E-Mails behalten\nif (staticData.emails.length > 100) staticData.emails = staticData.emails.slice(-100);\n\nreturn [{\n  json: {\n    from: email.from?.text || email.from || 'Unbekannt',\n    subject: email.subject || 'Kein Betreff',\n    snippet: (email.text || '').substring(0, 200),\n    _classification: classification\n  }\n}];"
      },
      "id": "classify-save",
      "name": "Klassifizieren + Speichern",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 300]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "is-important",
              "leftValue": "={{ $json._classification }}",
              "rightValue": "important",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-important",
      "name": "Wichtig?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [440, 300]
    },
    {
      "parameters": {
        "jsCode": "const email = $input.first().json;\nconst esc = (str) => (str || '').replace(/[_*\\[\\]()~`>#+\\-=|{}.!]/g, '\\\\$&');\n\nlet msg = `📧 *Wichtige E\\\\-Mail*\\n\\n`;\nmsg += `*${esc(email.subject)}*\\n`;\nmsg += `📤 ${esc(email.from.substring(0, 50))}\\n\\n`;\nif (email.snippet) msg += `_${esc(email.snippet.substring(0, 150))}\\\\.\\\\.\\\\._`;\n\nreturn [{ json: { message: msg } }];"
      },
      "id": "format-important",
      "name": "Format",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 200]
    },
    {
      "parameters": {
        "chatId": "DEINE_CHAT_ID",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "parse_mode": "MarkdownV2",
          "appendAttribution": false
        }
      },
      "id": "telegram-important",
      "name": "Telegram Sofort",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [880, 200],
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Dein Telegram Bot"
        }
      }
    },
    {
      "parameters": {},
      "id": "noop-not-important",
      "name": "Nicht wichtig",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [660, 400]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "cronExpression",
              "expression": "0 7 * * *"
            }
          ]
        }
      },
      "id": "schedule-digest",
      "name": "Taeglich 7:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [0, 600]
    },
    {
      "parameters": {
        "jsCode": "/*\n * Liest gespeicherte E-Mails und erstellt Digest\n */\nconst staticData = $getWorkflowStaticData('global');\nconst emails = staticData.emails || [];\n\n// Zaehle nach Kategorie\nconst results = { important: [], info: [], spam: [] };\nfor (const e of emails) {\n  results[e.classification || 'info'].push(e);\n}\n\n// Speicher leeren\nstaticData.emails = [];\n\nreturn [{\n  json: {\n    important: results.important,\n    info: results.info,\n    spam: results.spam,\n    totalCount: emails.length,\n    importantCount: results.important.length,\n    infoCount: results.info.length,\n    spamCount: results.spam.length\n  }\n}];"
      },
      "id": "read-digest",
      "name": "Digest lesen",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [220, 600]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "has-emails",
              "leftValue": "={{ $json.totalCount }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-has-emails",
      "name": "E-Mails?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [440, 600]
    },
    {
      "parameters": {
        "jsCode": "const data = $input.first().json;\nconst esc = (str) => (str || '').replace(/[_*\\[\\]()~`>#+\\-=|{}.!]/g, '\\\\$&');\n\nconst today = new Date().toLocaleDateString('de-DE', { weekday: 'long', day: 'numeric', month: 'long' });\n\nlet msg = `☀️ *Guten Morgen\\\\!*\\n_${esc(today)}_\\n\\n`;\nmsg += `📬 *E\\\\-Mail Uebersicht \\\\(24h\\\\)*\\n\\n`;\n\nif (data.importantCount > 0) {\n  msg += `🔴 *${data.importantCount} wichtig:*\\n`;\n  for (const e of data.important.slice(0, 5)) {\n    msg += `• ${esc(e.subject.substring(0, 35))}\\n`;\n  }\n  if (data.importantCount > 5) msg += `_\\\\+${data.importantCount - 5} weitere_\\n`;\n  msg += `\\n`;\n}\n\nif (data.infoCount > 0) {\n  msg += `🔵 *${data.infoCount} Info:*\\n`;\n  for (const e of data.info.slice(0, 3)) {\n    msg += `• ${esc(e.subject.substring(0, 35))}\\n`;\n  }\n  if (data.infoCount > 3) msg += `_\\\\+${data.infoCount - 3} weitere_\\n`;\n  msg += `\\n`;\n}\n\nif (data.spamCount > 0) {\n  msg += `⚪ _${data.spamCount} Werbung gefiltert_\\n`;\n}\n\nreturn [{ json: { message: msg } }];"
      },
      "id": "format-digest",
      "name": "Format Digest",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [660, 500]
    },
    {
      "parameters": {
        "chatId": "DEINE_CHAT_ID",
        "text": "={{ $json.message }}",
        "additionalFields": {
          "parse_mode": "MarkdownV2",
          "appendAttribution": false
        }
      },
      "id": "telegram-digest",
      "name": "Telegram Digest",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [880, 500],
      "credentials": {
        "telegramApi": {
          "id": "",
          "name": "Dein Telegram Bot"
        }
      }
    },
    {
      "parameters": {},
      "id": "noop-no-emails",
      "name": "Keine E-Mails",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [660, 700]
    },
    {
      "parameters": {
        "content": "## 📧 Email Assistent\n\n**Echtzeit (oben):**\nNeue E-Mail → Klassifizieren → Speichern\n→ Wichtig? Sofort Telegram\n\n**Digest (unten):**\nTaeglich 7:00 → Gespeicherte lesen\n→ Zusammenfassung → Telegram\n→ Speicher leeren\n\n**Anpassen:**\n- importantSenders: Absender die wichtig sind\n- spamSenders: Absender die ignoriert werden\n- chatId: Deine Telegram Chat ID",
        "height": 280,
        "width": 320
      },
      "id": "sticky-note",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [-100, 40]
    }
  ],
  "connections": {
    "Neue E-Mail": {
      "main": [[{"node": "Klassifizieren + Speichern", "type": "main", "index": 0}]]
    },
    "Klassifizieren + Speichern": {
      "main": [[{"node": "Wichtig?", "type": "main", "index": 0}]]
    },
    "Wichtig?": {
      "main": [
        [{"node": "Format", "type": "main", "index": 0}],
        [{"node": "Nicht wichtig", "type": "main", "index": 0}]
      ]
    },
    "Format": {
      "main": [[{"node": "Telegram Sofort", "type": "main", "index": 0}]]
    },
    "Taeglich 7:00": {
      "main": [[{"node": "Digest lesen", "type": "main", "index": 0}]]
    },
    "Digest lesen": {
      "main": [[{"node": "E-Mails?", "type": "main", "index": 0}]]
    },
    "E-Mails?": {
      "main": [
        [{"node": "Format Digest", "type": "main", "index": 0}],
        [{"node": "Keine E-Mails", "type": "main", "index": 0}]
      ]
    },
    "Format Digest": {
      "main": [[{"node": "Telegram Digest", "type": "main", "index": 0}]]
    }
  },
  "settings": {"executionOrder": "v1"},
  "staticData": {"global": {"emails": []}},
  "tags": [],
  "triggerCount": 2,
  "pinData": {}
}
