{
  "name": "Agente de facturas · VisionRs",
  "nodes": [
    {
      "id": "87fb1805-8489-46c8-8fa3-aa40706847c0",
      "name": "Llega una factura",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.4,
      "position": [0, 0],
      "parameters": {
        "pollTimes": { "item": [{ "mode": "everyMinute" }] },
        "simple": false,
        "filters": {
          "q": "has:attachment filename:pdf (factura OR comprobante OR invoice) -label:agente-revisado newer_than:7d",
          "readStatus": "both"
        },
        "options": { "downloadAttachments": true, "dataPropertyAttachmentsPrefixName": "attachment_" }
      }
    },
    {
      "id": "e39d7d46-d54b-4a46-8874-2b2033f4698a",
      "name": "Configuración",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.5,
      "position": [240, 0],
      "parameters": {
        "mode": "manual",
        "includeOtherFields": true,
        "assignments": {
          "assignments": [
            { "id": "cfg-chat", "name": "telegram_chat_id", "value": "PEGÁ ACÁ TU CHAT ID", "type": "string" }
          ]
        },
        "options": { "stripBinary": false }
      }
    },
    {
      "id": "8874ab9b-885e-4ce9-8810-a34b8303f862",
      "name": "Leer el PDF",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1.1,
      "position": [480, 0],
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "attachment_0",
        "options": { "keepSource": "both", "joinPages": true, "maxPages": 5 }
      }
    },
    {
      "id": "622fd8d8-a61d-428d-89cf-02aa84dcf281",
      "name": "Guardar en Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [720, 0],
      "parameters": {
        "resource": "file",
        "operation": "upload",
        "inputDataFieldName": "attachment_0",
        "name": "={{ $now.toFormat('yyyy-MM-dd') }} {{ $binary.attachment_0.fileName }}",
        "driveId": { "__rl": true, "mode": "list", "value": "My Drive" },
        "folderId": { "__rl": true, "mode": "list", "value": "root", "cachedResultName": "/ (Root folder)" },
        "options": {}
      }
    },
    {
      "id": "ac6d0253-b012-4e8c-b1b9-8d72a97e6fdf",
      "name": "Sacar los datos",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [960, 0],
      "parameters": {
        "text": "={{ ($('Leer el PDF').item.json.text || '').slice(0, 6000) }}",
        "schemaType": "fromAttributes",
        "attributes": {
          "attributes": [
            { "name": "proveedor", "type": "string", "description": "Nombre o razón social de quien emite la factura", "required": false },
            { "name": "cuit", "type": "string", "description": "CUIT de quien emite la factura", "required": false },
            { "name": "fecha", "type": "string", "description": "Fecha de emisión, en formato AAAA-MM-DD", "required": false },
            { "name": "numero", "type": "string", "description": "Número de factura o comprobante, por ejemplo 0003-00004521", "required": false },
            { "name": "total", "type": "number", "description": "Importe total a pagar: solo el número, sin símbolo ni separador de miles", "required": false },
            { "name": "moneda", "type": "string", "description": "Moneda del total: ARS, USD u otra", "required": false }
          ]
        },
        "options": {
          "systemPromptTemplate": "Leés facturas de un negocio. Sacá solo los datos que aparecen en el texto. Si un dato no está o no se lee bien, dejalo vacío: no lo inventes ni lo calcules."
        }
      }
    },
    {
      "id": "907639ec-bca3-4f32-92dd-3bdfa76b2092",
      "name": "Cabeza: el modelo",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [980, 220],
      "parameters": {
        "model": { "__rl": true, "mode": "list", "value": "gpt-5-mini" },
        "options": { "reasoningEffort": "low" }
      }
    },
    {
      "id": "a3b8910a-6590-4432-9b5c-d058a4fe4c85",
      "name": "Anotar en la planilla",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [1240, 0],
      "parameters": {
        "resource": "sheet",
        "operation": "append",
        "documentId": { "__rl": true, "mode": "list", "value": "", "cachedResultName": "Facturas" },
        "sheetName": { "__rl": true, "mode": "list", "value": "" },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "recibida": "={{ $now.toFormat('yyyy-MM-dd') }}",
            "proveedor": "={{ $json.output.proveedor }}",
            "cuit": "={{ $json.output.cuit }}",
            "fecha": "={{ $json.output.fecha }}",
            "numero": "={{ $json.output.numero }}",
            "total": "={{ $json.output.total }}",
            "moneda": "={{ $json.output.moneda }}",
            "archivo": "=https://drive.google.com/file/d/{{ $('Guardar en Drive').item.json.id }}/view"
          },
          "schema": [
            { "id": "recibida", "displayName": "recibida", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "proveedor", "displayName": "proveedor", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "cuit", "displayName": "cuit", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "fecha", "displayName": "fecha", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "numero", "displayName": "numero", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "total", "displayName": "total", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "moneda", "displayName": "moneda", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "archivo", "displayName": "archivo", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "58869ff6-2500-40c9-b2fc-f463cf599c0c",
      "name": "¿Falta algún dato?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [1480, 0],
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "loose" },
          "conditions": [
            { "leftValue": "={{ $('Sacar los datos').item.json.output.proveedor || '' }}", "rightValue": "", "operator": { "type": "string", "operation": "empty", "singleValue": true } },
            { "leftValue": "={{ $('Sacar los datos').item.json.output.numero || '' }}", "rightValue": "", "operator": { "type": "string", "operation": "empty", "singleValue": true } },
            { "leftValue": "={{ String($('Sacar los datos').item.json.output.total ?? '') }}", "rightValue": "", "operator": { "type": "string", "operation": "empty", "singleValue": true } }
          ],
          "combinator": "or"
        },
        "options": {}
      }
    },
    {
      "id": "a2a12bab-e7f3-42b4-a816-52aed66ad2d8",
      "name": "Pedir que la revises",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [1720, -180],
      "parameters": {
        "resource": "message",
        "operation": "sendMessage",
        "chatId": "={{ $('Configuración').first().json.telegram_chat_id }}",
        "text": "=Guardé una factura de {{ ($(\"Sacar los datos\").item.json.output.proveedor || \"un proveedor que no reconocí\").replace(/&/g, \"&amp;\").replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\") }}, pero no pude leer {{ [[\"el proveedor\", $(\"Sacar los datos\").item.json.output.proveedor], [\"el número\", $(\"Sacar los datos\").item.json.output.numero], [\"el total\", $(\"Sacar los datos\").item.json.output.total]].filter(d => !d[1]).map(d => d[0]).join(\", \") }}.\nRevisala y completá la planilla: https://drive.google.com/file/d/{{ $(\"Guardar en Drive\").item.json.id }}/view",
        "additionalFields": { "appendAttribution": false }
      },
      "webhookId": "e795e441-6c56-4d50-b49a-317f57e1efb8"
    },
    {
      "id": "152da14f-8262-45bd-b872-755a72ff3233",
      "name": "Marcar como revisada",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [1960, 0],
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $('Llega una factura').item.json.id }}",
        "labelIds": []
      },
      "webhookId": "58a77468-50fb-4bbb-97a0-e46508850bf7"
    },
    {
      "id": "042d2aa2-bef3-45a2-ac38-9154e02da1c6",
      "name": "Nota: para usarlo",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [-60, -580],
      "parameters": {
        "content": "## Agente de facturas\nUna plantilla de la guía «Tu primer agente, paso a paso» de VisionRs: visionrs.com.ar/ia/que-es-un-agente/\n\nCuando te llega una factura en PDF, la guarda en tu Drive con la fecha adelante, saca el proveedor, el número, la fecha y el total, y lo anota en tu planilla. Si no pudo leer algún dato, te avisa por Telegram para que lo revises.\n\n**Para usarlo:**\n1. Conectá tus cuentas en los bloques con aviso: Gmail, OpenAI, Google Drive, Google Sheets y Telegram.\n2. En «Configuración», pegá tu chat ID de Telegram.\n3. En «Guardar en Drive», elegí tu carpeta de facturas.\n4. Creá una planilla con las columnas recibida, proveedor, cuit, fecha, numero, total, moneda y archivo, y elegila en «Anotar en la planilla».\n5. En Gmail, creá la etiqueta «agente-revisado» y elegila en «Marcar como revisada».\n6. Si tus facturas llegan con otras palabras en el asunto, cambiá la búsqueda en «Llega una factura».\n\nOjo: lee PDFs con texto. Una foto o un escaneo no los puede leer.",
        "color": 5,
        "width": 620,
        "height": 520
      }
    },
    {
      "id": "e2be71a5-4953-4ccc-abb9-fd778c357048",
      "name": "Nota: la cabeza y la regla",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [880, 330],
      "parameters": {
        "content": "**La cabeza** es el modelo. Si preferís Claude o Gemini, borrá este bloque y conectá «Anthropic Chat Model» o «Google Gemini Chat Model».\n\n**La regla más importante** está en «Sacar los datos»: si un dato no está, lo deja vacío y no lo inventa.",
        "color": 6,
        "width": 420,
        "height": 180
      }
    }
  ],
  "connections": {
    "Llega una factura": { "main": [[{ "node": "Configuración", "type": "main", "index": 0 }]] },
    "Configuración": { "main": [[{ "node": "Leer el PDF", "type": "main", "index": 0 }]] },
    "Leer el PDF": { "main": [[{ "node": "Guardar en Drive", "type": "main", "index": 0 }]] },
    "Guardar en Drive": { "main": [[{ "node": "Sacar los datos", "type": "main", "index": 0 }]] },
    "Sacar los datos": { "main": [[{ "node": "Anotar en la planilla", "type": "main", "index": 0 }]] },
    "Cabeza: el modelo": { "ai_languageModel": [[{ "node": "Sacar los datos", "type": "ai_languageModel", "index": 0 }]] },
    "Anotar en la planilla": { "main": [[{ "node": "¿Falta algún dato?", "type": "main", "index": 0 }]] },
    "¿Falta algún dato?": {
      "main": [
        [{ "node": "Pedir que la revises", "type": "main", "index": 0 }],
        [{ "node": "Marcar como revisada", "type": "main", "index": 0 }]
      ]
    },
    "Pedir que la revises": { "main": [[{ "node": "Marcar como revisada", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1", "timezone": "America/Argentina/Buenos_Aires" },
  "pinData": {},
  "nodeGroups": [
    {
      "id": "1d091a1d-fdb3-48f8-bcef-a8d9cf569f98",
      "name": "Lee y guarda la factura",
      "nodeNames": ["Leer el PDF", "Guardar en Drive", "Sacar los datos", "Cabeza: el modelo", "Anotar en la planilla"],
      "description": "Lee el PDF, lo guarda en Drive, saca los datos sin inventar y los anota en la planilla"
    }
  ]
}
