flows: credenziali e flusso di gmail digest

This commit is contained in:
2026-03-21 10:43:15 +00:00
parent e257a6795f
commit e4d523fce6
16 changed files with 1033 additions and 0 deletions

View File

@@ -0,0 +1,779 @@
{
"id": "1lIKvVJQIcva30YM",
"name": "📬 Gmail — Daily Digest [Schedule]",
"nodes": [
{
"parameters": {
"rule": {
"interval": [
{
"field": "hours",
"hoursInterval": 3
}
]
}
},
"id": "85c1a689-35df-4b58-bab4-478b11c880af",
"name": "⏰ Ogni 3 ore",
"type": "n8n-nodes-base.scheduleTrigger",
"typeVersion": 1.2,
"position": [
-1824,
48
]
},
{
"parameters": {
"resource": "label"
},
"id": "a3e97bde-9106-4254-b49d-a164b5fa8cfd",
"name": "Gmail - Leggi tutte le label",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2.2,
"position": [
-1600,
48
],
"webhookId": "9959425d-bdbb-4597-ad72-77ec25fcf49c",
"credentials": {
"gmailOAuth2": {
"id": "qvOikS6IF0H5khr8",
"name": "Gmail account"
}
}
},
{
"parameters": {
"aggregate": "aggregateAllItemData",
"destinationFieldName": "labels",
"options": {}
},
"id": "a7ae3604-c2e6-4182-b89e-c9667217e86f",
"name": "Aggrega Label",
"type": "n8n-nodes-base.aggregate",
"typeVersion": 1,
"position": [
-1376,
48
]
},
{
"parameters": {
"operation": "getAll",
"filters": {
"readStatus": "unread",
"q": "-label:Processed"
},
"limit": 20
},
"id": "b73f8915-1173-43a8-9f78-0c49fa471b7d",
"name": "Gmail - Fetch ultime 3h",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2.2,
"position": [
608,
160
],
"webhookId": "efc020f5-87c1-4009-8bc2-82ad371c0dde",
"credentials": {
"gmailOAuth2": {
"id": "qvOikS6IF0H5khr8",
"name": "Gmail account"
}
}
},
{
"parameters": {
"operation": "get",
"messageId": "={{ $json.id }}"
},
"id": "157667ee-6539-40c2-b826-d6abf7fe945c",
"name": "Gmail - Leggi contenuto",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2.2,
"position": [
-944,
48
],
"webhookId": "078abbbb-a75c-4507-8c0a-23196b1fef45",
"credentials": {
"gmailOAuth2": {
"id": "qvOikS6IF0H5khr8",
"name": "Gmail account"
}
}
},
{
"parameters": {
"aggregate": "aggregateAllItemData",
"destinationFieldName": "emails",
"options": {}
},
"id": "153d3836-9028-4838-8cdc-4281d6531cc5",
"name": "Aggrega Email",
"type": "n8n-nodes-base.aggregate",
"typeVersion": 1,
"position": [
-720,
48
]
},
{
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const item = $input.first();\nconst emailsRaw = item.json.emails || [];\nconst emails = emailsRaw.map(e => e.json || e);\n\nif (emails.length === 0) {\n return [{ json: { prompt: 'NESSUNA_EMAIL', emailCount: 0, emailMeta: [] } }];\n}\n\nconst emailMeta = emails.map(e => ({ id: e.id, threadId: e.threadId || e.id }));\n\nconst threads = {};\nemails.forEach(e => {\n const tid = e.threadId || e.id;\n if (!threads[tid]) threads[tid] = [];\n threads[tid].push(e.id);\n});\n\nconst emailList = emails.map((e, i) => {\n const from = e.From || e.from || 'Mittente sconosciuto';\n const subject = e.Subject || e.subject || '(nessun oggetto)';\n const date = e.Date || e.date || '';\n const body = (e.text || e.snippet || '').substring(0, 600);\n // Gmail node restituisce payload.mimeType='multipart/mixed' quando ci sono allegati\n const hasMixedPayload = e.payload && e.payload.mimeType === 'multipart/mixed';\n const hasAtt = (hasMixedPayload || (e.attachments && e.attachments.length > 0)) ? 'Sì (PDF probabile)' : 'No';\n const threadCount = (threads[e.threadId || e.id] || []).length;\n const threadNote = threadCount > 1 ? ` [THREAD: ${threadCount} msg]` : '';\n return `--- EMAIL ${i + 1}${threadNote} ---\nID: ${e.id}\nThreadID: ${e.threadId || e.id}\nDa: ${from}\nOggetto: ${subject}\nData: ${date}\nAllegati: ${hasAtt}\n${body}`;\n}).join('\\n\\n');\n\nconst today = new Date().toLocaleDateString('it-IT', {\n weekday: 'long', year: 'numeric', month: 'long', day: 'numeric'\n});\n\nconst prompt = `Sei l'assistente personale di Martin. Analizza ${emails.length} email non lette.\n\nLABEL GMAIL (nomi ESATTI, rispetta maiuscole e &):\nLavoro/Comunicazioni, Lavoro/Cedolino, Lavoro/Contratto\nCondominio/Comunicazioni, Condominio/Spese\nInternet/Bollette, Internet/Comunicazioni\nLuce&Gas/Bollette, Luce&Gas/Comunicazioni\nMarketing, Prenotazioni\nRicevute/Trasporti, Ricevute/Acquisti, Ricevute/Abbonamenti\n\nREGOLE CLASSIFICAZIONE:\n- action \"trash\" solo per Marketing/newsletter → verranno eliminate automaticamente\n- action \"keep\" per tutto il resto → etichettate e incluse nel report\n- EMAIL con stesso ThreadID = stessa conversazione: riassumile INSIEME nel report\n- Non usare label non presenti nella lista sopra\n\nPAPERLESS ACTION (campo aggiuntivo, guarda il campo Allegati):\n- paperless_action \"pdf_allegato\" → email con allegato PDF che potrebbe valere la pena archiviare (Allegati: Sì) — bollette, ricevute, contratti, cedolini, comunicazioni ufficiali, etc.\n- paperless_action null → email senza allegati PDF rilevanti, o allegati irrilevanti (immagini, firma, etc.)\n\nREPORT (daily_report) - OBBLIGATORIO essere narrativo:\n- Scrivi come un assistente personale che racconta a Martin cosa c'è nella sua inbox\n- NON fare semplice elenco mittente/oggetto: racconta cosa è successo\n- Raggruppa thread correlati in un unico punto\n- Estrai dettagli concreti: importi €, date, numeri ordine, azioni richieste\n- Segnala esplicitamente se Martin deve fare qualcosa\n- Ordine: urgente/da rispondere, poi info utili, poi ricevute, poi eliminati\n- Usa *grassetto* e emoji Telegram. Max 3000 caratteri.\n\nRispondi SOLO JSON valido:\n{\n \"emails\": [\n {\"id\": \"ID_ESATTO\", \"action\": \"trash\", \"labels\": [\"Marketing\"], \"summary\": \"\", \"paperless_action\": null},\n {\"id\": \"ID_ESATTO\", \"action\": \"keep\", \"labels\": [\"Luce&Gas/Bollette\"], \"summary\": \"E.ON €87,50 scad. 28/03\", \"paperless_action\": \"pdf_allegato\"},\n {\"id\": \"ID_ESATTO\", \"action\": \"keep\", \"labels\": [\"Internet/Bollette\"], \"summary\": \"Avviso bolletta Fastweb disponibile\", \"paperless_action\": null}\n ],\n \"daily_report\": \"[REPORT NARRATIVO]\"\n}\n\n${emailList}`;\n\nreturn [{ json: { prompt, emailCount: emails.length, emailMeta } }];"
},
"id": "f6b052dd-48da-41dd-8c68-f58c8b9f8c1d",
"name": "Costruisci Prompt",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-496,
48
]
},
{
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const raw = $input.first().json;\n// Formato OpenAI: choices[0].message.content\nconst rawText = (raw.choices && raw.choices[0] && raw.choices[0].message)\n ? raw.choices[0].message.content\n : (raw.text || raw.output || '');\n\nconst cleaned = rawText\n .replace(/<thinking>[\\s\\S]*?<\\/thinking>/gi, '')\n .replace(/```json\\n?/g, '')\n .replace(/```\\n?/g, '')\n .trim();\n\nlet parsed;\ntry {\n parsed = JSON.parse(cleaned);\n} catch (e) {\n const match = cleaned.match(/\\{[\\s\\S]*\\}/);\n if (match) {\n try { parsed = JSON.parse(match[0]); }\n catch (e2) { throw new Error('Parse error: ' + rawText.substring(0, 300)); }\n } else {\n throw new Error('No JSON found: ' + rawText.substring(0, 300));\n }\n}\n\nif (!parsed.emails || !Array.isArray(parsed.emails)) {\n throw new Error('Missing emails array: ' + JSON.stringify(parsed).substring(0, 200));\n}\n\nconst emailMeta = $('Costruisci Prompt').first().json.emailMeta || [];\nconst metaMap = {};\nemailMeta.forEach(m => { metaMap[m.id] = m.threadId; });\n\nparsed.emails = parsed.emails.map(e => ({\n ...e,\n threadId: metaMap[e.id] || e.id\n}));\n\nreturn [{ json: parsed }];"
},
"id": "cac1efac-2d3e-482c-a529-22bdd2136575",
"name": "Parse risposta GPT-4.1",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
-64,
48
]
},
{
"parameters": {
"chatId": "-4814221197",
"text": "={{ $json.daily_report }}",
"additionalFields": {
"parse_mode": "Markdown"
}
},
"id": "1855e5cf-9dfb-4b81-9f1a-72d2d8a546a0",
"name": "Telegram - Invia Report",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1.2,
"position": [
160,
-96
],
"webhookId": "d42335fd-4748-4406-b0dd-d8d7f4d0b027",
"credentials": {
"telegramApi": {
"id": "uTXHLqcCJxbOvqN3",
"name": "Telegram account"
}
}
},
{
"parameters": {
"fieldToSplitOut": "emails",
"options": {}
},
"id": "a0502f51-c43d-4792-8bf1-f5a95d592562",
"name": "Dividi Email",
"type": "n8n-nodes-base.splitOut",
"typeVersion": 1,
"position": [
160,
48
]
},
{
"parameters": {
"conditions": {
"string": [
{
"value1": "={{ $json.action }}",
"value2": "trash"
}
]
}
},
"id": "dec6cf2a-7254-4496-a24f-be4a0070eb0f",
"name": "È da eliminare?",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [
384,
48
]
},
{
"parameters": {
"operation": "markAsRead",
"messageId": "={{ $json.id }}"
},
"id": "3c5a7e25-08df-4620-afc7-fbbdf5e5730c",
"name": "Gmail - Segna come letta",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2.2,
"position": [
608,
-96
],
"webhookId": "abc3390b-9a83-4a40-bf36-d52a921a25b0",
"credentials": {
"gmailOAuth2": {
"id": "qvOikS6IF0H5khr8",
"name": "Gmail account"
}
}
},
{
"parameters": {
"resource": "thread",
"operation": "trash",
"threadId": "={{ $json.threadId }}"
},
"id": "e3fdcf0f-c55b-40d9-a99a-a665db028895",
"name": "Gmail - Sposta nel cestino",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2.2,
"position": [
832,
-96
],
"webhookId": "e0919d48-248a-4191-b8ca-6f9ff619bf16",
"credentials": {
"gmailOAuth2": {
"id": "qvOikS6IF0H5khr8",
"name": "Gmail account"
}
}
},
{
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "const items = $input.all();\nif (items.length === 0) return [];\n\n// Costruisce mappa nome → ID dalle label Gmail\nconst labelsData = $('Gmail - Leggi tutte le label').all();\nconst labelMap = {};\nlabelsData.forEach(item => {\n const name = item.json.name;\n const id = item.json.id;\n if (name && id) labelMap[name] = id;\n});\n\n// Processa ogni email nella branch \"keep\"\nconst result = [];\nfor (const item of items) {\n const email = item.json;\n const requestedLabels = Array.isArray(email.labels) ? email.labels : [];\n const resolvedLabelIds = requestedLabels\n .map(name => labelMap[name])\n .filter(id => !!id);\n\n // Skip se nessuna label trovata (evita errore Gmail 'No label add or removes')\n if (resolvedLabelIds.length === 0) continue;\n\n result.push({\n json: {\n id: String(email.id || ''),\n threadId: String(email.threadId || email.id || ''),\n action: String(email.action || 'keep'),\n labels: requestedLabels,\n summary: String(email.summary || ''),\n resolvedLabelIds: resolvedLabelIds\n }\n });\n}\n\nreturn result;"
},
"id": "99e0b420-3d9a-4ece-80ed-27dab248b018",
"name": "Risolvi ID Label",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
608,
192
]
},
{
"parameters": {
"operation": "addLabels",
"messageId": "={{ $json.id }}",
"labelIds": "={{ $json.resolvedLabelIds }}"
},
"id": "29b7cc48-03a5-45d3-a94a-dbeae0710f7b",
"name": "Gmail - Aggiungi Label",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2.2,
"position": [
832,
192
],
"webhookId": "debadb11-9b5c-44a6-9ad0-7f1a6598b14a",
"credentials": {
"gmailOAuth2": {
"id": "qvOikS6IF0H5khr8",
"name": "Gmail account"
}
}
},
{
"id": "test-webhook-trigger",
"name": "🧪 Test Webhook",
"type": "n8n-nodes-base.webhook",
"typeVersion": 2,
"position": [
-1824,
200
],
"parameters": {
"path": "gmail-digest-test",
"responseMode": "onReceived",
"options": {}
},
"webhookId": "gmail-digest-test-001"
},
{
"id": "copilot-gpt41-node",
"name": "GPT-4.1 - Classifica Email",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-336,
48
],
"parameters": {
"method": "POST",
"url": "https://api.githubcopilot.com/chat/completions",
"sendBody": true,
"options": {},
"genericAuthType": "httpHeaderAuth",
"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"
}
]
},
"authentication": "none",
"contentType": "raw",
"rawContentType": "application/json",
"body": "={{ JSON.stringify({\"model\":\"gpt-4.1\",\"messages\":[{\"role\":\"system\",\"content\":\"Rispondi SOLO con JSON valido.\"},{\"role\":\"user\",\"content\": $('Costruisci Prompt').first().json.prompt}],\"response_format\":{\"type\":\"json_object\"},\"max_tokens\":8192}) }}"
},
"credentials": {}
},
{
"id": "copilot-token-refresh",
"name": "Ottieni Token Copilot",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-496,
48
],
"parameters": {
"method": "GET",
"url": "https://api.github.com/copilot_internal/v2/token",
"authentication": "predefinedCredentialType",
"nodeCredentialType": "httpHeaderAuth",
"options": {}
},
"credentials": {
"httpHeaderAuth": {
"id": "vBwUxlzKrX3oDHyN",
"name": "GitHub Copilot OAuth Token"
}
}
},
{
"id": "if-paperless",
"name": "Ha azione Paperless?",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
384,
250
],
"parameters": {
"conditions": {
"options": {
"leftValue": "",
"caseSensitive": true,
"typeValidation": "loose"
},
"combinator": "and",
"conditions": [
{
"id": "p1",
"leftValue": "={{ Boolean($json.paperless_action) }}",
"operator": {
"type": "boolean",
"operation": "true"
},
"rightValue": ""
}
]
}
}
},
{
"id": "if-bolletta-or-fastweb",
"name": "Ha PDF allegato?",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
608,
350
],
"parameters": {
"conditions": {
"options": {},
"combinator": "and",
"conditions": [
{
"id": "b1",
"leftValue": "={{ $json.paperless_action }}",
"operator": {
"type": "string",
"operation": "equals"
},
"rightValue": "pdf_allegato"
}
]
}
}
},
{
"id": "http-trigger-bolletta",
"name": "Estrai Allegati PDF",
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
832,
260
],
"parameters": {
"mode": "runOnceForAllItems",
"jsCode": "// Leggi il messaggio Gmail completo per trovare gli allegati PDF\n// Il nodo Gmail - Leggi contenuto ha già il payload completo\nconst emails = $input.all();\nconst results = [];\nfor (const emailItem of emails) {\n const email = emailItem.json;\n if (email.paperless_action !== 'pdf_allegato') continue;\n // Trova allegati PDF in payload.parts (ricorsivo)\n function findPDFs(parts, found) {\n if (!parts) return;\n for (const p of parts) {\n if (p.parts) findPDFs(p.parts, found);\n if (p.body && p.body.attachmentId) {\n const isPDF = (p.mimeType||'').includes('pdf') || (p.filename||'').toLowerCase().endsWith('.pdf');\n if (isPDF) found.push({ attachment_id: p.body.attachmentId, filename: p.filename || 'documento.pdf' });\n }\n }\n }\n const pdfs = [];\n findPDFs((email.payload && email.payload.parts) || [], pdfs);\n for (const pdf of pdfs) {\n results.push({ json: {\n email_id: email.id,\n attachment_id: pdf.attachment_id,\n filename: pdf.filename,\n hint: (email.Subject || email.subject || '') + ' da ' + (email.From || email.from || ''),\n from: email.From || email.from || '',\n }});\n }\n}\nreturn results;"
}
},
{
"id": "if-is-test",
"name": "È un test?",
"type": "n8n-nodes-base.if",
"typeVersion": 2,
"position": [
384,
260
],
"parameters": {
"conditions": {
"options": {},
"combinator": "and",
"conditions": [
{
"id": "t1",
"leftValue": "={{ $('🧪 Test Webhook').isExecuted }}",
"operator": {
"type": "boolean",
"operation": "true"
}
}
]
}
}
},
{
"id": "gmail-fetch-all-unread",
"name": "Gmail - Fetch tutte non lette",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2.1,
"position": [
608,
360
],
"parameters": {
"operation": "getAll",
"filters": {
"readStatus": "unread",
"q": "-label:Processed"
},
"limit": 20
},
"credentials": {
"gmailOAuth2": {
"id": "qvOikS6IF0H5khr8",
"name": "Gmail account"
}
}
},
{
"id": "n_callcore",
"name": "Chiama Core Upload",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
2200,
200
],
"parameters": {
"method": "POST",
"url": "https://orchestrator.mt-home.uk/webhook/paperless-upload",
"sendBody": true,
"specifyBody": "json",
"jsonBody": "={{ JSON.stringify({ email_id: $json.email_id, attachment_id: $json.attachment_id, filename: $json.filename, hint: $json.hint, from: $json.from }) }}",
"options": {}
}
}
],
"connections": {
"Gmail - Leggi tutte le label": {
"main": [
[
{
"node": "Aggrega Label",
"type": "main",
"index": 0
}
]
]
},
"Aggrega Label": {
"main": [
[
{
"node": "È un test?",
"type": "main",
"index": 0
}
]
]
},
"Gmail - Leggi contenuto": {
"main": [
[
{
"node": "Aggrega Email",
"type": "main",
"index": 0
}
]
]
},
"Aggrega Email": {
"main": [
[
{
"node": "Costruisci Prompt",
"type": "main",
"index": 0
}
]
]
},
"Dividi Email": {
"main": [
[
{
"node": "È da eliminare?",
"type": "main",
"index": 0
}
]
]
},
"È da eliminare?": {
"main": [
[
{
"node": "Gmail - Segna come letta",
"type": "main",
"index": 0
}
],
[
{
"node": "Ha azione Paperless?",
"type": "main",
"index": 0
}
]
]
},
"Gmail - Segna come letta": {
"main": [
[
{
"node": "Gmail - Sposta nel cestino",
"type": "main",
"index": 0
}
]
]
},
"Risolvi ID Label": {
"main": [
[
{
"node": "Gmail - Aggiungi Label",
"type": "main",
"index": 0
}
]
]
},
"Costruisci Prompt": {
"main": [
[
{
"node": "Ottieni Token Copilot",
"type": "main",
"index": 0
}
]
]
},
"🧪 Test Webhook": {
"main": [
[
{
"node": "Gmail - Leggi tutte le label",
"type": "0",
"index": 0
}
]
]
},
"GPT-4.1 - Classifica Email": {
"main": [
[
{
"node": "Parse risposta GPT-4.1",
"type": "main",
"index": 0
}
]
]
},
"Parse risposta GPT-4.1": {
"main": [
[
{
"node": "Telegram - Invia Report",
"type": "main",
"index": 0
},
{
"node": "Dividi Email",
"type": "main",
"index": 0
}
]
]
},
"Ottieni Token Copilot": {
"main": [
[
{
"node": "GPT-4.1 - Classifica Email",
"type": "main",
"index": 0
}
]
]
},
"⏰ Ogni 3 ore": {
"main": [
[
{
"node": "Gmail - Leggi tutte le label",
"type": "main",
"index": 0
}
]
]
},
"Ha azione Paperless?": {
"main": [
[
{
"node": "Ha PDF allegato?",
"type": "main",
"index": 0
}
],
[
{
"node": "Risolvi ID Label",
"type": "main",
"index": 0
}
]
]
},
"È un test?": {
"main": [
[
{
"node": "Gmail - Fetch tutte non lette",
"type": "main",
"index": 0
}
],
[
{
"node": "Gmail - Fetch ultime 3h",
"type": "main",
"index": 0
}
]
]
},
"Gmail - Fetch tutte non lette": {
"main": [
[
{
"node": "Gmail - Leggi contenuto",
"type": "main",
"index": 0
}
]
]
},
"Gmail - Fetch ultime 3h": {
"main": [
[
{
"node": "Gmail - Leggi contenuto",
"type": "main",
"index": 0
}
]
]
},
"Ha PDF allegato?": {
"main": [
[
{
"node": "Estrai Allegati PDF",
"type": "main",
"index": 0
}
],
[
{
"node": "Chiama Core Upload",
"type": "main",
"index": 0
}
]
]
},
"Estrai Allegati PDF": {
"main": [
[
{
"node": "Chiama Core Upload",
"type": "main",
"index": 0
}
]
]
}
},
"settings": {
"executionOrder": "v1",
"availableInMCP": false,
"callerPolicy": "workflowsFromSameOwner"
},
"triggerCount": 2,
"versionId": "55dea25a-a72c-4c4f-8126-cc6dae55e15a",
"owner": {
"type": "personal",
"projectId": "Hdttz401OqqtObPo",
"projectName": "Martin Tahiraj <tahiraj.martin@gmail.com>",
"personalEmail": "tahiraj.martin@gmail.com"
},
"parentFolderId": null,
"isArchived": false
}