{
  "id": "6D7Qa7LGmBt8ka3g",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Telegram Smart File Manager (Drive + OCR Integration)",
  "tags": [],
  "nodes": [
    {
      "id": "b1414d19-9d49-480b-be77-6d8709c86919",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "notes": "Receives all incoming messages and documents from Telegram bot",
      "position": [
        -1264,
        272
      ],
      "parameters": {
        "updates": [
          "message",
          "edited_message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "ae8100e3-c852-48a3-a564-f8d1524439cc",
      "name": "Check if File/Image",
      "type": "n8n-nodes-base.if",
      "notes": "Filters messages to process only documents and images",
      "position": [
        -1040,
        272
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "condition-001",
              "operator": {
                "type": "object",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.message.document }}",
              "rightValue": ""
            },
            {
              "id": "condition-002",
              "operator": {
                "type": "object",
                "operation": "exists"
              },
              "leftValue": "={{ $json.message.photo }}",
              "rightValue": "",
              "combineOperation": "or"
            }
          ],
          "combineOperation": "any"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "0c28e059-5e63-4107-b8c9-02074ed7b7ca",
      "name": "Sticky: Check File",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1104,
        144
      ],
      "parameters": {
        "color": 3,
        "width": 200,
        "height": 288,
        "content": "\ud83d\udd0d **Filter**: Only proceed if message has file or image"
      },
      "typeVersion": 1
    },
    {
      "id": "5c25a662-adbc-44cb-a3ef-678aff284c6d",
      "name": "Extract File Metadata",
      "type": "n8n-nodes-base.set",
      "notes": "Extracts file ID, name, type and user info for processing",
      "position": [
        -816,
        160
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "assign-001",
              "name": "fileId",
              "type": "string",
              "value": "={{ $json.message.document?.file_id || $json.message.photo?.slice(-1)[0].file_id }}"
            },
            {
              "id": "assign-002",
              "name": "fileName",
              "type": "string",
              "value": "={{ $json.message.document?.file_name || 'image_' + $json.message.date + '.jpg' }}"
            },
            {
              "id": "assign-003",
              "name": "mimeType",
              "type": "string",
              "value": "={{ $json.message.document?.mime_type || 'image/jpeg' }}"
            },
            {
              "id": "assign-004",
              "name": "chatId",
              "type": "number",
              "value": "={{ $json.message.chat.id }}"
            },
            {
              "id": "assign-005",
              "name": "userName",
              "type": "string",
              "value": "={{ $json.message.from.first_name }} {{ $json.message.from.last_name || '' }}"
            }
          ]
        }
      },
      "typeVersion": 3.3
    },
    {
      "id": "5007396a-0dcf-4c74-ac08-eab12479ef61",
      "name": "Sticky: Extract Metadata",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -880,
        0
      ],
      "parameters": {
        "color": 2,
        "width": 210,
        "height": 304,
        "content": "\ud83d\udccb **Extract**: fileId, name, MIME, chat & user info"
      },
      "typeVersion": 1
    },
    {
      "id": "79240107-39e4-4185-9cd2-d015b2fd2a39",
      "name": "Download File from Telegram",
      "type": "n8n-nodes-base.telegram",
      "notes": "Downloads the actual file content from Telegram servers",
      "position": [
        -608,
        160
      ],
      "parameters": {
        "fileId": "qwryuu75ewrjh",
        "resource": "file",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "76a9187c-768d-40d9-97b0-4e93d2b09e40",
      "name": "Sticky: Download File",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -656,
        -16
      ],
      "parameters": {
        "width": 220,
        "height": 320,
        "content": "\u2b07\ufe0f **Download**: Get file binary from Telegram API"
      },
      "typeVersion": 1
    },
    {
      "id": "b1a86b46-e69b-41cb-90b4-8c9bef5e4e59",
      "name": "Upload to Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "notes": "Uploads file to Google Drive and returns Drive file ID",
      "position": [
        -384,
        160
      ],
      "parameters": {
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "/ (Root folder)"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "5695fd86-ddd8-40a1-ad2d-90debbc441bb",
      "name": "Sticky: Upload to Drive",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -432,
        -16
      ],
      "parameters": {
        "color": 5,
        "width": 220,
        "height": 320,
        "content": "\u2601\ufe0f **Upload**: Save file to Google Drive root"
      },
      "typeVersion": 1
    },
    {
      "id": "399a6769-627f-40b8-b2ea-c82a8c32eea9",
      "name": "Check if OCR Eligible",
      "type": "n8n-nodes-base.if",
      "notes": "Determines if file type supports OCR (images and PDFs)",
      "position": [
        -160,
        160
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "ocr-condition-001",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              },
              "leftValue": "={{ $json.mimeType }}",
              "rightValue": "image/"
            },
            {
              "id": "ocr-condition-002",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.mimeType }}",
              "rightValue": "application/pdf",
              "combineOperation": "or"
            }
          ],
          "combineOperation": "any"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "4fbd99c5-1138-41e6-ac76-1e37b179330e",
      "name": "Sticky: OCR Eligible",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -224,
        -16
      ],
      "parameters": {
        "color": 3,
        "width": 200,
        "height": 320,
        "content": "\ud83e\udde0 **OCR Check**: Is it image or PDF?"
      },
      "typeVersion": 1
    },
    {
      "id": "c0310c17-5f48-4646-ae7e-6bbef0ef4504",
      "name": "Google Vision OCR",
      "type": "n8n-nodes-base.httpRequest",
      "notes": "Performs OCR using Google Vision API to extract text from images",
      "position": [
        64,
        64
      ],
      "parameters": {
        "url": "=https://vision.googleapis.com/v1/images:annotate",
        "options": {},
        "sendBody": true,
        "authentication": "oAuth2",
        "bodyParameters": {
          "parameters": [
            {
              "name": "requests",
              "value": "=[{\"image\":{\"content\":\"{{ $('Download File from Telegram').item.binary.data.data }}\"},\"features\":[{\"type\":\"TEXT_DETECTION\"}]}]"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "75dd9b9d-a291-4122-a6cd-98a09dc88fe7",
      "name": "Sticky: Vision OCR",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        -80
      ],
      "parameters": {
        "color": 6,
        "width": 220,
        "height": 320,
        "content": "\ud83d\udd0d **OCR**: Extract text via Google Vision API"
      },
      "typeVersion": 1
    },
    {
      "id": "352815cf-6eda-4ac2-8e1e-de2f04975c56",
      "name": "Extract OCR Text",
      "type": "n8n-nodes-base.set",
      "notes": "Parses OCR results and extracts detected text content",
      "position": [
        272,
        64
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "extract-text-001",
              "name": "extractedText",
              "type": "string",
              "value": "={{ $json.responses[0]?.fullTextAnnotation?.text || 'No text detected' }}"
            },
            {
              "id": "extract-text-002",
              "name": "confidence",
              "type": "string",
              "value": "={{ $json.responses[0]?.fullTextAnnotation ? 'High' : 'None' }}"
            }
          ]
        }
      },
      "typeVersion": 3.3
    },
    {
      "id": "9ada9e69-ac35-4b83-a028-3f1f3e182cf0",
      "name": "Sticky: Extract OCR Text",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        224,
        -160
      ],
      "parameters": {
        "color": 2,
        "width": 220,
        "height": 352,
        "content": "\u2702\ufe0f **Parse**: Get fullTextAnnotation from OCR response"
      },
      "typeVersion": 1
    },
    {
      "id": "6d4867d7-32ef-4b1a-b219-fbbe4a06b2a0",
      "name": "No OCR Needed",
      "type": "n8n-nodes-base.set",
      "notes": "Sets default values for files that don't need OCR processing",
      "position": [
        64,
        272
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "no-ocr-001",
              "name": "extractedText",
              "type": "string",
              "value": "Not applicable for this file type"
            },
            {
              "id": "no-ocr-002",
              "name": "confidence",
              "type": "string",
              "value": "N/A"
            }
          ]
        }
      },
      "typeVersion": 3.3
    },
    {
      "id": "e3179bd4-e6d9-4931-8133-d9856a2ddbf2",
      "name": "Sticky: No OCR",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        400
      ],
      "parameters": {
        "color": 7,
        "width": 200,
        "height": 240,
        "content": "\u2796 **Skip OCR**: Non-image/PDF files"
      },
      "typeVersion": 1
    },
    {
      "id": "d4cbfc85-2c1e-4b18-92ce-9d245caefc77",
      "name": "Merge OCR Paths",
      "type": "n8n-nodes-base.merge",
      "notes": "Combines results from OCR and non-OCR processing paths",
      "position": [
        400,
        160
      ],
      "parameters": {},
      "typeVersion": 2.1
    },
    {
      "id": "ba51bc68-77df-4727-9bb3-11febae993cc",
      "name": "Index in Airtable",
      "type": "n8n-nodes-base.airtable",
      "notes": "Stores file metadata and extracted text in searchable Airtable database",
      "position": [
        624,
        160
      ],
      "parameters": {
        "base": {
          "__rl": true,
          "mode": "id",
          "value": "juu7654retyu"
        },
        "table": {
          "__rl": true,
          "mode": "id",
          "value": "6ikkiu654erfghyt54321q"
        },
        "columns": {
          "value": {},
          "schema": [],
          "mappingMode": "autoMapInputData",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "create"
      },
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "838b0f5b-987d-4d35-b03e-e21e71bd8183",
      "name": "Sticky: Airtable Index",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        544,
        80
      ],
      "parameters": {
        "color": 8,
        "width": 230,
        "height": 224,
        "content": "\ud83d\udcca **Index**: Save metadata + OCR text to Airtable"
      },
      "typeVersion": 1
    },
    {
      "id": "aa68c4ad-3bbc-4d71-b822-7a4eb988eba2",
      "name": "Send Success Message",
      "type": "n8n-nodes-base.telegram",
      "notes": "Sends confirmation message to user with file details and preview",
      "position": [
        848,
        160
      ],
      "parameters": {
        "text": "=\u2705 **File Uploaded Successfully!**\n\n\ud83d\udcc1 **File:** {{ $json.fileName }}\n\ud83d\udd17 **Drive Link:** {{ $('Upload to Google Drive').item.json.webViewLink }}\n\ud83d\udcdd **Extracted Text:** {{ $json.extractedText.substring(0, 200) }}{{ $json.extractedText.length > 200 ? '...' : '' }}\n\u23f0 **Uploaded:** {{ $now.format('yyyy-MM-dd HH:mm') }}",
        "chatId": "={{ $json.chatId }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "5a85d261-8535-4024-aa0a-d13508ab68b0",
      "name": "Sticky: Success Msg",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        800,
        16
      ],
      "parameters": {
        "color": 4,
        "width": 220,
        "height": 288,
        "content": "\u2705 **Notify**: Success message with link & preview"
      },
      "typeVersion": 1
    },
    {
      "id": "247f8290-8372-41b6-af93-d50004d49a27",
      "name": "Send Error Message",
      "type": "n8n-nodes-base.telegram",
      "notes": "Notifies user if any error occurs during processing",
      "position": [
        848,
        368
      ],
      "parameters": {
        "text": "\u274c **Error Processing File**\n\nSorry, something went wrong while processing your file. Please try again later.",
        "chatId": "={{ $json.chatId }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "6c943962-95b5-40c0-8492-daa5a5421bbf",
      "name": "Sticky: Error Msg",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        768,
        528
      ],
      "parameters": {
        "width": 200,
        "height": 70,
        "content": "\u274c **Error**: Notify user on failure"
      },
      "typeVersion": 1
    },
    {
      "id": "d645926d-5289-4878-b6fd-12d6e7b0490b",
      "name": "Check Search Command",
      "type": "n8n-nodes-base.if",
      "notes": "Detects if user wants to search indexed files using /search command",
      "position": [
        -816,
        464
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "search-condition-001",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              },
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "/search "
            }
          ],
          "combineOperation": "any"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "9328c7e3-6c2b-4d0c-897f-0cbf92c61ce8",
      "name": "Sticky: Search Cmd",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -944,
        592
      ],
      "parameters": {
        "color": 3,
        "width": 200,
        "height": 70,
        "content": "\ud83d\udd0e **Detect**: /search <query> command"
      },
      "typeVersion": 1
    },
    {
      "id": "56783c48-c377-4415-90b2-66f6793196ef",
      "name": "Extract Search Query",
      "type": "n8n-nodes-base.set",
      "notes": "Extracts the search keywords from user's message",
      "position": [
        -608,
        464
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "search-query-001",
              "name": "searchQuery",
              "type": "string",
              "value": "={{ $json.message.text.replace('/search ', '').trim() }}"
            },
            {
              "id": "search-chat-001",
              "name": "chatId",
              "type": "number",
              "value": "={{ $json.message.chat.id }}"
            }
          ]
        }
      },
      "typeVersion": 3.3
    },
    {
      "id": "6da1bc77-fe2b-4d7d-af2f-96956d2ada79",
      "name": "Sticky: Extract Query",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -656,
        592
      ],
      "parameters": {
        "color": 2,
        "width": 210,
        "height": 70,
        "content": "\u2702\ufe0f **Parse**: Extract query after /search"
      },
      "typeVersion": 1
    },
    {
      "id": "2a8429f9-89fe-4387-872d-fb6bc4702eac",
      "name": "Search in Index",
      "type": "n8n-nodes-base.airtable",
      "notes": "Searches indexed files for matching text content",
      "position": [
        -384,
        464
      ],
      "parameters": {
        "base": {
          "__rl": true,
          "mode": "id",
          "value": "vbhytrewsxcv"
        },
        "table": {
          "__rl": true,
          "mode": "id",
          "value": "vcde456yuhgfdsz"
        },
        "options": {},
        "operation": "search",
        "filterByFormula": "=SEARCH(LOWER(\"{{ $json.searchQuery }}\"), LOWER({extracted_text}))"
      },
      "credentials": {
        "airtableTokenApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "e82131ab-bb70-41d1-a904-d0d088fb3c53",
      "name": "Sticky: Search Index",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -480,
        384
      ],
      "parameters": {
        "color": 6,
        "width": 230,
        "height": 70,
        "content": "\ud83d\udd0d **Search**: Airtable full-text in extracted_text"
      },
      "typeVersion": 1
    },
    {
      "id": "81969e62-0c4b-43e4-ab04-d6d4c9978f7c",
      "name": "Send Search Results",
      "type": "n8n-nodes-base.telegram",
      "notes": "Returns search results with file links to user",
      "position": [
        -160,
        464
      ],
      "parameters": {
        "text": "=\ud83d\udd0d **Search Results for:** \"{{ $json.searchQuery }}\"\n\n{{ $('Search in Index').all().map((item, i) => `${i + 1}. **${item.json.fields.file_name}**\\n\ud83d\udd17 ${item.json.fields.drive_file_url}\\n\ud83d\udcdd Preview: ${item.json.fields.extracted_text?.substring(0, 100)}...\\n`).join('\\n') || 'No results found.' }}",
        "chatId": "={{ $json.chatId }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "d7cafa5b-c77f-4cc7-bc80-36491aa20d4e",
      "name": "Sticky: Search Results",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -224,
        384
      ],
      "parameters": {
        "color": 4,
        "width": 220,
        "height": 70,
        "content": "\ud83d\udccb **Results**: Send matched files with previews"
      },
      "typeVersion": 1
    },
    {
      "id": "3896a4e4-efa1-42af-af9b-d1954ac06471",
      "name": "Sticky: Trigger",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1344,
        192
      ],
      "parameters": {
        "color": 4,
        "width": 220,
        "height": 224,
        "content": "\ud83c\udfaf **Trigger**: Listens for new messages/documents from users"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "32fa1b16-0531-459e-a037-441d674eb57f",
  "connections": {
    "No OCR Needed": {
      "main": [
        [
          {
            "node": "Merge OCR Paths",
            "type": "main",
            "index": 1
          },
          {
            "node": "Send Error Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge OCR Paths": {
      "main": [
        [
          {
            "node": "Index in Airtable",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search in Index": {
      "main": [
        [
          {
            "node": "Send Search Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract OCR Text": {
      "main": [
        [
          {
            "node": "Merge OCR Paths",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Check if File/Image",
            "type": "main",
            "index": 0
          },
          {
            "node": "Check Search Command",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Vision OCR": {
      "main": [
        [
          {
            "node": "Extract OCR Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Index in Airtable": {
      "main": [
        [
          {
            "node": "Send Success Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if File/Image": {
      "main": [
        [
          {
            "node": "Extract File Metadata",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Search Command": {
      "main": [
        [
          {
            "node": "Extract Search Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Search Query": {
      "main": [
        [
          {
            "node": "Search in Index",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if OCR Eligible": {
      "main": [
        [
          {
            "node": "Google Vision OCR",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No OCR Needed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract File Metadata": {
      "main": [
        [
          {
            "node": "Download File from Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload to Google Drive": {
      "main": [
        [
          {
            "node": "Check if OCR Eligible",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download File from Telegram": {
      "main": [
        [
          {
            "node": "Upload to Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}