{
  "name": "Extract data from Thai Government letters with Mistral OCR and store in Google Sheets",
  "nodes": [
    {
      "id": "beb2e565-ce98-4068-9a78-b4a26b85bd80",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3888,
        -32
      ],
      "parameters": {
        "width": 261,
        "height": 608,
        "content": "## LINE Bot Letter OCR Workflow\n\n**Features:**\n- Receive files from LINE and Google Drive\n- OCR using Mistral Document AI\n- Extract invoice information using OpenAI's Information Extractor\n- Automatic"
      }
    },
    {
      "id": "30426fbd-4053-415d-b3b5-fc3dff0b1b82",
      "name": "LINE Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -3568,
        176
      ]
    },
    {
      "id": "9a8dbc27-67e0-44db-aa45-677ada852e1b",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3616,
        -32
      ],
      "parameters": {
        "width": 200,
        "height": 341,
        "content": "### LINE Webhook\n\n**Receive data from LINE:**\n- Accepts POST request from LINE Messaging API\n- Extracts `messageId` for downloading file\n\n**Path:** `/line-invoice`\n**Method:** `POST`"
      }
    },
    {
      "id": "2a996f22-ad7f-4c4e-ae64-7e098e2dccda",
      "name": "Get Line File",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -3296,
        176
      ]
    },
    {
      "id": "bead3404-66a3-4d15-98d3-ec1cf2eda7a7",
      "name": "Set Line Data",
      "type": "n8n-nodes-base.set",
      "position": [
        -3072,
        176
      ]
    },
    {
      "id": "399b0815-e925-46de-bc80-9befdbe48739",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3408,
        -32
      ],
      "parameters": {
        "width": 567,
        "height": 341,
        "content": "### Download File from LINE\n\n**Process:**\n1. Check if message type is \"image\"\n2. Use `messageId` to download the file\n3. Set metadata fields: source, fileName, fileId\n4. Store `fileBinary` for OCR\n\n**"
      }
    },
    {
      "id": "a066a27e-e10c-4bd4-8663-b9614682e991",
      "name": "Google Drive Trigger",
      "type": "n8n-nodes-base.googleDriveTrigger",
      "position": [
        -3344,
        336
      ]
    },
    {
      "id": "f278bb26-dc66-4d55-a2c5-0065775f471b",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3616,
        320
      ],
      "parameters": {
        "width": 776,
        "height": 257,
        "content": "\n### Google Drive Trigger\n\n**Monitor for new files:**\n- Watch a specific folder in Google Drive\n- Trigger the workflow when a new file is added\n- Supports PDF, JPG, JPEG formats\n\n**Folder ID:** `$env."
      }
    },
    {
      "id": "b85acdcb-66bb-4707-98ca-e91af2a614d2",
      "name": "Download File (Drive)",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -3136,
        336
      ]
    },
    {
      "id": "dac53614-da13-4147-992a-f0db91e7bc89",
      "name": "Merge Data Sources",
      "type": "n8n-nodes-base.merge",
      "position": [
        -2768,
        256
      ]
    },
    {
      "id": "fcecfcb9-deb2-489b-a36e-e71c779f0d29",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2832,
        -32
      ],
      "parameters": {
        "width": 231,
        "height": 609,
        "content": "### Merge Data Sources\n\n**Combine data from both sources:**\n- LINE: source = \"line\", fileName, fileId, fileBinary\n- Drive: source = \"drive\", fileName, fileId, fileBinary\n\n**Output:** Unified data obje"
      }
    },
    {
      "id": "2b36574f-9c6b-46da-8902-9efbc07667b3",
      "name": "Check File Type",
      "type": "n8n-nodes-base.if",
      "position": [
        -2528,
        176
      ]
    },
    {
      "id": "f5c236db-a54d-4c15-bf40-e9166a552cfc",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2592,
        -32
      ],
      "parameters": {
        "width": 231,
        "height": 341,
        "content": "### File Type Check\n\n**Determine file type:**\n- JPG, JPEG → OCR Image Processing\n- PDF → OCR Document Processing\n\n**Regex Pattern:** `.*\\.(jpg|jpeg|png)$`\n\n**True:** Continue to OCR via Mistral  \n**Fa"
      }
    },
    {
      "id": "c6948c15-9278-4c2b-9d6e-c832e43b75c8",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2352,
        -32
      ],
      "parameters": {
        "width": 279,
        "height": 341,
        "content": "### Mistral OCR Image\n\n**OCR using AI:**\n\n**Steps:**\n1. Encode image file to Base64\n2. Send to Mistral OCR node\n3. Receive parsed text from the image"
      }
    },
    {
      "id": "1bfcad59-b68e-47ee-8796-71ee066cf8fa",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2592,
        320
      ],
      "parameters": {
        "width": 519,
        "height": 261,
        "content": "### Mistral OCR Document\n\n**OCR using AI:**\n\n**Steps:**\n1. Encode PDF file to Base64\n2. Send to Mistral OCR node\n3. Receive parsed text from the document"
      }
    },
    {
      "id": "1700e1d6-b00a-4872-96b7-5f46f50cce94",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2064,
        -32
      ],
      "parameters": {
        "width": 343,
        "height": 341,
        "content": "### AI Validation Agent\n\n**Extract and validate data using AI:**\n\n**Validation fields:**\n- book_id\n- subject\n- to\n- ..."
      }
    },
    {
      "id": "d1974977-c464-4b6f-8ad2-d3000b9ccde6",
      "name": "Write to Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -1520,
        160
      ]
    },
    {
      "id": "05d85bdd-c642-4a45-a2c9-ffcda27c4906",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1712,
        -32
      ],
      "parameters": {
        "width": 391,
        "height": 341,
        "content": "### Google Sheets Output\n\n**Append structured data to Google Sheets:**\n\n**Columns:**\n- book_id\n- subject\n- to\n...\n\n**Sheet:** `data`  \n**Operation:** `append`\n"
      }
    },
    {
      "id": "c51e40ff-0aab-4c99-84e9-dbaa7ecb01bb",
      "name": "Reply to LINE",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1456,
        336
      ]
    },
    {
      "id": "076fdad3-6c0b-4812-bdb5-8b13550da89c",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1712,
        320
      ],
      "parameters": {
        "width": 391,
        "height": 133,
        "content": "### LINE Reply\n\n**Send reply message to LINE user:**\n- Confirm invoice has been saved\n- Include extracted details"
      }
    },
    {
      "id": "1a14bc7a-a7ca-468c-a053-aed3a3a71cb2",
      "name": "Check Source for Reply",
      "type": "n8n-nodes-base.if",
      "position": [
        -2016,
        448
      ]
    },
    {
      "id": "54929d5d-2f74-4e71-a06b-47051baefca1",
      "name": "Information Extractor",
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "position": [
        -2032,
        160
      ]
    },
    {
      "id": "3d82e9cd-a982-4ad6-8a18-5fecdd3bbfd5",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -1808,
        208
      ]
    },
    {
      "id": "efe1b305-288b-4ea9-a35b-0cd634e2e011",
      "name": "Set Drive Data",
      "type": "n8n-nodes-base.set",
      "position": [
        -2976,
        336
      ]
    },
    {
      "id": "8067480c-7800-4da7-a195-8e972b01cb5e",
      "name": "Move file",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -1456,
        480
      ]
    },
    {
      "id": "9ea5b7ae-0a37-4b57-a2ee-259b57cc05ff",
      "name": "Sticky Note11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2064,
        320
      ],
      "parameters": {
        "width": 343,
        "height": 261,
        "content": "### File Source Handling\n\n**Differentiate by file origin:**\n- LINE → Reply with summary\n- Google Drive → Move file to archive folder\n"
      }
    },
    {
      "id": "d66136d7-8145-449a-958b-5498fa6b5a9a",
      "name": "Sticky Note12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1712,
        464
      ],
      "parameters": {
        "width": 391,
        "height": 117,
        "content": "### Google Drive Move Folder\n\n**Organize processed files:**\n- Move to archive/storage folder\n"
      }
    },
    {
      "id": "beb6d82a-27f7-4960-876a-90de2b5f76dc",
      "name": "Extract text IMG",
      "type": "n8n-nodes-base.mistralAi",
      "position": [
        -2256,
        160
      ]
    },
    {
      "id": "407ee96d-ba16-4afb-ab0f-fdf3cdb95e4a",
      "name": "Extract text PDF",
      "type": "n8n-nodes-base.mistralAi",
      "position": [
        -2256,
        368
      ]
    }
  ],
  "connections": {
    "LINE Webhook": {
      "main": [
        [
          {
            "node": "Get Line File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Line File": {
      "main": [
        [
          {
            "node": "Set Line Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Line Data": {
      "main": [
        [
          {
            "node": "Merge Data Sources",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Drive Data": {
      "main": [
        [
          {
            "node": "Merge Data Sources",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Check File Type": {
      "main": [
        [
          {
            "node": "Extract text IMG",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Extract text PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract text IMG": {
      "main": [
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract text PDF": {
      "main": [
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Information Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Merge Data Sources": {
      "main": [
        [
          {
            "node": "Check File Type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Drive Trigger": {
      "main": [
        [
          {
            "node": "Download File (Drive)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download File (Drive)": {
      "main": [
        [
          {
            "node": "Set Drive Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Information Extractor": {
      "main": [
        [
          {
            "node": "Write to Google Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write to Google Sheet": {
      "main": [
        [
          {
            "node": "Check Source for Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Source for Reply": {
      "main": [
        [
          {
            "node": "Reply to LINE",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Move file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}