{
  "name": "E-Mails per KI einordnen und Antwortentwürfe erstellen",
  "nodes": [
    {
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "in:inbox is:unread"
        },
        "options": {
          "downloadAttachments": true
        }
      },
      "id": "00000000-0000-4000-8000-000000000007",
      "position": [
        240,
        300
      ]
    },
    {
      "name": "KI",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: 'gpt-4o-mini', temperature: 0.2, response_format: { type: 'json_object' }, messages: [ { role: 'system', content: 'Du sortierst geschäftliche E-Mails. Antworte als JSON: kategorie (anfrage, rechnung, newsletter, spam, sonstiges), dringend (true/false), entwurf (höfliche deutsche Antwort in Sie-Form, nur bei kategorie anfrage, sonst leer).' }, { role: 'user', content: String('Betreff: ' + $json.subject + '\\n\\n' + $json.text).slice(0, 12000) } ] }) }}",
        "options": {}
      },
      "id": "00000000-0000-4000-8000-000000000008",
      "position": [
        480,
        300
      ]
    },
    {
      "name": "Antwort aufbereiten",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "const src = $('Gmail Trigger').all();\nreturn $input.all().map((item, i) => {\n  const raw = item.json.choices[0].message.content;\n  let data; try { data = JSON.parse(raw); } catch (e) { data = { text: raw }; }\n  \n  return { json: data, binary: src[i].binary };\n});"
      },
      "id": "00000000-0000-4000-8000-000000000009",
      "position": [
        720,
        300
      ]
    },
    {
      "name": "Entwurf anlegen",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "parameters": {
        "resource": "draft",
        "subject": "=Re: {{ $('Gmail Trigger').item.json.subject }}",
        "message": "={{ $json.entwurf || \"(kein Entwurf nötig, Kategorie: \" + $json.kategorie + \")\" }}",
        "options": {
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}"
        }
      },
      "id": "00000000-0000-4000-8000-000000000010",
      "position": [
        960,
        300
      ]
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "KI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "KI": {
      "main": [
        [
          {
            "node": "Antwort aufbereiten",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Antwort aufbereiten": {
      "main": [
        [
          {
            "node": "Entwurf anlegen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "active": false,
  "meta": {
    "templateCredsSetupCompleted": false,
    "source": "flowregal.de"
  }
}