{
  "name": "AI obsługa zapytań ze strony (demo)",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "zapytanie",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a1b2c3d4-0001-4000-8000-000000000001",
      "name": "Formularz (Webhook)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 300],
      "webhookId": "a1b2c3d4-0001-4000-8000-0000000000aa"
    },
    {
      "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: 'Jesteś asystentem działu obsługi klienta. Na podstawie zapytania ze strony zwróć WYŁĄCZNIE JSON z polami: kategoria (wycena | reklamacja | pytanie o ofertę | spam), priorytet (pilny | wysoki | normalny | niski), sentyment, produkt, lokalizacja, termin, telefon, email, opiekun (Dział sprzedaży | Serwis | null), odpowiedz (uprzejmy szkic odpowiedzi po polsku, max 6 zdań, bez obietnic cenowych; dla spamu pusty tekst).' }, { role: 'user', content: 'Imię: ' + $json.body.name + '\\nKontakt: ' + $json.body.contact + '\\nWiadomość: ' + $json.body.message } ] }) }}",
        "options": {}
      },
      "id": "a1b2c3d4-0002-4000-8000-000000000002",
      "name": "AI: klasyfikacja + szkic",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "jsCode": "const form = $('Formularz (Webhook)').first().json.body;\nconst ai = JSON.parse($input.first().json.choices[0].message.content);\nreturn [{ json: { data: new Date().toISOString(), imie: form.name, kontakt: form.contact, wiadomosc: form.message, ...ai } }];"
      },
      "id": "a1b2c3d4-0003-4000-8000-000000000003",
      "name": "Złóż rekord",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [480, 300]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.kategoria }}",
              "operation": "contains",
              "value2": "spam"
            }
          ]
        }
      },
      "id": "a1b2c3d4-0004-4000-8000-000000000004",
      "name": "Spam?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [720, 300]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": { "__rl": true, "mode": "id", "value": "WSTAW_ID_ARKUSZA" },
        "sheetName": { "__rl": true, "mode": "name", "value": "Zgłoszenia" },
        "columns": { "mappingMode": "autoMapInputData", "value": {} },
        "options": {}
      },
      "id": "a1b2c3d4-0005-4000-8000-000000000005",
      "name": "Zapis do arkusza",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [960, 400]
    },
    {
      "parameters": {
        "chatId": "WSTAW_CHAT_ID",
        "text": "={{ ($('Złóż rekord').item.json.priorytet === 'pilny' ? '🔴' : $('Złóż rekord').item.json.priorytet === 'wysoki' ? '🟠' : '🟢') + ' Nowe zgłoszenie: ' + $('Złóż rekord').item.json.kategoria + '\\n' + $('Złóż rekord').item.json.imie + ' · ' + $('Złóż rekord').item.json.kontakt + '\\n' + ($('Złóż rekord').item.json.produkt || $('Złóż rekord').item.json.wiadomosc.slice(0, 120)) + '\\n\\nSzkic odpowiedzi:\\n' + $('Złóż rekord').item.json.odpowiedz }}",
        "additionalFields": {}
      },
      "id": "a1b2c3d4-0006-4000-8000-000000000006",
      "name": "Powiadom handlowca",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1200, 400]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ ok: true, komunikat: 'Dziękujemy! Odpowiemy najszybciej, jak to możliwe.' }) }}",
        "options": {}
      },
      "id": "a1b2c3d4-0007-4000-8000-000000000007",
      "name": "Odpowiedź dla formularza",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [1440, 300]
    }
  ],
  "connections": {
    "Formularz (Webhook)": { "main": [[{ "node": "AI: klasyfikacja + szkic", "type": "main", "index": 0 }]] },
    "AI: klasyfikacja + szkic": { "main": [[{ "node": "Złóż rekord", "type": "main", "index": 0 }]] },
    "Złóż rekord": { "main": [[{ "node": "Spam?", "type": "main", "index": 0 }]] },
    "Spam?": { "main": [
      [{ "node": "Odpowiedź dla formularza", "type": "main", "index": 0 }],
      [{ "node": "Zapis do arkusza", "type": "main", "index": 0 }]
    ] },
    "Zapis do arkusza": { "main": [[{ "node": "Powiadom handlowca", "type": "main", "index": 0 }]] },
    "Powiadom handlowca": { "main": [[{ "node": "Odpowiedź dla formularza", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "pinData": {}
}
