Files
Alpha/workflows/ZX5rLSETg6Xcymps.json

857 lines
24 KiB
JSON

{
"id": "ZX5rLSETg6Xcymps",
"name": "📄 Paperless — Upload Documento [Telegram]",
"nodes": [
{
"id": "n01",
"name": "Telegram Trigger",
"type": "n8n-nodes-base.telegramTrigger",
"typeVersion": 1.1,
"position": [
-2000,
0
],
"parameters": {
"updates": [
"message"
],
"additionalFields": {}
},
"credentials": {
"telegramApi": {
"id": "uTXHLqcCJxbOvqN3",
"name": "Telegram account"
}
},
"webhookId": "tg-documento-v3"
},
{
"id": "n02",
"name": "Check Caption",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-1750,
0
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const msg = $input.first().json.message;\nconst doc = msg.document;\nif (!doc) return [];\nconst caption = (msg.caption || '').trim();\nif (!caption.toLowerCase().startsWith('documento')) return [];\nreturn [{ json: {\n file_id: doc.file_id,\n filename: doc.file_name || 'documento.pdf',\n mime_type: doc.mime_type || 'application/pdf',\n caption,\n chat_id: String(msg.chat.id),\n}}];"
}
},
{
"id": "n03new",
"name": "Telegram - Scarica File",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1.2,
"position": [
-1500,
0
],
"parameters": {
"resource": "file",
"operation": "get",
"fileId": "={{ $json.file_id }}",
"download": true,
"binaryProperty": "attachment",
"additionalFields": {}
},
"credentials": {
"telegramApi": {
"id": "uTXHLqcCJxbOvqN3",
"name": "Telegram account"
}
}
},
{
"id": "n05",
"name": "FileWizard - Avvia OCR",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-1000,
0
],
"parameters": {
"method": "POST",
"url": "http://filewizard.home.svc.cluster.local:8000/ocr-pdf",
"sendBody": true,
"contentType": "multipart-form-data",
"bodyParameters": {
"parameters": [
{
"parameterType": "formBinaryData",
"name": "file",
"inputDataFieldName": "data"
}
]
},
"options": {}
}
},
{
"id": "n06",
"name": "⏳ Attendi OCR",
"type": "n8n-nodes-base.wait",
"typeVersion": 1.1,
"position": [
-750,
0
],
"parameters": {
"amount": 25,
"unit": "seconds"
},
"webhookId": "wait-ocr-tg"
},
{
"id": "n07",
"name": "FileWizard - Stato OCR",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-500,
0
],
"parameters": {
"method": "GET",
"url": "=http://filewizard.home.svc.cluster.local:8000/job/{{ $json.job_id }}",
"options": {}
}
},
{
"id": "n08",
"name": "Paperless - Corrispondenti",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-250,
0
],
"parameters": {
"method": "GET",
"url": "https://docs.mt-home.uk/api/correspondents/?page_size=50",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"options": {}
},
"credentials": {
"httpHeaderAuth": {
"id": "uvGjLbrN5yQTQIzv",
"name": "Paperless-NGX API"
}
}
},
{
"id": "n09",
"name": "Paperless - Tipi Doc",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
0,
0
],
"parameters": {
"method": "GET",
"url": "https://docs.mt-home.uk/api/document_types/?page_size=50",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"options": {}
},
"credentials": {
"httpHeaderAuth": {
"id": "uvGjLbrN5yQTQIzv",
"name": "Paperless-NGX API"
}
}
},
{
"id": "n10",
"name": "Paperless - Tag",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
250,
0
],
"parameters": {
"method": "GET",
"url": "https://docs.mt-home.uk/api/tags/?page_size=50",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"options": {}
},
"credentials": {
"httpHeaderAuth": {
"id": "uvGjLbrN5yQTQIzv",
"name": "Paperless-NGX API"
}
}
},
{
"id": "n11",
"name": "Paperless - Percorsi",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
500,
0
],
"parameters": {
"method": "GET",
"url": "https://docs.mt-home.uk/api/storage_paths/?page_size=50",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"options": {}
},
"credentials": {
"httpHeaderAuth": {
"id": "uvGjLbrN5yQTQIzv",
"name": "Paperless-NGX API"
}
}
},
{
"id": "n12",
"name": "Build Prompt",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
750,
0
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const fi = $('Check Caption').first().json;\nconst ocr = ($('FileWizard - Stato OCR').first().json.result_preview || '').substring(0, 2500);\nconst C = ($('Paperless - Corrispondenti').first().json.results || []).map(c => '[' + c.id + '] ' + c.name + ' (' + (c.document_count||0) + ' doc)').join('\\n');\nconst D = ($('Paperless - Tipi Doc').first().json.results || []).map(d => '[' + d.id + '] ' + d.name).join('\\n');\nconst T = ($('Paperless - Tag').first().json.results || []).map(t => '[' + t.id + '] ' + t.name).join('\\n');\nconst P = ($('Paperless - Percorsi').first().json.results || []).map(s => '[' + s.id + '] ' + s.name).join('\\n');\nconst prompt = `Sei l'assistente di Martin che gestisce Paperless-NGX.\nHa inviato un PDF via Telegram con caption: \"${fi.caption}\".\nNome file: ${fi.filename}\n\nTESTO ESTRATTO DAL PDF (OCR):\n${ocr}\n\nCORRISPONDENTI DISPONIBILI:\n${C}\n\nTIPI DOCUMENTO:\n${D}\n\nTAG:\n${T}\n\nPERCORSI:\n${P}\n\nISTRUZIONI:\n1. Se il documento non corrisponde a nessun tipo/tag esistente: skip:true con motivazione\n2. Altrimenti inferisci i metadati dal testo OCR (importo, periodo, data emissione...)\n3. Titolo descrittivo con dettagli concreti (es: 'E.ON Bolletta Energia Mar2026 87.50EUR')\n4. created_date = data emissione dal documento (non oggi)\n\nRispondi SOLO JSON valido:\n{\\\"skip\\\":false,\\\"skip_reason\\\":\\\"\\\",\\\"correspondent_id\\\":44,\\\"document_type_id\\\":2,\\\"tag_ids\\\":[3],\\\"storage_path_id\\\":2,\\\"title\\\":\\\"...\\\",\\\"created_date\\\":\\\"2026-03-01\\\",\\\"confidence_note\\\":\\\"...\\\"}`;\nreturn [{ json: { prompt, filename: fi.filename, caption: fi.caption }}];"
}
},
{
"id": "n13",
"name": "Ottieni Token Copilot",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
1000,
0
],
"parameters": {
"method": "GET",
"url": "https://api.github.com/copilot_internal/v2/token",
"options": {},
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth"
},
"credentials": {
"httpHeaderAuth": {
"id": "vBwUxlzKrX3oDHyN",
"name": "GitHub Copilot OAuth Token"
}
}
},
{
"id": "n14",
"name": "GPT-4.1 - Inferisci Metadati",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
1250,
0
],
"parameters": {
"method": "POST",
"url": "https://api.githubcopilot.com/chat/completions",
"authentication": "none",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "Authorization",
"value": "={{ 'Bearer ' + $('Ottieni Token Copilot').first().json.token }}"
},
{
"name": "Content-Type",
"value": "application/json"
},
{
"name": "Copilot-Integration-Id",
"value": "vscode-chat"
},
{
"name": "Editor-Version",
"value": "vscode/1.85.0"
}
]
},
"sendBody": true,
"contentType": "raw",
"rawContentType": "application/json",
"body": "={{ JSON.stringify({\"model\":\"gpt-4.1\",\"messages\":[{\"role\":\"system\",\"content\":\"Rispondi SOLO con JSON valido.\"},{\"role\":\"user\",\"content\": $('Build Prompt').first().json.prompt}],\"response_format\":{\"type\":\"json_object\"},\"max_tokens\":1024}) }}",
"options": {}
}
},
{
"id": "n15",
"name": "Parse Risposta GPT",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
1500,
0
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const raw = $input.first().json;\nconst content = (raw.choices || [])[0]?.message?.content || '{}';\nlet meta;\ntry { meta = JSON.parse(content); } catch(e) { throw new Error('GPT non JSON: ' + content.substring(0,300)); }\nconst fi = $('Check Caption').first().json;\nreturn [{ json: { ...meta, filename: fi.filename, caption: fi.caption }}];"
}
},
{
"id": "n16",
"name": "Skip?",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
1750,
0
],
"parameters": {
"conditions": {
"options": {},
"combinator": "and",
"conditions": [
{
"id": "s1",
"leftValue": "={{ $json.skip }}",
"operator": {
"type": "boolean",
"operation": "true"
},
"rightValue": ""
}
]
}
}
},
{
"id": "n17",
"name": "Telegram - Documento Non Riconosciuto",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1.2,
"position": [
1980,
140
],
"parameters": {
"chatId": "-4814221197",
"text": "={{ '⚠️ *Documento non archiviato*\\n\\n' + $json.skip_reason + '\\n\\nCaricalo manualmente su Paperless se necessario.' }}",
"additionalFields": {
"parse_mode": "Markdown"
}
},
"credentials": {
"telegramApi": {
"id": "uTXHLqcCJxbOvqN3",
"name": "Telegram account"
}
}
},
{
"id": "n18",
"name": "Prepara Upload",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
1980,
-140
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const meta = $('Parse Risposta GPT').first().json;\nconst dl = $('Telegram - Scarica File').first();\nconst filename = meta.filename || 'documento.pdf';\nconst titleFromFilename = filename.replace(/\\.[^/.]+$/, '');\nlet tag_ids = meta.tag_ids;\nif (typeof tag_ids === 'string') { try { tag_ids = JSON.parse(tag_ids); } catch(e) { tag_ids = []; } }\nconst created_date = meta.created_date && meta.created_date !== 'None'\n ? String(meta.created_date).substring(0, 10)\n : new Date().toISOString().substring(0, 10);\nreturn [{ json: {\n title: meta.title || titleFromFilename,\n upload_title: titleFromFilename,\n filename,\n correspondent_id: meta.correspondent_id != null ? Number(meta.correspondent_id) : null,\n document_type_id: meta.document_type_id != null ? Number(meta.document_type_id) : null,\n storage_path_id: meta.storage_path_id != null ? Number(meta.storage_path_id) : null,\n tag_ids: Array.isArray(tag_ids) ? tag_ids.map(Number).filter(n => !isNaN(n)) : [],\n created_date,\n}, binary: dl.binary }];"
}
},
{
"id": "n19",
"name": "Paperless - Upload Documento",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
2220,
-140
],
"parameters": {
"method": "POST",
"url": "https://docs.mt-home.uk/api/documents/post_document/",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"sendBody": true,
"contentType": "multipart-form-data",
"bodyParameters": {
"parameters": [
{
"parameterType": "formBinaryData",
"name": "document",
"inputDataFieldName": "data"
},
{
"name": "title",
"value": "={{ $json.upload_title }}"
}
]
},
"options": {
"response": {
"response": {
"responseFormat": "text"
}
}
}
},
"credentials": {
"httpHeaderAuth": {
"id": "uvGjLbrN5yQTQIzv",
"name": "Paperless-NGX API"
}
}
},
{
"id": "n20",
"name": "Estrai Task ID",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
2440,
-140
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const raw = $input.first().json;\nconst task_id = (raw.data || '').replace(/\"/g, '').trim();\nconst meta = $('Prepara Upload').first().json;\nlet tag_ids = meta.tag_ids;\nif (typeof tag_ids === 'string') { try { tag_ids = JSON.parse(tag_ids); } catch(e) { tag_ids = []; } }\nreturn [{ json: { task_id, ...meta, tag_ids } }];"
}
},
{
"id": "n21",
"name": "⏳ Attendi Paperless",
"type": "n8n-nodes-base.wait",
"typeVersion": 1.1,
"position": [
2660,
-140
],
"parameters": {
"amount": 20,
"unit": "seconds"
},
"webhookId": "wait-pl-documento"
},
{
"id": "n22",
"name": "Paperless - Stato Task",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
2880,
-140
],
"parameters": {
"method": "GET",
"url": "https://docs.mt-home.uk/api/tasks/",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"sendQuery": true,
"queryParameters": {
"parameters": [
{
"name": "task_id",
"value": "={{ $json.task_id }}"
}
]
},
"options": {}
},
"credentials": {
"httpHeaderAuth": {
"id": "uvGjLbrN5yQTQIzv",
"name": "Paperless-NGX API"
}
}
},
{
"id": "n23",
"name": "Estrai Document ID",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
3100,
-140
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const tasks = $input.first().json;\nconst task = Array.isArray(tasks) ? tasks[0] : tasks;\nif (!task) throw new Error('Nessun task');\nconst isDup = task.status === 'FAILURE' && String(task.result || '').includes('duplicate');\nif (!isDup && task.status !== 'SUCCESS') throw new Error('Task ' + task.status + ': ' + (task.result || ''));\nconst doc_id = parseInt(String(task.related_document || '').replace(/\\D/g, ''), 10);\nif (!doc_id) throw new Error('Doc ID non trovato: ' + JSON.stringify(task));\nconst meta = $('Prepara Upload').first().json;\nlet tag_ids = meta.tag_ids;\nif (typeof tag_ids === 'string') { try { tag_ids = JSON.parse(tag_ids); } catch(e) { tag_ids = []; } }\nreturn [{ json: {\n doc_id, is_duplicate: isDup, title: meta.title,\n correspondent_id: meta.correspondent_id != null ? Number(meta.correspondent_id) : null,\n document_type_id: meta.document_type_id != null ? Number(meta.document_type_id) : null,\n storage_path_id: meta.storage_path_id != null ? Number(meta.storage_path_id) : null,\n tag_ids: Array.isArray(tag_ids) ? tag_ids.map(Number).filter(n => !isNaN(n)) : [],\n created_date: meta.created_date ? String(meta.created_date).substring(0, 10) : null,\n} }];"
}
},
{
"id": "n24",
"name": "Paperless - Patch Metadati",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
3320,
-140
],
"parameters": {
"method": "PATCH",
"url": "=https://docs.mt-home.uk/api/documents/{{ $json.doc_id }}/",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify(Object.fromEntries(Object.entries({correspondent:$json.correspondent_id,document_type:$json.document_type_id,storage_path:$json.storage_path_id,tags:$json.tag_ids&&$json.tag_ids.length>0?$json.tag_ids:undefined,created:$json.created_date||undefined}).filter(([_,v])=>v!==null&&v!==undefined))) }}",
"options": {}
},
"credentials": {
"httpHeaderAuth": {
"id": "uvGjLbrN5yQTQIzv",
"name": "Paperless-NGX API"
}
}
},
{
"id": "n25",
"name": "Telegram - Conferma Upload",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1.2,
"position": [
3540,
-140
],
"parameters": {
"chatId": "-4814221197",
"text": "={{ '✅ *Documento archiviato su Paperless!*\\n\\n📄 *' + $('Prepara Upload').first().json.filename + '*\\n🗂 ' + $('Prepara Upload').first().json.title + '\\n' + ($('Estrai Document ID').first().json.is_duplicate ? '⚠️ _Duplicato — metadati aggiornati_\\n' : '') + '🔗 https://docs.mt-home.uk/documents/' + $json.doc_id + '/details/' }}",
"additionalFields": {
"parse_mode": "Markdown"
}
},
"credentials": {
"telegramApi": {
"id": "uTXHLqcCJxbOvqN3",
"name": "Telegram account"
}
}
},
{
"id": "n_fw_del",
"name": "FileWizard - Elimina File",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
3760,
-140
],
"parameters": {
"method": "POST",
"url": "http://filewizard.home.svc.cluster.local:8000/settings/delete-files",
"options": {}
}
},
{
"id": "n_fw_clr",
"name": "FileWizard - Pulisci Job",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
3980,
-140
],
"parameters": {
"method": "POST",
"url": "http://filewizard.home.svc.cluster.local:8000/settings/clear-history",
"options": {}
}
}
],
"connections": {
"Telegram Trigger": {
"main": [
[
{
"node": "Check Caption",
"type": "main",
"index": 0
}
]
]
},
"Check Caption": {
"main": [
[
{
"node": "Telegram - Scarica File",
"type": "main",
"index": 0
}
]
]
},
"Telegram - Scarica File": {
"main": [
[
{
"node": "FileWizard - Avvia OCR",
"type": "main",
"index": 0
}
]
]
},
"FileWizard - Avvia OCR": {
"main": [
[
{
"node": "⏳ Attendi OCR",
"type": "main",
"index": 0
}
]
]
},
"⏳ Attendi OCR": {
"main": [
[
{
"node": "FileWizard - Stato OCR",
"type": "main",
"index": 0
}
]
]
},
"FileWizard - Stato OCR": {
"main": [
[
{
"node": "Paperless - Corrispondenti",
"type": "main",
"index": 0
}
]
]
},
"Paperless - Corrispondenti": {
"main": [
[
{
"node": "Paperless - Tipi Doc",
"type": "main",
"index": 0
}
]
]
},
"Paperless - Tipi Doc": {
"main": [
[
{
"node": "Paperless - Tag",
"type": "main",
"index": 0
}
]
]
},
"Paperless - Tag": {
"main": [
[
{
"node": "Paperless - Percorsi",
"type": "main",
"index": 0
}
]
]
},
"Paperless - Percorsi": {
"main": [
[
{
"node": "Build Prompt",
"type": "main",
"index": 0
}
]
]
},
"Build Prompt": {
"main": [
[
{
"node": "Ottieni Token Copilot",
"type": "main",
"index": 0
}
]
]
},
"Ottieni Token Copilot": {
"main": [
[
{
"node": "GPT-4.1 - Inferisci Metadati",
"type": "main",
"index": 0
}
]
]
},
"GPT-4.1 - Inferisci Metadati": {
"main": [
[
{
"node": "Parse Risposta GPT",
"type": "main",
"index": 0
}
]
]
},
"Parse Risposta GPT": {
"main": [
[
{
"node": "Skip?",
"type": "main",
"index": 0
}
]
]
},
"Skip?": {
"main": [
[
{
"node": "Telegram - Documento Non Riconosciuto",
"type": "main",
"index": 0
}
],
[
{
"node": "Prepara Upload",
"type": "main",
"index": 0
}
]
]
},
"Prepara Upload": {
"main": [
[
{
"node": "Paperless - Upload Documento",
"type": "main",
"index": 0
}
]
]
},
"Paperless - Upload Documento": {
"main": [
[
{
"node": "Estrai Task ID",
"type": "main",
"index": 0
}
]
]
},
"Estrai Task ID": {
"main": [
[
{
"node": "⏳ Attendi Paperless",
"type": "main",
"index": 0
}
]
]
},
"⏳ Attendi Paperless": {
"main": [
[
{
"node": "Paperless - Stato Task",
"type": "main",
"index": 0
}
]
]
},
"Paperless - Stato Task": {
"main": [
[
{
"node": "Estrai Document ID",
"type": "main",
"index": 0
}
]
]
},
"Estrai Document ID": {
"main": [
[
{
"node": "Paperless - Patch Metadati",
"type": "main",
"index": 0
}
]
]
},
"Paperless - Patch Metadati": {
"main": [
[
{
"node": "Telegram - Conferma Upload",
"type": "main",
"index": 0
}
]
]
},
"Telegram - Conferma Upload": {
"main": [
[
{
"node": "FileWizard - Elimina File",
"type": "main",
"index": 0
}
]
]
},
"FileWizard - Elimina File": {
"main": [
[
{
"node": "FileWizard - Pulisci Job",
"type": "main",
"index": 0
}
]
]
}
},
"settings": {
"executionOrder": "v1",
"callerPolicy": "workflowsFromSameOwner",
"availableInMCP": false
},
"triggerCount": 1,
"versionId": "ebc247d9-6391-45c1-94f8-f73ded65ce0b",
"owner": {
"type": "personal",
"projectId": "Hdttz401OqqtObPo",
"projectName": "Martin Tahiraj <tahiraj.martin@gmail.com>",
"personalEmail": "tahiraj.martin@gmail.com"
},
"parentFolderId": null,
"isArchived": true
}