{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "475789cb-c3cf-49d5-94d0-4f4256ab10a4",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -176,
        880
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {
          "downloadAttachments": true,
          "dataPropertyAttachmentsPrefixName": "attachment_"
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8f3068ac-1e05-442a-a43e-cc89bd2a88e7",
      "name": "Get a message",
      "type": "n8n-nodes-base.gmail",
      "position": [
        192,
        880
      ],
      "parameters": {
        "simple": false,
        "options": {
          "downloadAttachments": true,
          "dataPropertyAttachmentsPrefixName": "attachment_"
        },
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "get"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "f6c752e4-24ba-4e28-9c99-cef45aed9147",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        400,
        1120
      ],
      "parameters": {
        "text": "=Subject:\n{{ $('Get a message').item.json.subject }}\n\nUser input : {{ $('Get a message').item.json.text }}\n\nHas Attachment: {{ $json.hasAttachment }}\n\n\n\n\n",
        "options": {
          "systemMessage": "=You are the personal emailing assistant for Zenith Engineering, a company that manufactures conveyors. Your role is to ensure emails are responded to professionally and categorized correctly. \n\n##Today\u2019s date and time is: {{ $now }}\n\n##Your tasks:\n\nCategorize each email by priority.\n\nDraft polite, professional replies using templates, past messages, or context.\n\nAuto-reply to common inquiries (e.g., catalogs, pricing, lead times) using approved text.\n\nDo not make up things, if you don't know, say so.\n\nIdentify attachments (e.g., drawings, POs, invoices) and label them.\n\n##Rules:\n\nNever return empty values.\n\nDo not call tools if required data is missing.\n\nIf HasAttachment is True:\n\nMark as \"Important\"\n\nThank the sender and say it\u2019s being reviewed.\n\nIf hasnoattachment is False or NO: Ask sender to resend the document politely.\n\n##Labels:\n\n\"Fishy\" \u2013 Spammy or irrelevant\n\n\"Important\" \u2013 Invoices, quotes, POs, attachment\n\n\"Promos\" \u2013 Product/service promotions\n\n\"Meeting\" \u2013 Calls, events, scheduling\n\n\"Check Later\" \u2013 Anything else\n\n\nResponse should be in JSON Object format.\n\nOnly return a valid JSON object with exactly two keys: \"Label\" and \"Response\". Do not include any explanation, markdown formatting, or extra text.\nHere's the structure example that you need to categorize each part of your response in:\n\n{\n\t\"Label\": \"Important\",\n\t\"Response\": \"Response\"\n}"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 2
    },
    {
      "id": "5e52c675-3d01-403e-a42e-c5b490054e56",
      "name": "Debugged code to find attachment",
      "type": "n8n-nodes-base.code",
      "position": [
        160,
        1120
      ],
      "parameters": {
        "jsCode": "// Initialize list to hold invoice filenames\nlet invoiceAttachments = [];\n\n// Check $binary for keys like 'attachment_0', 'attachment_1'\nif ($binary && Object.keys($binary).length > 0) {\n  for (const key of Object.keys($binary)) {\n    const file = $binary[key];\n\n    // If filename contains 'invoice', treat it as an invoice attachment\n    if (\n      file.fileName &&\n      /invoice/i.test(file.fileName)\n    ) {\n      invoiceAttachments.push(file.fileName);\n    }\n  }\n}\n\n// Return updated JSON with invoice detection\nreturn [\n  {\n    json: {\n      ...$json,\n      hasInvoiceAttachment: invoiceAttachments.length > 0,\n      invoiceFilenames: invoiceAttachments.join(', ')\n    }\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "423dc208-4abc-43bc-936e-8081b7c84b06",
      "name": "Structured Output Parser1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        592,
        1360
      ],
      "parameters": {
        "jsonSchemaExample": "{\n\t\"Label\": \"Important\",\n\t\"Response\": \"Response\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "6340421b-dd62-47a4-9dfc-5dd08c66b387",
      "name": "OpenRouter Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        400,
        1360
      ],
      "parameters": {
        "model": "google/gemini-2.5-flash-lite-preview-06-17",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "abb27cad-f021-448a-9f53-349f5766a0af",
      "name": "Information Extractor",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "position": [
        1632,
        1296
      ],
      "parameters": {
        "text": "={{ $json.text }}",
        "options": {
          "systemPromptTemplate": "=You are an expert extraction algorithm.\nOnly extract relevant information from the text.\nIf you do not know the value of an attribute asked to extract, you may omit the attribute's value.\n\n\nYour job is to:\n\n# Extract key information from the invoice such as:\n   - invoice_number\n   - invoice_date\n   - due_date (if available)\n   - vendor_name\n   - total_amount\n   - currency (e.g., USD, IDR, etc.)\n   - items (as a list of item descriptions and their amounts)\n   - tax (if available)"
        },
        "schemaType": "manual",
        "inputSchema": "{\n  \"invoice_number\": \"\",\n  \"invoice_date\": \"\",\n  \"due_date\": \"\",\n  \"vendor_name\": \"\",\n  \"total_amount\": \"\",\n  \"currency\": \"\",\n  \"items\": [\n    {\n      \"description\": \"\",\n      \"amount\": \"\"\n    }\n  ],\n  \"tax\": \"\",\n  \"category\": \"\"\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "b534c247-73ec-4a18-a9ef-75d9d94f3702",
      "name": "Split Out",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        528,
        576
      ],
      "parameters": {
        "include": "allOtherFields",
        "options": {
          "includeBinary": true
        },
        "fieldToSplitOut": "attachments"
      },
      "typeVersion": 1
    },
    {
      "id": "e03d09d3-b733-4ebb-9e6b-0e3dddd46534",
      "name": "Extract from File",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        1104,
        1296
      ],
      "parameters": {
        "options": {},
        "operation": "pdf",
        "binaryPropertyName": "={{ $json.attachments }}"
      },
      "typeVersion": 1
    },
    {
      "id": "88c73900-f883-4fe6-a1f3-bead44b4f6e6",
      "name": "Edit Fields1",
      "type": "n8n-nodes-base.set",
      "position": [
        368,
        592
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "540e2d20-e09f-4174-bcef-6e89c14a02be",
              "name": "attachments",
              "type": "array",
              "value": "={{ $('Get a message').item.binary.keys () }}"
            },
            {
              "id": "f1efedf8-155e-4fa1-9033-28129e3bf896",
              "name": "message_id",
              "type": "string",
              "value": "={{ $json.id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "3be55a2e-b289-4919-80c1-927eb487fe03",
      "name": "Edit Fields2",
      "type": "n8n-nodes-base.set",
      "position": [
        368,
        736
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "540e2d20-e09f-4174-bcef-6e89c14a02be",
              "name": "attachments_metadata",
              "type": "array",
              "value": "={{ $('Get a message').item.binary.values () }}"
            },
            {
              "id": "f1efedf8-155e-4fa1-9033-28129e3bf896",
              "name": "message_id",
              "type": "string",
              "value": "={{ $json.id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "92542f0e-7681-4933-a11b-d4291e5338b5",
      "name": "Split Out1",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        528,
        736
      ],
      "parameters": {
        "include": "allOtherFields",
        "options": {
          "includeBinary": true
        },
        "fieldToSplitOut": "attachments_metadata"
      },
      "typeVersion": 1
    },
    {
      "id": "92e64272-92eb-4bcd-b61d-ba3fe610a1d5",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        704,
        864
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2
    },
    {
      "id": "fe54224c-447d-4870-a659-2bfd755bfc3e",
      "name": "Filter",
      "type": "n8n-nodes-base.filter",
      "position": [
        928,
        864
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "3e8c8981-2067-4589-a297-98180da98539",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.attachments_metadata.fileType }}",
              "rightValue": "=pdf"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "3864b113-2733-408b-b2cc-4c3e409cbab0",
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "position": [
        1104,
        1056
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "advanced": true,
        "mergeByFields": {
          "values": [
            {
              "field1": "message_id",
              "field2": "id"
            }
          ]
        }
      },
      "typeVersion": 3.2
    },
    {
      "id": "44ff8834-36e3-40d5-976f-2f289f9e0474",
      "name": "Filter1",
      "type": "n8n-nodes-base.filter",
      "position": [
        16,
        880
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "4ba5f268-39bd-4fab-bd79-897522c7eb34",
              "operator": {
                "type": "object",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $('Gmail Trigger').item.binary }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "7e1ec925-c0e1-492d-891f-81127bdcc231",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -240,
        528
      ],
      "parameters": {
        "color": 2,
        "width": 1632,
        "height": 1008,
        "content": "## MULTIPLE DOCUMENTS EXTRACTOR\n"
      },
      "typeVersion": 1
    },
    {
      "id": "9b6c2acc-fdeb-4bed-9ac0-00092b354b74",
      "name": "Extracting File Details",
      "type": "n8n-nodes-base.set",
      "position": [
        2016,
        1296
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "6a37b21c-1372-43e4-b277-53377a5fd57e",
              "name": "output.invoice_number",
              "type": "string",
              "value": "={{ $json.output.invoice_number }}"
            },
            {
              "id": "276f7e85-cba7-43a2-8d54-9d25eb534f92",
              "name": "output.invoice_date",
              "type": "string",
              "value": "={{ $json.output.invoice_date }}"
            },
            {
              "id": "d0348246-509c-4fec-a6da-eed65b74d5d5",
              "name": "output.vendor_name",
              "type": "string",
              "value": "={{ $json.output.vendor_name }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "ad2c0eb2-0691-4401-b708-76c596fc1526",
      "name": "Convert Names to download files",
      "type": "n8n-nodes-base.code",
      "position": [
        1632,
        1056
      ],
      "parameters": {
        "jsCode": "let results = [];\n\nfor (item of items) {\n  for (key of Object.keys(item.binary)) {\n    results.push({\n      json: {\n        fileName: item.binary[key].fileName\n      },\n      binary: {\n        data: item.binary[key]\n      }\n    });\n  }\n}\n\nreturn results;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "07b492c0-2c62-4d96-91b7-d63ad46baf7c",
      "name": "Remove Duplicates",
      "type": "n8n-nodes-base.removeDuplicates",
      "position": [
        2016,
        1056
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "badba30d-fe2d-413c-a25e-3b75d13985bd",
      "name": "Gemini",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        1632,
        1440
      ],
      "parameters": {
        "model": "google/gemini-2.5-flash-lite-preview-06-17",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "29edd5f6-1a03-465c-94ba-c04ca8bea09f",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1472,
        912
      ],
      "parameters": {
        "color": 7,
        "width": 1024,
        "height": 640,
        "content": "## Telegram - Invoice and details to Engineering dept."
      },
      "typeVersion": 1
    },
    {
      "id": "ef2167ee-0038-49dd-ac6b-5a1086956294",
      "name": "Reply to same thread",
      "type": "n8n-nodes-base.gmail",
      "position": [
        768,
        1120
      ],
      "parameters": {
        "message": "={{ $json.output.Response }}",
        "options": {},
        "resource": "thread",
        "threadId": "={{ $('Gmail Trigger').item.json.threadId }}",
        "messageId": "={{ $('Gmail Trigger').item.json.id }}",
        "operation": "reply"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "7f745152-b54a-4f51-ba13-b78a3cf498cd",
      "name": "Send important details",
      "type": "n8n-nodes-base.telegram",
      "position": [
        2240,
        1296
      ],
      "parameters": {
        "text": "=Invoice Number :  {{ $json.output.invoice_number }}\n\nDate : {{ $json.output.invoice_date }}\n\nFrom : {{ $json.output.vendor_name }}\n\n{{ $json.output }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "bdfe5a8c-27c2-400d-a43f-126714240af3",
      "name": "Send a document (e.g. dwg files)",
      "type": "n8n-nodes-base.telegram",
      "position": [
        2240,
        1056
      ],
      "parameters": {
        "operation": "sendDocument",
        "binaryData": true,
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2,
      "alwaysOutputData": false
    }
  ],
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini": {
      "ai_languageModel": [
        [
          {
            "node": "Information Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Merge1": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          },
          {
            "node": "Convert Names to download files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter1": {
      "main": [
        [
          {
            "node": "Get a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Reply to same thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Edit Fields1": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields2": {
      "main": [
        [
          {
            "node": "Split Out1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a message": {
      "main": [
        [
          {
            "node": "Edit Fields1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Edit Fields2",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          },
          {
            "node": "Debugged code to find attachment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Filter1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Remove Duplicates": {
      "main": [
        [
          {
            "node": "Send a document (e.g. dwg files)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Information Extractor": {
      "main": [
        [
          {
            "node": "Extracting File Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Extracting File Details": {
      "main": [
        [
          {
            "node": "Send important details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser1": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Convert Names to download files": {
      "main": [
        [
          {
            "node": "Remove Duplicates",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Debugged code to find attachment": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}