{
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "custom",
              "cronExpression": "*/5 * * * *"
            }
          ]
        },
        "filters": {
          "readStatus": "unread"
        }
      },
      "id": "f0dac76f-41ce-4c33-add9-0278701badaf",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1,
      "position": [
        -2224,
        32
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "id": "check-n8n-label",
              "leftValue": "={{$json.labels.some(label => label.id.includes('n8n-processed')) }}",
              "rightValue": "INBOX",
              "operator": {
                "type": "boolean",
                "operation": "false",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "bac38f70-f0ab-43ae-b2a3-04dc17ee1f40",
      "name": "Skip if Already Processed",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        -2000,
        32
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "emailFrom",
              "name": "emailFrom",
              "value": "={{ $json.From }}",
              "type": "string"
            },
            {
              "id": "emailFromName",
              "name": "emailFromName",
              "value": "={{ $json.From }}",
              "type": "string"
            },
            {
              "id": "subject",
              "name": "subject",
              "value": "={{ $json.Subject }}",
              "type": "string"
            },
            {
              "id": "bodyPlain",
              "name": "bodyPlain",
              "value": "={{ ($json.text || $json.snippet || '').substring(0, 4000) }}",
              "type": "string"
            },
            {
              "id": "messageId",
              "name": "messageId",
              "value": "={{ $json.id }}",
              "type": "string"
            },
            {
              "id": "threadId",
              "name": "threadId",
              "value": "={{ $json.threadId }}",
              "type": "string"
            },
            {
              "id": "receivedAt",
              "name": "receivedAt",
              "value": "={{ $json.internalDate.toDateTime('ms') }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "1f2380bb-2533-4a25-acbb-1e389b461146",
      "name": "Extract Email Fields",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.3,
      "position": [
        -1776,
        32
      ]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "GPT-4O-MINI"
        },
        "messages": {
          "values": [
            {
              "content": "Jeste\u015b inteligentnym botem odpowiadaj\u0105cym na wiadomo\u015bci e-mail dla firmy \"Projekt na Cito\" (us\u0142ugi projektowania wn\u0119trz). \nTwoim zadaniem jest oceni\u0107, czy wiadomo\u015b\u0107 to zapytanie o ofert\u0119/us\u0142ugi biura projektowego, portfolio, wycen\u0119 lub inne kwestie zwi\u0105zane z cenami lub projektem, lub pro\u015bba dotyczy kontaktu telefonicznego, oznacz j\u0105 jako \"offer\", w przeciwnym razie je\u015bli widaomo\u015b\u0107 nie dotyczy zapytania zwi\u0105zanego z prowadzonym biznesem jako \"other\".\n\nODPOWIED\u0179 WY\u0141\u0104CZNIE W FORMACIE JSON (NIE DODAWAJ INNEGO TEKSTU). \nZWR\u00d3\u0106 OBIEKT Z POLAMI: \nlabel: \"offer\" (zapytanie ofertowe) lub \"other\" (inne) \n- confidence: liczba od 0 do 1 (pewno\u015b\u0107 klasyfikacji) \n- intent: kr\u00f3tki token opisuj\u0105cy intencj\u0119 (np. \"pricing_request\", \"portfolio_request\", \"meeting_request\", \"complaint\", \"spam\")\n- entities: lista fraz kluczowych z maila (np. [\"kuchnia\", \"\u0142azienka\", \"salon\"])\n- reason: 1-zdaniowe uzasadnienie klasyfikacji. \n\nPRZYK\u0141ADY: Email: \"Ile b\u0119dzie kosztowa\u0142o zaprojektowanie kuchni i \u0142azienki?\"\nOdpowied\u017a: \n{\"label\":\"offer\",\"confidence\":0.95,\"intent\":\"pricing_request\",\"entities\":[\"kuchnia\",\"\u0142azienka\"],\"reason\":\"bezpo\u015brednie pytanie o koszt zaprojektowania pomieszcze\u0144\"} \nEmail: \"Czy macie portfolio?\"\nOdpowied\u017a: {\"label\":\"offer\",\"confidence\":0.98,\"intent\":\"portfolio_request\",\"entities\":[\"portfolio\"],\"reason\":\"pro\u015bba o portfolio\"}\nEmail: \"Chc\u0119 um\u00f3wi\u0107 spotkanie z architektem\"\nOdpowied\u017a: {\"label\":\"offer\",\"confidence\":0.95,\"intent\":\"meeting_request\",\"entities\":[\"meeting\", \"architect\"],\"reason\":\"pro\u015bba o spotkanie z architektem\"}\nEmail: \"Interesuje mnie projekt ca\u0142ego mieszkania 60m2, ile to b\u0119dzie kosztowa\u0107?\"\nOdpowied\u017a: {\"label\":\"offer\",\"confidence\":0.98,\"intent\":\"pricing_request\",\"entities\":[\"mieszkanie\",\"60m2\"],\"reason\":\"pytanie o wycen\u0119 projektu mieszkania z podanym metra\u017cem\"}\nEmail: \"Dzie\u0144 dobry, mam psa i kota\"\nOdpowied\u017a: {\"label\":\"other\",\"confidence\":0.95,\"intent\":\"spam\",\"entities\":[],\"reason\":\"wiadomo\u015b\u0107 nie dotyczy us\u0142ug projektowych\"}",
              "role": "assistant"
            },
            {
              "content": "=Email subject:  {{ $json.subject }}\nEmail body: {{ $json.bodyPlain }}"
            }
          ]
        },
        "jsonOutput": true,
        "options": {}
      },
      "id": "5f493157-c102-49cb-9c58-d0898af77c4d",
      "name": "OpenAI Classify",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.3,
      "position": [
        -1552,
        32
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 1
          },
          "conditions": [
            {
              "id": "condition-label",
              "leftValue": "={{ $json.message.content.label }}",
              "rightValue": "offer",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            },
            {
              "id": "condition-confidence",
              "leftValue": "={{ $json.message.content.confidence }}",
              "rightValue": 0.7,
              "operator": {
                "type": "number",
                "operation": "gte"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "cec5bc8c-d341-40d7-bd75-20147fdb8809",
      "name": "Is Offer Request?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        -1200,
        32
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "replyTo",
              "name": "replyTo",
              "value": "={{ $('Extract Email Fields').item.json.emailFrom }}",
              "type": "string"
            },
            {
              "id": "replySubject",
              "name": "replySubject",
              "value": "=Re: {{ $('Extract Email Fields').item.json.subject || 'Zapytanie' }} \u2014 Informacja o kalkulatorze oferty",
              "type": "string"
            },
            {
              "id": "replyBody",
              "name": "replyBody",
              "value": "=<p>\n      Cze\u015b\u0107 {{ $json.emailFromName || 'Kliencie' }},\n    </p>\n\n    <p>\n      Dzi\u0119kujemy za wiadomo\u015b\u0107 i zainteresowanie us\u0142ugami <strong>Projekt na Cito</strong>.\n    </p>\n\n    <p>\n      Wygl\u0105da na to, \u017ce pytasz o wycen\u0119 projektu. Najszybszym sposobem uzyskania przybli\u017conej\n      ceny jest nasz kalkulator oferty:\n      <a href=\"https://projektnacito.com.pl\" target=\"_blank\" rel=\"noopener noreferrer\">\n        https://projektnacito.com.pl\n      </a>\n    </p>\n\n    <p>Wybierz odpowiedni pakiet i skonfiguruj go wed\u0142ug swoich potrzeb:</p>\n    <ul>\n      <li>\n        <strong>Pakiet na Cito</strong> \u2013 szybki projekt zawieraj\u0105cy wszystko co niezb\u0119dne.\n      </li>\n      <li>\n        <strong>Pakiet Premium</strong> \u2013 kompleksowy projekt z pe\u0142n\u0105 dokumentacj\u0105.\n      </li>\n      <li>\n        <strong>Konsultacje</strong> \u2013 godzinne konsultacje z projektantem.\n      </li>\n    </ul>\n\n    <p>Pozdrawiamy serdecznie,<br />\n    Zesp\u00f3\u0142 Projekt na Cito</p>\n\n    <hr />\n    <p style=\"font-size: 12px; color: #777;\">\n      Wiadomo\u015b\u0107 wys\u0142ana automatycznie\n    </p>",
              "type": "string"
            },
            {
              "id": "threadId",
              "name": "threadId",
              "value": "={{ $('Extract Email Fields').item.json.threadId }}",
              "type": "string"
            },
            {
              "id": "messageId",
              "name": "messageId",
              "value": "={{ $('Extract Email Fields').item.json.messageId }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "e398836f-91ed-4d0e-a1e1-7f4cd0719c8e",
      "name": "Prepare Auto Reply",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.3,
      "position": [
        -976,
        -64
      ]
    },
    {
      "parameters": {
        "sendTo": "={{ $json.replyTo }}",
        "subject": "={{ $json.replySubject }}",
        "message": "={{ $json.replyBody }}",
        "options": {
          "appendAttribution": false,
          "senderName": "Projekt na Cito",
          "replyToSenderOnly": false
        }
      },
      "id": "1937159f-3db5-44a9-b3f7-1935fbbc761b",
      "name": "Send Auto Reply",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -752,
        -64
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $('Extract Email Fields').item.json.messageId }}",
        "labelIds": [
          "Label_1170604044697810115",
          "Label_5333537222323505779"
        ]
      },
      "id": "985c8e1f-e1b5-4e18-9ae1-6feae3616b7a",
      "name": "Add Label (Offer)",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -528,
        -64
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "markAsRead",
        "messageId": "={{ $('Extract Email Fields').item.json.messageId }}"
      },
      "id": "88d27f17-1e1f-4ba6-a16a-7a3989a72f40",
      "name": "Mark as Read (Offer)",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -304,
        -64
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $('Extract Email Fields').item.json.messageId }}",
        "labelIds": [
          "Label_5333537222323505779"
        ]
      },
      "id": "1b11f004-1612-400a-b12a-d1f2d3316b14",
      "name": "Add Label (Other)",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        -976,
        128
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Skip if Already Processed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Skip if Already Processed": {
      "main": [
        [
          {
            "node": "Extract Email Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Email Fields": {
      "main": [
        [
          {
            "node": "OpenAI Classify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Classify": {
      "main": [
        [
          {
            "node": "Is Offer Request?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Offer Request?": {
      "main": [
        [
          {
            "node": "Prepare Auto Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Add Label (Other)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Auto Reply": {
      "main": [
        [
          {
            "node": "Send Auto Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Auto Reply": {
      "main": [
        [
          {
            "node": "Add Label (Offer)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Label (Offer)": {
      "main": [
        [
          {
            "node": "Mark as Read (Offer)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Label (Other)": {
      "main": [
        []
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": true
  }
}