{
  "id": "oa2HPa75Tv0eg4UK",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Query personal finance data in Notion via Telegram and WhatsApp",
  "tags": [],
  "nodes": [
    {
      "id": "1dc32a36-fbd0-42f3-ac17-4793146b433b",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        624,
        256
      ],
      "parameters": {
        "sessionKey": "1",
        "sessionIdType": "customKey",
        "contextWindowLength": 10
      },
      "typeVersion": 1.3
    },
    {
      "id": "953a2f07-2e7f-456e-8482-ed574652f5a3",
      "name": "Send message",
      "type": "n8n-nodes-base.whatsApp",
      "position": [
        1248,
        -64
      ],
      "parameters": {
        "textBody": "={{ $json.output }}",
        "operation": "send",
        "phoneNumberId": "689568790917776",
        "additionalFields": {},
        "recipientPhoneNumber": "={{ \"recipent phone number\" }}"
      },
      "credentials": {
        "whatsAppApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "96364ebf-57cc-4586-a4f4-b915f4143e34",
      "name": "Accountant Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        688,
        32
      ],
      "parameters": {
        "text": "=Query: {{ $json.message.text}}\nCurrent Date {{ $now }}",
        "options": {
          "systemMessage": "=# Overview\nYou are a Accountant Agent who can retrieve and summarize information from all my notion databases relating to my personal finance using the 3 tools.\n\n# Tools\n1) Get Databases Tool - Get list of all databases with name and id.\n2) Get Notion Database Properties Tool - Get all the properties from a database based on database id.\n3) Query Notion Database Pages Tool - Get the list of rows based on database id and filters created based on input\n\n# Instructions\n1) Use the \"Get Databases Tool\" to get the list of databases from you notion account and return the database id based on selected database based on name similarity with input.\n2) Use the \"Get Notion Database Properties Tool\" to get the properties of the databases based on databaseId selected\n3) Based on the properties obtained from previous step and user input create a notion filter json.\n4) Use the \"Query Notion Database Pages Tool\", with the notion filter json on the database id obtained before to get the data you requested from notion database.\n\n# Examples\n\n1) Always use the **Financial Transactions** database when user input mentions expenses or transactions.  \n   - `Amount` \u2192 property for the money value  \n   - `Date` \u2192 property for the transaction date  \n   - `Type` (or similar) \u2192 filter where value = \"Expense\"\n\n2) When the user asks \u201cthis month\u201d, build a Notion filter JSON:\n```json\n{\n  \"and\": [\n    { \"property\": \"Type\", \"select\": { \"equals\": \"Expense\" } },\n    { \"property\": \"Date\", \"date\": { \"on_or_after\": \"YYYY-MM-01\" } },\n    { \"property\": \"Date\", \"date\": { \"on_or_before\": \"YYYY-MM-31\" } }\n  ]\n}\n```\n\n3) Sum all the `Amount` values from the filtered rows and return the total.\n\n# Notes\n- Expenses \u2192 **Financial Transaction Database**  \n- Budget \u2192 **Budget Database**  \n- Funds (yearly plan to spend on big things or irregular expenditure) \u2192 **Funds Database**\n- Income -> **Income Database**\n- Assets and Liabilities -> **Assets and Liabilities**",
          "returnIntermediateSteps": true
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "faafeeeb-939b-409c-8bf4-b30a145bf058",
      "name": "Get Databases Tool",
      "type": "n8n-nodes-base.notionTool",
      "position": [
        752,
        256
      ],
      "parameters": {
        "resource": "database",
        "operation": "getAll",
        "returnAll": true
      },
      "credentials": {
        "notionApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "863b4560-b048-4d80-8ac7-98d083615b22",
      "name": "Query Notion Database Pages Tool",
      "type": "n8n-nodes-base.httpRequestTool",
      "position": [
        896,
        256
      ],
      "parameters": {
        "url": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('URL', \"Notion api url to get database rows with database id like - `https://api.notion.com/v1/databases/{database_id}/query`\", 'string') }}",
        "method": "POST",
        "options": {},
        "jsonBody": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('JSON', ``, 'json') }}",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "predefinedCredentialType",
        "toolDescription": "Get rows from Notion Database based on database id and filters                                             ",
        "nodeCredentialType": "notionApi"
      },
      "credentials": {
        "notionApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "49a583b8-6fa2-46b7-a098-526952d48ad2",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        304,
        32
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "877008f0-db36-4689-a06b-ad15465f88f6",
      "name": "Send a text message",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1248,
        128
      ],
      "parameters": {
        "text": "={{ $json.output }}",
        "chatId": "={{\"chat_id\"}}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "cdddeed2-10bf-4a8c-b333-56b86dd4b168",
      "name": "Get Notion Database Properties Tool",
      "type": "n8n-nodes-base.httpRequestTool",
      "position": [
        1056,
        256
      ],
      "parameters": {
        "url": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('URL', \"Notion api to get database properties with database id like - `https://api.notion.com/v1/databases/{database_id}`\", 'string') }}",
        "options": {},
        "authentication": "predefinedCredentialType",
        "toolDescription": "Get Properties of notion database",
        "nodeCredentialType": "notionApi"
      },
      "credentials": {
        "notionApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "1b1a008b-322e-4a79-9d03-d9c2bfcad942",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        480,
        -144
      ],
      "parameters": {
        "width": 704,
        "height": 528,
        "content": "## Step 2 - Agent\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "02e7fb48-5f2d-4e82-9736-e0187b1f184a",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        272,
        -144
      ],
      "parameters": {
        "color": 4,
        "width": 208,
        "height": 528,
        "content": "## Step 1 - Trigger\n"
      },
      "typeVersion": 1
    },
    {
      "id": "66ac405f-9fe2-4702-959d-5f7a38a0ddd0",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1184,
        -144
      ],
      "parameters": {
        "color": 3,
        "width": 288,
        "height": 528,
        "content": "## Step 3 - Send Message\n\n\n"
      },
      "typeVersion": 1
    },
    {
      "id": "dce6393b-994d-421c-ae76-4a9561ebe388",
      "name": "claude-3.5-sonnet",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        496,
        256
      ],
      "parameters": {
        "model": "anthropic/claude-3.5-sonnet",
        "options": {}
      },
      "credentials": {
        "openRouterApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "bacc7b49-798d-4b2d-91cc-843b4d94c1fb",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        272,
        384
      ],
      "parameters": {
        "color": 5,
        "width": 1200,
        "height": 1408,
        "content": "## \ud83d\udcdd Query personal finance data in Notion via Telegram and WhatsApp\n\n#### This workflow acts as an **Accountant Agent** to automatically answer questions about your personal finances by retrieving and summarizing data from your Notion databases.\n\n### **Step 1 \u2013 Trigger**\n\n* The workflow starts when you send a query (for example: *\u201cTotal expenses for August 2025\u201d*) via **Telegram** or **WhatsApp**.\n* The message is captured by the **Telegram Trigger** node.\n* Example input:\n\n  * `Total expenses for August 2025`\n\n### **Step 2 \u2013 Accountant Agent**\n\n* The query is passed to the **Accountant Agent** (powered by GPT-4o via OpenRouter).\n* The Agent uses **Simple Memory** to maintain conversation context.\n* The Agent has access to three Notion tools:\n\n  1. **Get Databases Tool** \u2192 Fetches all databases (e.g., Financial Transactions, Budget, Income).\n  2. **Get Notion Database Properties Tool** \u2192 Reads the schema of the selected database.\n  3. **Query Notion Database Pages Tool** \u2192 Pulls rows based on filters created from your query.\n\n### **How the Agent Works**\n\n1. **Identifies the right database**:\n\n   * Expenses/Transactions \u2192 **Financial Transactions** database\n   * Income \u2192 **Income Database**\n   * Assets/Liabilities \u2192 **Assets & Liabilities** database\n   * Budget \u2192 **Budget Database**\n\n2. **Creates the Notion filter JSON** based on your query.\n   Example for *\u201cTotal expenses this month\u201d*:\n\n   ```json\n   {\n     \"and\": [\n       { \"property\": \"Type\", \"select\": { \"equals\": \"Expense\" } },\n       { \"property\": \"Date\", \"date\": { \"on_or_after\": \"2025-08-01\" } },\n       { \"property\": \"Date\", \"date\": { \"on_or_before\": \"2025-08-31\" } }\n     ]\n   }\n   ```\n\n3. **Runs the query**, fetches all matching rows, and sums the `Amount` field.\n\n### **Step 3 \u2013 Response**\n\n* The summarized result (like total expenses or income) is sent back to you via:\n\n  * **Telegram** (chat message)\n  * **WhatsApp** (text message)\n\n* Example output:\n\n  * *\u201cYour total expenses for August 2025 are \u20b942,350.\u201d*\n\n\u2705 With this setup, you just type queries in natural language, and the bot automatically talks to Notion, runs the calculations, and sends answers back to your phone instantly.\n "
      },
      "typeVersion": 1
    },
    {
      "id": "4ed7acf4-7d71-4aa5-ab1f-274bbce5cace",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -928,
        -144
      ],
      "parameters": {
        "color": 7,
        "width": 1200,
        "height": 3824,
        "content": "## Create Account and Get Credentials for the below Tools\n\n* **Telegram**: Bot Token, **Chat ID** (or a recent **Message ID** for testing)\n* **OpenRouter**: API Key\n* **Notion**: Integration Token\n* **WhatsApp (Meta)**: Phone Number ID, WhatsApp Business Account ID, **Permanent Access Token**\n\n## 1) Telegram \u2014 Create Bot & Get IDs\n\n### 1.1 Create a bot with **@BotFather**\n\n1. Open Telegram \u2192 search **BotFather** \u2192 Start.\n2. Send: `/newbot`\n3. Give it a **name** (e.g., `Accountant AI`), then a **username** ending in `bot` (e.g., `accountant_ai_helper_bot`).\n4. BotFather returns a **HTTP API Token** \u2192 **save it** (this is your **Bot Token**).\n\n### 1.2 Get your **Chat ID** (two quick methods)\n\n**Method A \u2014 via n8n trigger (easiest):**\n\n1. In n8n, add **Telegram Trigger** node and connect your bot token in credentials.\n2. Start the workflow \u2192 send any message to your bot.\n3. Run once / listen \u2192 inspect the incoming message in the node output \u2192 copy `chat.id`.\n\n**Method B \u2014 via Telegram API (curl):**\n\n1. First, send a message to your bot so there is at least one update.\n2. Run:\n\n```bash\ncurl -s \"https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates\" | jq\n```\n\n3. Find `result[0].message.chat.id` \u2192 that\u2019s your **Chat ID**.\n\n> You can also grab a **Message ID** from `result[0].message.message_id` for testing.\n\n### 1.3 Plug into n8n\n\n* **Telegram Trigger**: use your Bot Token.\n* **Telegram node (Send Message)**: set `chatId = <YOUR_CHAT_ID>` (or pass from previous node JSON).\n\n**Suggested env vars**\n\n```bash\nTELEGRAM_BOT_TOKEN=XXX:YYYY\nTELEGRAM_CHAT_ID=123456789\n```\n\n## 2) OpenRouter \u2014 API Key\n\n1. Sign up / log in to **OpenRouter**.\n2. Create a new **API Key**.\n3. In n8n \u2192 **Credentials** \u2192 choose your OpenRouter credential type \u2192 paste the key.\n4. In your LLM node, set the **model** you plan to use (e.g., `anthropic/claude-3.5-sonnet`).\n\n\n## 3) Notion \u2014 Duplicate Template & Get Credentials\n\n### 3.1 Duplicate your Notion template\n\n1. Open [Personal Finance System Notion template link](https://www.notion.so/templates/personal-finance-system) ( \u2192 click **Duplicate** (top-right) to add it to your workspace.\n\n### 3.2 Create a Notion **Integration** (Internal)\n\n1. Go to **Notion \u2192 Settings & members \u2192 Connections \u2192 Develop or Manage integrations**.\n2. Create **New integration** \u2192 give it a name (e.g., `Accountant AI`), select your workspace.\n3. Copy the **Internal Integration Token** (bearer token).\n4. In the Integrations sidebar tab, click on the integration 'Accountant AI', select \"Access\" Tab, click on \"Edit access\" tab, and in the Teamspaces Pages, find \"Personal Finance System\" Page, check it and Update the Access.\n\n### 3.3 Plug into n8n\n\n* Use the built-in **Notion** credentials (paste `NOTION_TOKEN`).\n* For custom HTTP Request Tool nodes, target endpoints like:\n\n  * `POST https://api.notion.com/v1/databases/{database_id}/query`\n  * `GET  https://api.notion.com/v1/databases/{database_id}` (for properties)\n* Required headers:\n\n```http\nAuthorization: Bearer <NOTION_TOKEN>\nNotion-Version: 2022-06-28\nContent-Type: application/json\n```\n\n\n## 4) WhatsApp (Meta) \u2014 Business Setup & Credentials\n\n### 4.1 Prereqs\n\n* **Facebook Business Manager** account\n* **Meta App** (in **Developers**)\n* A **phone number** for WhatsApp Business (can be a fresh number)\n\n### 4.2 Create a WhatsApp Business app\n\n1. Go to **developers.facebook.com** \u2192 **My Apps** \u2192 **Create App** \u2192 choose **Business** (or **Other** with WhatsApp product).\n2. Add the **WhatsApp** product to your app.\n3. In **WhatsApp \u2192 Getting Started**, link a **WhatsApp Business Account (WABA)**.\n\nYou\u2019ll need to note:\n\n* **WhatsApp Business Account ID (WABA ID)**\n* **Phone Number ID**\n* **Access Token** (start with a **temporary** one for testing)\n\n### 4.3 Generate a **Permanent Access Token** (recommended)\n\n* In **Business Settings \u2192 System Users**: create a **System User** \u2192 assign **WhatsApp** permissions \u2192 **Generate Token** for that user with **never expires** option. Store **App ID/Secret** too.\n\n**Env vars**\n\n```bash\nMETA_WABA_ID=1234567890\nMETA_PHONE_NUMBER_ID=111222333444555\nMETA_ACCESS_TOKEN=EAAG... (long string)\n```\n\n### 4.4 Send a message via Graph API\n\n**Curl test**\n\n```bash\ncurl -X POST \\\n  https://graph.facebook.com/v20.0/${META_PHONE_NUMBER_ID}/messages \\\n  -H \"Authorization: Bearer ${META_ACCESS_TOKEN}\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"messaging_product\": \"whatsapp\",\n    \"recipient_type\": \"individual\",\n    \"to\": \"<E164_PHONE_NUMBER>\",\n    \"type\": \"text\",\n    \"text\": { \"preview_url\": false, \"body\": \"Hello from n8n!\" }\n  }'\n```\n\n**In n8n**\n\n* Use the **WhatsApp node** (or **HTTP Request** node) with the same endpoint.\n* Required fields: `phoneNumberId`, `to`, and a `text` or template payload.\n\n**Templates (optional, recommended for outbound)**\n\n* In **WABA \u2192 Message Templates**, create and **submit for approval** (e.g., `finance_update`).\n* Once approved, you can send templated notifications without prior user messages.\n\n---\n\n## 5) Recommended Credential Names in n8n\n\n* **Telegram API**: `Accountant AI` (Bot Token)\n* **OpenRouter**: `OpenRouter account`\n* **Notion API**: `Notion account`\n* **WhatsApp API**: `Accountant AI` (WABA credentials)\n\nMatch these names in your existing nodes so you don\u2019t have to rewire connections.\n\n---\n\n## 6) Minimal sanity checks\n\n* **Telegram**: `getUpdates` returns your sent message \u2192 Chat ID correct\n* **OpenRouter**: Call a trivial prompt node \u2192 no 401\n* **Notion**: `GET /v1/databases/{id}` works \u2192 Integration has access\n* **WhatsApp**: `POST /{PHONE_NUMBER_ID}/messages` returns `messages[0].id` \u2192 token/ID valid\n\n---\n\n## 7) Quick reference \u2014 Headers & Endpoints\n\n**Notion**\n\n```http\nBase: https://api.notion.com/v1\nAuth: Authorization: Bearer <NOTION_TOKEN>\nVer:  Notion-Version: 2022-06-28\n```\n\n* Query DB: `POST /databases/{id}/query`\n* Get DB:  `GET  /databases/{id}`\n\n**Telegram**\n\n* `GET https://api.telegram.org/bot<BOT_TOKEN>/getUpdates`\n* `POST https://api.telegram.org/bot<BOT_TOKEN>/sendMessage`\n\n**WhatsApp (Graph API)**\n\n* `POST https://graph.facebook.com/v20.0/{PHONE_NUMBER_ID}/messages`\n* Webhooks: App dashboard \u2192 WhatsApp \u2192 **Configure Webhooks**\n\n---\n\n## 8) Security tips\n\n* Never commit tokens to Git. Use n8n **Credentials** or environment variables.\n* Rotate long\u2011lived tokens periodically.\n* Scope the Notion integration to **read\u2011only** unless you plan to write back.\n* Restrict who can message your Telegram bot (check chat IDs in workflow).\n\n---\n\n## 9) Plug into your existing workflow\n\n* Update the **Telegram Trigger** and **Telegram Send** nodes with the new **Telegram credential** + `messageId`.\n* Confirm the **LLM node** uses the **OpenRouter** credential and desired model.\n* Ensure the **Notion** HTTP Tool nodes use the **Notion** credential.\n* For **WhatsApp**, point your **Send message** node to the correct `phoneNumberId` and ensure the access token is valid (permanent if possible).\n\n> Once these are in, your Personal Finance workflow can answer \u201cTotal expenses this month?\u201d via Telegram without manual lookups. \ud83d\ude80\n"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2e743705-8bc3-4055-9f4b-12d66d341327",
  "connections": {
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "Accountant Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Accountant Agent": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Send message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Accountant Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "claude-3.5-sonnet": {
      "ai_languageModel": [
        [
          {
            "node": "Accountant Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Get Databases Tool": {
      "ai_tool": [
        [
          {
            "node": "Accountant Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message": {
      "main": [
        []
      ]
    },
    "Query Notion Database Pages Tool": {
      "ai_tool": [
        [
          {
            "node": "Accountant Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Get Notion Database Properties Tool": {
      "ai_tool": [
        [
          {
            "node": "Accountant Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    }
  }
}