{
  "name": "MDIA - Triage emails de soporte (Gemini Flash-Lite)",
  "nodes": [
    {
      "parameters": {
        "content": "## Triage de emails de soporte con Gemini 3.5 Flash-Lite — Miercoles de IA\n\nANTES DE ACTIVAR, 3 pasos:\n1) En los DOS nodos \"(Gemini)\" cambia REEMPLAZA_CON_TU_API_KEY por tu clave de ai.google.dev\n2) Conecta tus credenciales de Gmail (los 3 nodos de Gmail) y de Telegram\n3) En \"Avisar Telegram\" cambia REEMPLAZA_CON_TU_CHAT_ID por tu chat de Telegram\n\nQue hace: lee cada email nuevo, lo clasifica (urgente / normal / spam),\nte redacta un BORRADOR de respuesta en el idioma del cliente y lo guarda en Gmail\n(NO lo envia). Si es urgente, ademas te avisa por Telegram.\n\nmiercolesdeia.es",
        "height": 320,
        "width": 460,
        "color": 4
      },
      "id": "note-1",
      "name": "Instrucciones",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        180,
        40
      ]
    },
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": true,
        "filters": {},
        "options": {}
      },
      "id": "node-trigger",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        420
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.5-flash-lite:generateContent?key=REEMPLAZA_CON_TU_API_KEY",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ ({ contents: [ { parts: [ { text: 'Eres un clasificador de emails de soporte al cliente. Clasifica el email en una de estas categorias exactas: urgente, normal, spam. Detecta tambien el idioma del email como codigo ISO (por ejemplo: es, en, fr, ar).\\n\\nAsunto: ' + $json.subject + '\\nDe: ' + $json.from + '\\nContenido: ' + $json.snippet + '\\n\\nDevuelve SOLO un JSON con dos claves: categoria e idioma.' } ] } ], generationConfig: { temperature: 0.1, responseMimeType: 'application/json' } }) }}",
        "options": {}
      },
      "id": "node-classify",
      "name": "Clasificar (Gemini)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        420
      ]
    },
    {
      "parameters": {
        "jsCode": "// Lee la respuesta JSON de Gemini y saca categoria + idioma\nconst out = [];\nfor (const item of $input.all()) {\n  const raw = item.json?.candidates?.[0]?.content?.parts?.[0]?.text || '{}';\n  let data;\n  try { data = JSON.parse(raw); } catch (e) { data = { categoria: 'normal', idioma: 'es' }; }\n  const categoria = String(data.categoria || 'normal').toLowerCase().trim();\n  const idioma = String(data.idioma || 'es').toLowerCase().trim();\n  out.push({ json: { categoria, idioma } });\n}\nreturn out;"
      },
      "id": "node-code",
      "name": "Leer categoria",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        680,
        420
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "c-spam",
              "leftValue": "={{ $json.categoria }}",
              "rightValue": "spam",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "node-ifspam",
      "name": "Es spam?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        900,
        420
      ]
    },
    {
      "parameters": {},
      "id": "node-noop",
      "name": "Ignorar spam",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1140,
        280
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.5-flash-lite:generateContent?key=REEMPLAZA_CON_TU_API_KEY",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ ({ contents: [ { parts: [ { text: 'Redacta un borrador de respuesta cordial, breve y profesional a este email de soporte. Escribelo en el idioma con codigo: ' + $('Leer categoria').item.json.idioma + '. Devuelve SOLO el cuerpo del mensaje, sin asunto y sin comillas. Termina firmando como \\'Equipo de soporte\\'.\\n\\nAsunto: ' + $('Gmail Trigger').item.json.subject + '\\nContenido del cliente: ' + $('Gmail Trigger').item.json.snippet } ] } ], generationConfig: { temperature: 0.4 } }) }}",
        "options": {}
      },
      "id": "node-draftgen",
      "name": "Generar borrador (Gemini)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1140,
        540
      ]
    },
    {
      "parameters": {
        "resource": "draft",
        "operation": "create",
        "subject": "={{ 'Re: ' + $('Gmail Trigger').item.json.subject }}",
        "emailType": "text",
        "message": "={{ $json.candidates[0].content.parts[0].text }}",
        "options": {
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}"
        }
      },
      "id": "node-draft",
      "name": "Crear borrador Gmail",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1360,
        540
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "c-urg",
              "leftValue": "={{ $('Leer categoria').item.json.categoria }}",
              "rightValue": "urgente",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "node-ifurg",
      "name": "Es urgente?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1580,
        540
      ]
    },
    {
      "parameters": {
        "chatId": "REEMPLAZA_CON_TU_CHAT_ID",
        "text": "={{ '\\ud83d\\udea8 Email URGENTE de ' + $('Gmail Trigger').item.json.from + '\\n' + 'Asunto: ' + $('Gmail Trigger').item.json.subject + '\\n' + 'Borrador ya creado en Gmail para que lo revises.' }}",
        "additionalFields": {}
      },
      "id": "node-tg",
      "name": "Avisar Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1800,
        420
      ]
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Clasificar (Gemini)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Clasificar (Gemini)": {
      "main": [
        [
          {
            "node": "Leer categoria",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Leer categoria": {
      "main": [
        [
          {
            "node": "Es spam?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Es spam?": {
      "main": [
        [
          {
            "node": "Ignorar spam",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Generar borrador (Gemini)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generar borrador (Gemini)": {
      "main": [
        [
          {
            "node": "Crear borrador Gmail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Crear borrador Gmail": {
      "main": [
        [
          {
            "node": "Es urgente?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Es urgente?": {
      "main": [
        [
          {
            "node": "Avisar Telegram",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}