{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "c7f13578-6fa1-49df-aae5-6337c8bbec9a",
      "name": "Sticky Note - Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        128
      ],
      "parameters": {
        "width": 551,
        "height": 378,
        "content": "# \ud83d\udcb8 Telegram Expense Tracker\n**Log expenses, track weekly totals, and get budget alerts via Telegram.**\n\nThis workflow turns your Telegram chat into a personal finance assistant. It logs every expense into Google Sheets, calculates your weekly spend, and warns you instantly if you go over budget.\n\n## \ud83d\ude80 Key Features\n- **Instant Logging:** Type `/spent 5 coffee` to log expenses immediately.\n- **Budget Watchdog:** Receive an instant alert if your weekly spend exceeds your limit (Default: \u20ac100).\n- **Weekly Report:** get a summary of your total spending every Sunday.\n\n\ud83d\udc49 **Follow the detailed steps in the Setup Guide sticky note.**"
      },
      "typeVersion": 1
    },
    {
      "id": "b51c5305-db0a-4d1b-8d5e-192aa4bb62e5",
      "name": "Sticky Note - Daily Log",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        416,
        496
      ],
      "parameters": {
        "color": 4,
        "width": 1232,
        "height": 304,
        "content": "## Daily logged expenses in sheets\n"
      },
      "typeVersion": 1
    },
    {
      "id": "6370c17d-3a25-41b2-9b28-6e8766dca966",
      "name": "Sticky Note - Weekly Total",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        416,
        800
      ],
      "parameters": {
        "color": 5,
        "width": 1056,
        "height": 272,
        "content": "## Weekly Total \n"
      },
      "typeVersion": 1
    },
    {
      "id": "043a639f-0605-4e05-8ea2-fc2567fc7670",
      "name": "Telegram - Get Expense Command",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        464,
        592
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "62ab5d7a-5ece-4bd5-90b2-5e79a9257d8e",
      "name": "Parse Telegram Message",
      "type": "n8n-nodes-base.code",
      "position": [
        656,
        592
      ],
      "parameters": {
        "jsCode": "const msg = $json.message.text;   // e.g. \"/spent 4 coffee\"\nconst parts = msg.split(\" \");      // [\"/spent\", \"4\", \"coffee\"]\nreturn [{\n  json: {\n    amount: parseFloat(parts[1]),\n    description: parts.slice(2).join(\" \"),\n    date: new Date().toISOString().split(\"T\")[0] // YYYY-MM-DD\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "467d4587-d057-4b7a-b092-f2466491dc5a",
      "name": "Google Sheets - Log Expense",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        848,
        592
      ],
      "parameters": {
        "columns": {
          "value": {
            "Date ": "={{ $json.date }}",
            "Amount ": "={{ $json.amount }}",
            "Description": "={{ $json.description }}"
          },
          "schema": [
            {
              "id": "Date ",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Date ",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Amount ",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Amount ",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Description",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Description",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "<YOUR_SHEET_ID_HERE>"
        }
      },
      "typeVersion": 4.6
    },
    {
      "id": "2ccf8900-b961-4b8a-aee8-327861e0b2e9",
      "name": "Weekly Summary Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        480,
        896
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtHour": 11
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "f6f5a5b3-22b0-4904-86ab-6ce04e4d6cb4",
      "name": "Calculate Weekly Total",
      "type": "n8n-nodes-base.code",
      "position": [
        896,
        896
      ],
      "parameters": {
        "jsCode": "const rows = items.map(item => item.json);\n\nconst now = new Date();\nconst weekAgo = new Date();\nweekAgo.setDate(now.getDate() - 7);\n\nlet total = 0;\nfor (let r of rows) {\n  // Handle column name issues (trim spaces in keys)\n  const dateStr = r[\"Date\"] || r[\"Date \"] || r[\"date\"];\n  const amountStr = r[\"Amount\"] || r[\"Amount \"] || r[\"amount\"];\n\n  // Try parsing date safely (Google Sheets often gives YYYY-MM-DD if set to ISO)\n  const d = new Date(dateStr);\n\n  if (!isNaN(d)) {\n    if (d >= weekAgo && d <= now) {\n      total += parseFloat(amountStr) || 0;\n    }\n  }\n}\n\nreturn [{\n  json: { summary: `\ud83d\udcb0 Your total expenses this week: \u20ac${total.toFixed(2)}` }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "62a48c4a-7445-4f3b-bc2c-0107f813c288",
      "name": "Telegram - Send Weekly Summary",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1104,
        896
      ],
      "parameters": {
        "text": "={{$json[\"summary\"]}}",
        "chatId": "<YOUR_CHAT_ID_HERE>",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "19fc0518-2cba-4c74-92bf-ddad3905ee2d",
      "name": "Sticky Note - Setup Guide",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        512
      ],
      "parameters": {
        "color": 6,
        "width": 551,
        "height": 791,
        "content": "## \ud83d\udee0\ufe0f Setup Guide\n### **[Gtaras](https://n8n.io/creators/tarasidis/)**  \n\n### 1\ufe0f\u20e3 Telegram Setup\n1. Open Telegram and search for **@BotFather**.\n2. Send `/newbot` and follow instructions to get your **API Token**.\n3. Create a Telegram Credential in n8n with this token.\n4. Send a message to your new bot (e.g. \"Hello\").\n5. Visit `https://api.telegram.org/bot<YOUR_TOKEN>/getUpdates`.\n6. Look for `\"chat\": { \"id\": 123456789 }`. Copy this number (Chat ID).\n\n### 2\ufe0f\u20e3 Google Sheets Setup\n\ud83d\udd17 **[Make a copy of the template](https://docs.google.com/spreadsheets/d/1uyQpX9_ZZUhLtBhyxfjdU80D6Q6cqbMV286MnhF5QBY/copy)**  \n1. Create a new Google Sheet.\n2. In the first row, add these **exact** headers:\n   - Column A: `Date`\n   - Column B: `Amount`\n   - Column C: `Description`\n3. Copy the **Spreadsheet ID** from the URL (it is the long string between `/d/` and `/edit`).\n\n### 3\ufe0f\u20e3 Configure the Workflow\n1. **Google Sheets Nodes:** Open all 3 Google Sheets nodes and paste your Spreadsheet ID.\n2. **Telegram Nodes:** Open \"Send Weekly Summary\" and \"Send Budget Alert\" and paste your Chat ID.\n3. **Set Budget Limit:** Open the \"Check Weekly Budget\" Code node and change `if (total > 100)` to your desired limit.\n\n### 4\ufe0f\u20e3 Activate\n1. Save the workflow and click **Active** (top right).\n2. Test it by sending `/spent 10 lunch` to your bot."
      },
      "typeVersion": 1
    },
    {
      "id": "615e900b-219c-4a1e-8a23-d4a9f04733aa",
      "name": "Google Sheets - Clean Up (Optional)",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1312,
        896
      ],
      "parameters": {
        "operation": "delete",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "<YOUR_SHEET_ID_HERE>"
        },
        "numberToDelete": 30
      },
      "typeVersion": 4.7
    },
    {
      "id": "015dad52-8d68-4f79-9c16-bf6676251e7a",
      "name": "Google Sheets - Get Weekly Expenses",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        688,
        896
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "<YOUR_SHEET_ID_HERE>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "a3dc986d-be75-489f-b44d-d2a7c2c14fa6",
      "name": "Google Sheets - Get Expenses (Realtime)",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1072,
        592
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "<YOUR_SHEET_ID_HERE>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "5f56fb27-24ce-4a80-976a-8e5e946df192",
      "name": "Check Weekly Budget",
      "type": "n8n-nodes-base.code",
      "position": [
        1280,
        592
      ],
      "parameters": {
        "jsCode": "const rows = items.map(item => item.json);\n\nlet total = 0;\nfor (let r of rows) {\n  const amountStr = r[\"Amount\"] || r[\"Amount \"] || r[\"amount\"];\n  total += parseFloat(amountStr) || 0;\n}\n\n// SET YOUR WEEKLY BUDGET LIMIT HERE\nconst budgetLimit = 100;\n\nif (total > budgetLimit) {\n  return [{\n    json: {\n      alert: `\u26a0\ufe0f Warning! You have exceeded your weekly budget of \u20ac${budgetLimit} (Current total: \u20ac${total.toFixed(2)})`\n    }\n  }];\n} else {\n  return [];\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "1a067c3b-c726-4c2d-b436-ba78647620cb",
      "name": "Telegram - Send Budget Alert",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1472,
        592
      ],
      "parameters": {
        "text": "={{$json[\"alert\"]}}",
        "chatId": "<YOUR_CHAT_ID_HERE>",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    }
  ],
  "connections": {
    "Check Weekly Budget": {
      "main": [
        [
          {
            "node": "Telegram - Send Budget Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Weekly Total": {
      "main": [
        [
          {
            "node": "Telegram - Send Weekly Summary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Telegram Message": {
      "main": [
        [
          {
            "node": "Google Sheets - Log Expense",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Weekly Summary Trigger": {
      "main": [
        [
          {
            "node": "Google Sheets - Get Weekly Expenses",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Log Expense": {
      "main": [
        [
          {
            "node": "Google Sheets - Get Expenses (Realtime)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram - Get Expense Command": {
      "main": [
        [
          {
            "node": "Parse Telegram Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram - Send Weekly Summary": {
      "main": [
        [
          {
            "node": "Google Sheets - Clean Up (Optional)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Get Weekly Expenses": {
      "main": [
        [
          {
            "node": "Calculate Weekly Total",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets - Get Expenses (Realtime)": {
      "main": [
        [
          {
            "node": "Check Weekly Budget",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}