{
  "name": "AI Expense Parser",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "id": "36a41a96-901f-4bda-bcdb-02e57faa03ac",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        224,
        176
      ],
      "webhookId": "70186ad0-686b-49df-9d22-f341b94d4af9",
      "credentials": {
        "telegramApi": {
          "id": "g4ig042auXSX6kTQ",
          "name": "Expense tracker agent"
        }
      }
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "e99e8177-877e-49ed-8af6-a22e30c4c867",
                    "leftValue": "={{ $json.message.photo[3].file_id }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Image Desktop"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "49e839b3-d831-4b76-a57f-775460dc1345",
                    "leftValue": "={{ $json.message.photo[2].file_id }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Image Mobile"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "45d8a48f-8e79-4878-9415-4ffc20961ed6",
                    "leftValue": "={{ $json.message.text }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Text"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "ca960615-e1e7-432e-a5a6-7819129ad95d",
                    "leftValue": "={{ $json.message.voice.file_id }}",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "exists",
                      "singleValue": true
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "voice"
            }
          ]
        },
        "options": {}
      },
      "id": "2d2bff9b-db06-491a-90eb-e0fcfc218435",
      "name": "Route by Message Type",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.3,
      "position": [
        368,
        160
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.text }}",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "=Extract expense information from the input. \nParse natural language variations like \"spent 20 on gas\", \"lunch $12.50\", \"coffee 3 euros\". \nExtract: amount (convert to number), description like for what service or product was paid, category, and date (use today if not specified). \n\nCategories:\n- food: groceries, restaurants, beverages (including alcohol, beer, soft drinks, coffee)\n- sport: gym, equipment, activities\n- transport: gas, public transport, taxi, parking\n- utilities: electricity, water, internet, phone\n- shopping: clothing, electronics, household items\n- entertainment: movies, games, events\n- work: any online tools, office equipment etc.\n- health: pharmacy, doctor, supplements, sports nutrition\n- personal: haircut, hygene products\n- other: anything unclear\n\nReturn structured JSON with these fields.\n#Rules:\n- Save the Date always in ISO format\n- Beverages of any kind (alcoholic or not) = food category\n- Restaurant/cafe purchases = food category\n#current time: {{ $now }}"
        }
      },
      "id": "52923739-2b95-45f3-8270-218baf88c503",
      "name": "Parse Expense Data",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2.2,
      "position": [
        1040,
        176
      ]
    },
    {
      "parameters": {
        "jsCode": "const monthNames = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];\n\nfor (const item of $input.all()) {\n  const date = new Date(item.json.output.date);\n  const monthIndex = date.getMonth(); // 0-11\n  const monthName = monthNames[monthIndex];\n  \n  item.json.output.month = monthName;\n}\n\nreturn $input.all();"
      },
      "id": "9ccb8026-d81e-452e-b591-d780efa6ba1f",
      "name": "Determine Month Sheet",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1360,
        176
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "value": "1otpnzONRjloPoAXSv-cREMpc5jJ4CkxjXRbTIpJZcgY",
          "mode": "list",
          "cachedResultName": "demo 27.10. v1 - Expense Tracker AI Agent - autonoma.agency",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1otpnzONRjloPoAXSv-cREMpc5jJ4CkxjXRbTIpJZcgY/edit?usp=drivesdk"
        },
        "sheetName": {
          "__rl": true,
          "value": "={{ $json.output.month }}",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Date": "={{ $json.output.date }}",
            "Amount": "={{ $json.output.amount }}",
            "Category": "={{ $json.output.category }}",
            "Description": "={{ $json.output.description }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Date",
              "displayName": "Date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Amount",
              "displayName": "Amount",
              "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": "Category",
              "displayName": "Category",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {
          "locationDefine": {
            "values": {
              "headerRow": 13
            }
          }
        }
      },
      "id": "c2f64797-d960-4aab-9ac4-2918bdb2945e",
      "name": "Append to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        1552,
        176
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "1kDwbYdAIpiZBIKr",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.message.photo[3]?.file_id || $json.message.photo[2]?.file_id }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        544,
        48
      ],
      "id": "45d684f3-e5a1-4d1a-a404-fb914de2dcb8",
      "name": "Get a file",
      "webhookId": "b96a4376-24ff-4b23-92db-e1671b8dac8b",
      "credentials": {
        "telegramApi": {
          "id": "g4ig042auXSX6kTQ",
          "name": "Expense tracker agent"
        }
      }
    },
    {
      "parameters": {
        "resource": "image",
        "operation": "analyze",
        "modelId": {
          "__rl": true,
          "value": "chatgpt-4o-latest",
          "mode": "list",
          "cachedResultName": "CHATGPT-4O-LATEST"
        },
        "inputType": "base64",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        672,
        48
      ],
      "id": "1fd74005-85d9-4504-a578-76281ac31e38",
      "name": "Analyze image",
      "credentials": {
        "openAiApi": {
          "id": "80b8DOj8REc2MMqi",
          "name": "OpenAi account 9"
        }
      }
    },
    {
      "parameters": {
        "resource": "file",
        "fileId": "={{ $json.message.voice.file_id }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        544,
        272
      ],
      "id": "03655f76-80b7-48a3-a016-0bc42903a5c6",
      "name": "Get Audio",
      "webhookId": "b96a4376-24ff-4b23-92db-e1671b8dac8b",
      "credentials": {
        "telegramApi": {
          "id": "g4ig042auXSX6kTQ",
          "name": "Expense tracker agent"
        }
      }
    },
    {
      "parameters": {
        "resource": "audio",
        "operation": "transcribe",
        "options": {}
      },
      "id": "aea5af42-0b0b-4e00-a21b-d9f6ccc5483b",
      "name": "Transcribe Audio",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        672,
        272
      ],
      "credentials": {
        "openAiApi": {
          "id": "6LnEKBlR3JCiWGdk",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\n\t\"date\": \"2024-01-15\",\n\t\"description\": \"Coffee\",\n\t\"amount\": 3.50,\n\t\"category\": \"food\"\n}"
      },
      "id": "08e32b85-0c0c-483a-b059-685d2c11ac3a",
      "name": "Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        1216,
        368
      ]
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "id": "ac35251b-1b93-4c30-8cdf-fc42396aeacf",
      "name": "AI Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        1024,
        368
      ],
      "credentials": {
        "openAiApi": {
          "id": "6LnEKBlR3JCiWGdk",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "ed3a4e56-b31b-47c9-bd0d-4231a3fd2e56",
              "name": "text",
              "value": "={{ $json.content || $json.text || $json.message?.text }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        880,
        176
      ],
      "id": "dea7c2eb-1cd7-4dac-9b90-7fe5949c256c",
      "name": "Text"
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Route by Message Type",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by Message Type": {
      "main": [
        [
          {
            "node": "Get a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Text",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Audio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Expense Data": {
      "main": [
        [
          {
            "node": "Determine Month Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Determine Month Sheet": {
      "main": [
        [
          {
            "node": "Append to Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a file": {
      "main": [
        [
          {
            "node": "Analyze image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze image": {
      "main": [
        [
          {
            "node": "Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Audio": {
      "main": [
        [
          {
            "node": "Transcribe Audio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Transcribe Audio": {
      "main": [
        [
          {
            "node": "Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Parse Expense Data",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "AI Model": {
      "ai_languageModel": [
        [
          {
            "node": "Parse Expense Data",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Text": {
      "main": [
        [
          {
            "node": "Parse Expense Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "b4ab6477-e270-4818-8d06-c7461e784465",
  "meta": {
    "instanceId": "7ff6625f0127f3e9a7865e3493009de1c48f57dc5ed87def3ae3316be7f6b238"
  },
  "id": "PnaVsErQzkrwim2a",
  "tags": []
}