{
  "active": false,
  "connections": {
    "Default Data Loader1": {
      "ai_document": [
        [
          {
            "node": "Insert into Supabase Vectorstore1",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI2": {
      "ai_embedding": [
        [
          {
            "node": "Insert into Supabase Vectorstore1",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Set File ID1": {
      "main": [
        [
          {
            "node": "Delete Old Docs1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Character Text Splitter1": {
      "ai_textSplitter": [
        [
          {
            "node": "Default Data Loader1",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Insert into Supabase Vectorstore1": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items1": {
      "main": [
        [],
        [
          {
            "node": "Set File ID1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Delete Old Docs1": {
      "main": [
        [
          {
            "node": "Switch2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "HTTP Request2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request2": {
      "main": [
        [
          {
            "node": "Edit Fields1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields1": {
      "main": [
        [
          {
            "node": "HTTP Request3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch2": {
      "main": [
        [
          {
            "node": "Insert into Supabase Vectorstore1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request3": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger1": {
      "main": [
        [
          {
            "node": "Switch4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch4": {
      "main": [
        [
          {
            "node": "Download DOCX",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Download PDF",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Download Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download PDF": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download DOCX": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Image": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "createdAt": "2025-07-06T10:19:47.922Z",
  "id": "OPO45v5u31HSzcKj",
  "meta": null,
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {
        "content": "\u0417\u0430\u0433\u0440\u0443\u0437\u0447\u0438\u043a",
        "height": 820,
        "width": 1640,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -300,
        220
      ],
      "typeVersion": 1,
      "id": "d3846e89-12b1-4576-83bd-c35c705386b8",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "dataType": "binary",
        "options": {
          "metadata": {
            "metadataValues": [
              {
                "name": "=file_id",
                "value": "={{ $('Set File ID1').first().json.file_id }}"
              },
              {
                "name": "file_title",
                "value": "={{ $('Set File ID1').first().json.file_title }}"
              }
            ]
          }
        }
      },
      "id": "6e7921ee-ea4c-4c1f-bcbe-f10a37a4998b",
      "name": "Default Data Loader1",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "typeVersion": 1,
      "position": [
        280,
        660
      ]
    },
    {
      "parameters": {
        "model": "text-embedding-3-small",
        "options": {}
      },
      "id": "46c67219-0897-4ded-a43d-a8a391330d15",
      "name": "Embeddings OpenAI2",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1,
      "position": [
        140,
        700
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "10646eae-ae46-4327-a4dc-9987c2d76173",
              "name": "file_id",
              "value": "={{ $('Telegram Trigger1').item.json.message.document.file_unique_id }}",
              "type": "string"
            },
            {
              "id": "f4536df5-d0b1-4392-bf17-b8137fb31a44",
              "name": "file_type",
              "value": "={{ $('Telegram Trigger1').item.json.message.document.mime_type }}",
              "type": "string"
            },
            {
              "id": "77d782de-169d-4a46-8a8e-a3831c04d90f",
              "name": "file_title",
              "value": "={{ $('Telegram Trigger1').item.json.message.document.file_name }}",
              "type": "string"
            },
            {
              "id": "9bde4d7f-e4f3-4ebd-9338-dce1350f9eab",
              "name": "file_url",
              "value": "=",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "c58fa8a2-b8cd-4237-9d80-bbf2b7da8c0c",
      "name": "Set File ID1",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        400,
        260
      ]
    },
    {
      "parameters": {
        "chunkSize": 2000,
        "chunkOverlap": 200
      },
      "id": "d93a74e9-ddb5-47cf-ad0b-a5ab2e4160b7",
      "name": "Character Text Splitter1",
      "type": "@n8n/n8n-nodes-langchain.textSplitterCharacterTextSplitter",
      "typeVersion": 1,
      "position": [
        360,
        760
      ]
    },
    {
      "parameters": {
        "mode": "insert",
        "tableName": {
          "__rl": true,
          "value": "documents",
          "mode": "id"
        },
        "options": {
          "queryName": "documents"
        }
      },
      "id": "a81beb19-ab5d-4c4e-916f-f085abe79571",
      "name": "Insert into Supabase Vectorstore1",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1,
      "position": [
        160,
        460
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        160,
        260
      ],
      "id": "3550e110-fd18-490e-915f-e925126b4296",
      "name": "Loop Over Items1"
    },
    {
      "parameters": {
        "operation": "delete",
        "tableId": "documents",
        "filterType": "string",
        "filterString": "=metadata->>file_id=like.*{{ $json.file_id }}*"
      },
      "id": "a3f88d8c-9596-4afa-9419-13cb105cba13",
      "name": "Delete Old Docs1",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        600,
        260
      ],
      "alwaysOutputData": true,
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const fn = $binary.data.fileName;          // \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u0438\u043c\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440\n                                          // \"SWD Rheinol Antifreeze GW-11 (-40\u00b0C).pdf\"\n\n// \u0432\u044b\u0442\u0430\u0449\u0438\u043c ID \u0438\u0437 \u0438\u043c\u0435\u043d\u0438, \u043a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435\nconst [, id] = fn.match(/(\\d+)/) || [];\n\n/**\n * \u0414\u0435\u043b\u0430\u0435\u0442 \u0438\u043c\u044f \u00abURL-safe\u00bb: \u043b\u0430\u0442\u0438\u043d\u0438\u0446\u0430/\u0446\u0438\u0444\u0440\u044b/- _ . /\n *   \u043f\u0440\u043e\u0431\u0435\u043b\u044b, \u0441\u043a\u043e\u0431\u043a\u0438, \u00b0, \u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430 \u0438 \u0442.\u043f. \u2192 _\n */\nfunction makeSafePath(name) {\n  return name\n    .normalize('NFKD')              // \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u043c \u0434\u0438\u0430\u043a\u0440\u0438\u0442\u0438\u043a\u0443\n    .replace(/[^\\w.\\-\\/]+/g, '_')   // \u0432\u0441\u0451 \u00ab\u043b\u0438\u0448\u043d\u0435\u0435\u00bb \u2192 _\n    .replace(/_+/g, '_')            // \u043f\u043e\u0434\u0440\u044f\u0434 \u0438\u0434\u0443\u0449\u0438\u0435 _ \u2192 \u043e\u0434\u0438\u043d _\n    .replace(/^_+|_+$/g, '');       // \u0443\u0431\u0438\u0440\u0430\u0435\u043c _ \u0432 \u043d\u0430\u0447\u0430\u043b\u0435/\u043a\u043e\u043d\u0446\u0435\n}\n\nconst safePath = makeSafePath(fn);  // \"SWD_Rheinol_Antifreeze_GW-11_-40C_.pdf\"\n\nreturn [{\n  json: {\n    product_id: +id || null,\n    file_type: $binary.data.mimeType === 'image/gif' ? 'gif' : 'passport',\n    file_title: fn,        // \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u043c\u044f \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e\n    path: safePath         // \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 \u0434\u043b\u044f Storage\n  },\n  binary: {\n    data: $binary.data\n  }\n}];\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        700,
        660
      ],
      "id": "96c68f9b-1e4c-4b5b-987b-3c9cd51a65ef",
      "name": "Code1"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://nkxxsqytgayjvnkwgrul.supabase.co/storage/v1/object/media/{{$json[\"path\"]}}",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-upsert",
              "value": "true"
            },
            {
              "name": "Content-Type",
              "value": "application/octet-stream"
            }
          ]
        },
        "sendBody": true,
        "contentType": "binaryData",
        "inputDataFieldName": "data",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        660
      ],
      "id": "581432d9-f079-4715-bfa3-745b0a5f9362",
      "name": "HTTP Request2",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "9c8b7b18-b04a-4868-9920-57a952f723bd",
              "name": "product_id",
              "value": "={{ $('Code1').item.json.product_id }}",
              "type": "string"
            },
            {
              "id": "23d5a7a5-1ddc-4b3a-a02a-47211205388c",
              "name": "file_type",
              "value": "={{ $('Set File ID1').item.json.file_type }}",
              "type": "string"
            },
            {
              "id": "64368f17-0e67-428d-aa0c-f4404ca9aeab",
              "name": "file_title",
              "value": "={{ $('Code1').item.json.file_title }}",
              "type": "string"
            },
            {
              "id": "bb0d5349-76cb-4d71-bcc2-fe8eaf0362da",
              "name": "=url",
              "value": "=https://nkxxsqytgayjvnkwgrul.supabase.co/storage/v1/object/public/media//{{ $('Code1').item.json.path }}",
              "type": "string"
            },
            {
              "id": "6edc999a-9964-41e7-a0b0-1dfaf0d0dfb5",
              "name": "path",
              "value": "={{ $('Code1').item.json.path }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1040,
        660
      ],
      "id": "dfed8a66-7312-40b1-9464-606943f22f93",
      "name": "Edit Fields1"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://nkxxsqytgayjvnkwgrul.supabase.co/rest/v1/media_files?on_conflict=path",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Prefer",
              "value": "resolution=merge-duplicates,return=representation"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{$json}}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1180,
        660
      ],
      "id": "394a152a-e831-483d-bb79-b53150b34ec8",
      "name": "HTTP Request3",
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $('Set File ID1').item.json.file_type }}",
                    "rightValue": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    },
                    "id": "9ae327be-71cb-4e63-98f5-957d214d51f4"
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "3eb3fbc0-1d29-431f-85b2-1d7e508d6b50",
                    "leftValue": "={{ $('Set File ID1').item.json.file_type }}",
                    "rightValue": "application/pdf",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "89a6f68e-9b80-4b7f-b437-942ed49da3df",
                    "leftValue": "={{ $('Set File ID1').item.json.file_type }}",
                    "rightValue": "gif",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "0981fd2c-98a3-47f6-b344-c23304302e60",
                    "leftValue": "={{ $('Set File ID1').item.json.file_type }}",
                    "rightValue": "png",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "779b4d36-9338-4609-b5ab-9e886275420f",
                    "leftValue": "={{ $('Set File ID1').item.json.file_type }}",
                    "rightValue": "jpg",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        980,
        240
      ],
      "id": "8d0811a1-468f-4be5-b355-38e33a866768",
      "name": "Switch2"
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        -280,
        260
      ],
      "id": "f8809d4b-e6f7-4333-b10f-4e938dbe06c4",
      "name": "Telegram Trigger1",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.message.document.mime_type }}",
                    "rightValue": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "9eadf6d7-f703-48cf-84b0-4eab4e8596de"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Docx"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "8c844924-b2ed-48b0-935c-c66a8fd0c778",
                    "leftValue": "={{ $json.message.document.mime_type }}",
                    "rightValue": "application/pdf",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "pdf"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "1d8a98b9-3313-41fe-972d-47cd8c00cfb8",
                    "leftValue": "={{ $json.message.photo }}",
                    "rightValue": "",
                    "operator": {
                      "type": "array",
                      "operation": "exists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "image"
            }
          ]
        },
        "options": {}
      },
      "id": "4e6dd924-4be6-4289-92ca-23103f5f726b",
      "name": "Switch4",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        -240,
        520
      ]
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.message.document.thumbnail.file_id }}"
      },
      "id": "d6dfe355-1a0d-4c23-a87e-5823c2e83b42",
      "name": "Download PDF",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -60,
        520
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.message.photo[1].file_id }}"
      },
      "id": "87fd575b-b127-4119-90f0-ceb0f3e88cec",
      "name": "Download Image",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -60,
        700
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.message.document.file_id }}"
      },
      "id": "9316db9d-5a1d-431b-89df-3bb9737dcd80",
      "name": "Download DOCX",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        -60,
        360
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 0,
  "updatedAt": "2025-07-06T10:23:53.000Z",
  "versionId": "9d1d6062-9cc8-460b-8b93-4ac969b35fe9"
}