{
  "name": "Upload Receipt DEMO",
  "nodes": [
    {
      "parameters": {
        "promptType": "define",
        "text": "=🎯 Objective:\nExtract the key information from the receipt:\n\n🧠 Your Role:\nYou are an automated Receipt  verification assistant.\n\nContent Extract from OCR:\n\n\n{{ $json.content }}\n\n\n\n📤 Required Output Format (example):\n\n{\n  \"Name\": \"\",\n  \"Description\": \"\",\n  \"Date\": \"\",\n  \"Image\": \"\",\n  \"Payment Method\": \"\",\n  \"Total\": \"\",\n  \"MessageBackToUser\": \"\",\n  \"file_id\": \"{{ $('Set File_Id').item.json.file_id }}\"\n}\n\n✅ Verification Criteria:\nDetermine if the uploaded image is a valid receipt.\nIf it is not an receipt, return a user-friendly message in the MessageBackToUser field.\n\n📥 Instructions for Response:\nAfter verifying the receipt:\n\nFill in the output fields based on the extracted OCR text.\n\nIn MessageBackToUser, confirm:\n\"The image has been uploaded to the Invoice Folder in Google Drive and added to the Google Spreadsheet.\"\n\nIf not an invoice, return:\n\"This image does not appear to be an receipt. Please upload a valid receipt.\"",
        "hasOutputParser": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.8,
      "position": [
        260,
        380
      ],
      "id": "12603e52-e52d-48ba-acce-d2150aa9aa45",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        180,
        560
      ],
      "id": "b1a7300d-3a48-4770-ab85-099e48965b8e",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "MzIepidaRw9ZqcPB",
          "name": "OpenAi account Credentials"
        }
      }
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\n  \"Name\": \"\",\n  \"Description\": \"\",\n  \"Date\": \"\",\n  \"Image\": \"\",\n  \"Payment Method\": \"\",\n  \"Total\": \"\",\n  \"MessageBackToUser\": \"\",\n  \"file_id\": \"\"\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.2,
      "position": [
        460,
        560
      ],
      "id": "edcfa635-5468-4cd4-b557-fea85e3b051e",
      "name": "Structured Output Parser"
    },
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        -20,
        -180
      ],
      "id": "ec969d88-9fe8-4810-bedd-f544b80f16fe",
      "name": "Telegram Trigger",
      "webhookId": "ad921f1d-6aee-4063-b33c-2a1b9cde041e",
      "credentials": {
        "telegramApi": {
          "id": "IR7xqx0ILwANdL7f",
          "name": "n8n bot"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Telegram Trigger').item.json.message.chat.id }}"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        320,
        560
      ],
      "id": "1ea45074-c2aa-4c6b-892e-6feebc7dbaf8",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $('AI Agent').item.json.output.MessageBackToUser }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        980,
        180
      ],
      "id": "a3e6c40e-1b23-445c-9099-869ee112254c",
      "name": "Telegram",
      "webhookId": "c50e46ab-d028-4dd3-9e7d-164ebd7b0786",
      "credentials": {
        "telegramApi": {
          "id": "IR7xqx0ILwANdL7f",
          "name": "n8n bot"
        }
      }
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.file_id }}"
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        20,
        60
      ],
      "id": "b7ece53c-9592-4e7a-9f2d-1d9d7eea2c28",
      "name": "Download File",
      "webhookId": "ef02c81f-c0f2-4ff1-8611-81fbaaba8501",
      "credentials": {
        "telegramApi": {
          "id": "IR7xqx0ILwANdL7f",
          "name": "n8n bot"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "dd5c3f83-ebc2-486f-ae54-d500444b4b7d",
              "leftValue": "={{ $json.message.document.file_id !== undefined }}",
              "rightValue": "true",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            },
            {
              "id": "3a823398-1472-4b15-9347-7d71e0301a7e",
              "leftValue": "={{ $json.message.photo[0].file_id !== undefined }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "or"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        180,
        -180
      ],
      "id": "6d3d1e84-8207-4849-9444-2e0cabcbf7e1",
      "name": "Check File Exists"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "93c20d47-ca76-4865-8dcd-9df060780d08",
              "name": "file_id",
              "value": "={{ $json.message.photo[3].file_id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        400,
        -200
      ],
      "id": "c0ecfd85-7af2-4d71-92ed-acc697057030",
      "name": "Set File_Id"
    },
    {
      "parameters": {
        "resource": "image",
        "operation": "analyze",
        "modelId": {
          "__rl": true,
          "value": "gpt-4o",
          "mode": "list",
          "cachedResultName": "GPT-4O"
        },
        "text": "Extract these fields",
        "inputType": "base64",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        420,
        160
      ],
      "id": "572daefe-7f60-46a1-b544-95bd9316dd2e",
      "name": "OpenAI",
      "credentials": {
        "openAiApi": {
          "id": "MzIepidaRw9ZqcPB",
          "name": "OpenAi account Credentials"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const newMimeType = 'image/png'; // or 'image/jpeg' as needed\n\nfor (const item of items) {\n  if (item.binary && item.binary.data) {\n    item.binary.data.mimeType = newMimeType;\n  }\n}\n\nreturn items;\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        160
      ],
      "id": "7654c82f-1006-4edc-92a0-7d94a15832c3",
      "name": "Set MIME Type"
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "1F4ytmNWsAv3wMTRI_t8V9hW_wD8Xcyg9QW_NIuPfbJE",
          "mode": "list",
          "cachedResultName": "Receipts",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1F4ytmNWsAv3wMTRI_t8V9hW_wD8Xcyg9QW_NIuPfbJE/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "gid=0",
          "mode": "list",
          "cachedResultName": "Sheet1",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1F4ytmNWsAv3wMTRI_t8V9hW_wD8Xcyg9QW_NIuPfbJE/edit#gid=0"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Id": "={{ $('Download File').item.json.result.file_unique_id }}",
            "Name": "={{ $json.output.Name }}",
            "Description": "={{ $json.output.Description }}",
            "Date": "={{ $json.output.Date }}",
            "Image": "={{ $json.webViewLink }}",
            "Payment Method": "={{ $json.output['Payment Method'] }}",
            "Total": "={{ $json.output.Total }}"
          },
          "matchingColumns": [
            "Id"
          ],
          "schema": [
            {
              "id": "Name",
              "displayName": "Name",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Description",
              "displayName": "Description",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Date",
              "displayName": "Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Payment Method",
              "displayName": "Payment Method",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Total",
              "displayName": "Total",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Image",
              "displayName": "Image",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Id",
              "displayName": "Id",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        820,
        180
      ],
      "id": "98578858-0891-4db1-9ee4-e57c1b248ba9",
      "name": "Add To Google Sheets",
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "ESD8JJHqd8A9NlNx",
          "name": "Google Sheets account (Kris)"
        }
      }
    },
    {
      "parameters": {
        "name": "=Invoice_{{ $json.result.file_unique_id }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "1W3zGpi2Qm31LHoRVzIz64pGafZ_1mqZD",
          "mode": "list",
          "cachedResultName": "Invoices",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1W3zGpi2Qm31LHoRVzIz64pGafZ_1mqZD"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        240,
        0
      ],
      "id": "cd316828-af07-4217-9206-771c612b1c8b",
      "name": "Upload To Receipt Folder",
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "VqZzm5YeRMOeGO9B",
          "name": "Google Drive account (kris)"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c5a159dc-7d27-4c7b-beb7-9085fd041a10",
              "name": "webViewLink",
              "value": "={{ $json.webViewLink }}",
              "type": "string"
            },
            {
              "id": "ef40c638-0c60-4c74-9968-1574a21e9326",
              "name": "file_id",
              "value": "={{ $('Set File_Id').item.json.file_id }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        460,
        0
      ],
      "id": "d9946aa2-9c7a-4bbe-81c1-b17f8cf3ae03",
      "name": "Set URL"
    },
    {
      "parameters": {
        "mode": "combine",
        "advanced": true,
        "mergeByFields": {
          "values": [
            {
              "field1": "file_id",
              "field2": "output.file_id"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        660,
        180
      ],
      "id": "360f4ecb-565d-4f52-bf11-1b6e8a5cc6d1",
      "name": "Merge"
    }
  ],
  "pinData": {},
  "connections": {
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Check File Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Download File": {
      "main": [
        [
          {
            "node": "Upload To Receipt Folder",
            "type": "main",
            "index": 0
          },
          {
            "node": "Set MIME Type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check File Exists": {
      "main": [
        [
          {
            "node": "Set File_Id",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Set File_Id": {
      "main": [
        [
          {
            "node": "Download File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set MIME Type": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add To Google Sheets": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload To Receipt Folder": {
      "main": [
        [
          {
            "node": "Set URL",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set URL": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Add To Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2a11a7ec-e125-4794-abbf-3a4b5dd6cbd2",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "4aabc0aa2e160dafa4f9d9bcedde563b5af29598cd16960f637ae2fb99aea720"
  },
  "id": "1PBp3wBiARK1nxCn",
  "tags": []
}