{
  "name": "TelegramQuery",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "seconds",
              "secondsInterval": 10
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        -1424,
        240
      ],
      "id": "748c04b3-9c77-4ced-a819-da83cf3a515b",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "url": "=https://api.telegram.org/bot{{ $('TelegramToken').item.json.token }}/getUpdates?timeout=8&limit=1&offset={{ $('TelegramOffset').item.json.offset }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        -688,
        240
      ],
      "id": "bfb2c9cd-e0a9-49ce-a50b-2f0cc44a74b1",
      "name": "HTTP Request"
    },
    {
      "parameters": {
        "operation": "get",
        "dataTableId": {
          "__rl": true,
          "value": "telegram_offset",
          "mode": "name"
        },
        "matchType": "allConditions",
        "limit": 1
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        -880,
        240
      ],
      "id": "3180c8fe-9a4d-4586-b5e7-981fc69b6da9",
      "name": "TelegramOffset"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "47e9db2b-a30f-4853-beb0-9d08686e76d9",
              "leftValue": "={{ $json.result.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "notEquals"
              }
            },
            {
              "id": "70643a47-1d0f-4af8-94df-595d22e967b0",
              "leftValue": "='{{ $json.result[0].message.chat.id }}'",
              "rightValue": "='{{ $('TelegramToken').item.json.group_id }}'",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -496,
        240
      ],
      "id": "43af7508-c1d6-4da6-8df6-b3838ef87146",
      "name": "If"
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "value": "telegram_offset",
          "mode": "name"
        },
        "filters": {
          "conditions": [
            {
              "keyValue": "1"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "offset": "={{ $json.result.first().update_id+1 }}"
          },
          "matchingColumns": [
            "offset"
          ],
          "schema": [
            {
              "id": "offset",
              "displayName": "offset",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        128,
        544
      ],
      "id": "c989a2e3-92bc-4b01-b696-7e89ad5c64fc",
      "name": "Update row(s)"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.result[0].message.text }}",
                    "rightValue": "/totalspend",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    },
                    "id": "1d3475ff-ee94-48c5-a6f1-054274b5cf48"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "GetTotalSpendings"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "9267e0c2-9382-4bed-a8a4-88fad7b4fc08",
                    "leftValue": "={{ $json.result[0].message.text }}",
                    "rightValue": "/spendbycards",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "GetTotalSpendByCards"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "cc296383-de2e-49b2-a663-d32878f4fed6",
                    "leftValue": "={{ $json.result[0].message.text }}",
                    "rightValue": "/updatemcc",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "UpdateMCC"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 3
                },
                "conditions": [
                  {
                    "id": "cc153695-0540-4d23-b446-de76521faff7",
                    "leftValue": "",
                    "rightValue": "",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.4,
      "position": [
        -288,
        144
      ],
      "id": "1fdbe15e-6292-4655-a307-c762475a2bd2",
      "name": "Switch"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT SUM(amount_base_currency) as total_amount FROM transactions;",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        0,
        0
      ],
      "id": "d6ef12f7-5ba8-4f72-94db-4ab915dc51c2",
      "name": "Execute a SQL query",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('TelegramToken').item.json.group_id }}",
        "text": "=\ud83d\udcb8 <b>Total Spendings</b>\n\n<b>Total Amount: SGD {{ $json.total_amount }}</b>",
        "additionalFields": {
          "appendAttribution": false,
          "parse_mode": "=HTML"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        176,
        0
      ],
      "id": "989eecd4-22af-4dc7-9dce-f2289d72627c",
      "name": "Send a text message",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "value": "telegram_offset",
          "mode": "name"
        },
        "filters": {
          "conditions": [
            {
              "keyValue": "1"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "offset": "={{ $json.result.first().update_id+1 }}"
          },
          "matchingColumns": [
            "offset"
          ],
          "schema": [
            {
              "id": "offset",
              "displayName": "offset",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        -176,
        592
      ],
      "id": "f31be11d-6cbc-4d7d-91ff-e7762aafb6b9",
      "name": "Update row(s)2"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT \n    SUM(transactions.amount_base_currency) AS total_amount,\n    ACCOUNTS.identifier as id,\n    ACCOUNTS.card_alias AS card_alias,\n    ACCOUNTS.bank AS bank\nFROM \n    transactions\nINNER JOIN \n    ACCOUNTS \nON \n    ACCOUNTS.id = transactions.account_id\nGROUP BY \n    ACCOUNTS.bank,\n    ACCOUNTS.identifier,\n    ACCOUNTS.card_alias;\n",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        160,
        144
      ],
      "id": "4792e52d-fe07-41ad-b32c-8a69af7badf5",
      "name": "Execute a SQL query1",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('TelegramToken').item.json.group_id }}",
        "text": "=Card/Wallet: {{ ($json.card_alias) || ($json.bank + ' ' + $json.id) }}\nTotal Amount Spent: SGD {{ $json.total_amount }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        384,
        144
      ],
      "id": "e2238a0e-e8a4-4f37-afb3-efbc18d2ef2d",
      "name": "Send a text message1",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "value": "telegram_offset",
          "mode": "name"
        },
        "filters": {
          "conditions": [
            {
              "keyValue": "1"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "offset": "={{ $('HTTP Request').item.json.result[0].update_id+1 }}"
          },
          "matchingColumns": [
            "offset"
          ],
          "schema": [
            {
              "id": "offset",
              "displayName": "offset",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        592,
        144
      ],
      "id": "6face375-e4cf-4e90-af47-df665da8605c",
      "name": "Update row(s)3"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "4347c406-71b4-401a-9c5b-7813f134b6b4",
              "leftValue": "={{ $json.result.length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "notEquals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        -400,
        608
      ],
      "id": "c7306e1b-aa76-48fd-9a48-4b07bf5c9b7d",
      "name": "If1"
    },
    {
      "parameters": {
        "resource": "table",
        "operation": "create",
        "tableName": "telegram_offset",
        "columns": {
          "column": [
            {
              "name": "offset",
              "type": "number"
            }
          ]
        },
        "options": {
          "createIfNotExists": true
        }
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        -1248,
        240
      ],
      "id": "0979068c-96ef-45b8-bf1d-e8d8665b7e79",
      "name": "Create a data table"
    },
    {
      "parameters": {
        "dataTableId": {
          "__rl": true,
          "value": "telegram_offset",
          "mode": "name"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {},
          "matchingColumns": [
            "offset"
          ],
          "schema": [
            {
              "id": "offset",
              "displayName": "offset",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        -720,
        464
      ],
      "id": "5340268a-5421-4334-86ca-a1f22f589088",
      "name": "Insert row",
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "operation": "rowNotExists",
        "dataTableId": {
          "__rl": true,
          "value": "telegram_offset",
          "mode": "name"
        },
        "matchType": "allConditions",
        "filters": {
          "conditions": [
            {
              "keyValue": "1"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        -928,
        464
      ],
      "id": "3cc0edd8-ce8d-440d-820d-098fae531100",
      "name": "If row does not exist",
      "alwaysOutputData": false
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "value": "telegram_offset",
          "mode": "name"
        },
        "filters": {
          "conditions": [
            {
              "keyValue": "1"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "offset": "={{ $('HTTP Request').item.json.result[0].update_id+1 }}"
          },
          "matchingColumns": [
            "offset"
          ],
          "schema": [
            {
              "id": "offset",
              "displayName": "offset",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        352,
        0
      ],
      "id": "e689d713-7b39-4242-bf5e-0c2c2b3505bb",
      "name": "Update row(s)1"
    },
    {
      "parameters": {
        "operation": "select",
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "table": {
          "__rl": true,
          "value": "telegram",
          "mode": "list",
          "cachedResultName": "telegram"
        },
        "limit": 1,
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        -1072,
        240
      ],
      "id": "2954f881-12c5-4cfd-8ac7-86875b55fb1f",
      "name": "TelegramToken",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const text = String($input.item.json?.result?.[0]?.message?.text || '').trim();\n\nlet transactionId = null;\nlet mccCode = null;\n\nconst exact = text.match(/\\/?updatemcc\\s+(\\d+)\\s+(\\d{3,4})/i);\nif (exact) {\n  transactionId = Number(exact[1]);\n  mccCode = exact[2].padStart(4, '0');\n} else {\n  const numericTokens = text.match(/\\d+/g) || [];\n  if (numericTokens.length >= 2) {\n    transactionId = Number(numericTokens[0]);\n    mccCode = numericTokens[1].slice(-4).padStart(4, '0');\n  }\n}\n\n$input.item.json.command_text = text;\n$input.item.json.transaction_id = Number.isFinite(transactionId) ? transactionId : null;\n$input.item.json.mcc_code = mccCode;\n$input.item.json.is_valid_updatemcc = Number.isFinite(transactionId) && /^\\d{4}$/.test(String(mccCode || ''));\n\nreturn $input.item;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        128,
        352
      ],
      "id": "bd11fb4c-d7f9-4499-b7e4-d9dafeca065b",
      "name": "Code in JavaScript"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "5e349434-6e4e-4a07-bc5d-f9f4d1705289",
              "leftValue": "={{ $json.is_valid_updatemcc }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        240,
        352
      ],
      "id": "2081bee6-345b-4b9a-babe-055e7bb20895",
      "name": "IfUpdateMCCInputValid"
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "WITH input AS (\n    SELECT\n      {{ $json.transaction_id || 0 }}::BIGINT AS tx_id,\n      CASE\n        WHEN '{{ $json.mcc_code || '' }}' = '' THEN NULL\n        ELSE LPAD('{{ $json.mcc_code || '' }}', 4, '0')\n      END::TEXT AS requested_mcc\n),\nmcc_meta AS (\n    SELECT\n      i.tx_id,\n      i.requested_mcc AS mcc_code,\n      COALESCE(\n        NULLIF(m.edited_description, ''),\n        NULLIF(m.combined_description, ''),\n        NULLIF(m.usda_description, ''),\n        NULLIF(m.irs_description, ''),\n        'Manual MCC Update'\n      ) AS mcc_description\n    FROM input i\n    LEFT JOIN mcc_reference m ON m.mcc = i.requested_mcc\n),\ntarget_tx AS (\n    SELECT\n      t.id,\n      t.merchant_id,\n      t.merchant,\n      trim(regexp_replace(replace(upper(COALESCE(t.merchant, '')), ' ', ''), '[^A-Z0-9]', '', 'g')) AS merchant_normalized\n    FROM transactions t\n    JOIN input i ON i.tx_id = t.id\n),\nupdate_existing_cache AS (\n    UPDATE merchant_mcc_cache c\n    SET\n      mcc_code = mm.mcc_code,\n      mcc_description = mm.mcc_description,\n      category = mm.mcc_description,\n      manual_override = TRUE,\n      updated_at = NOW()\n    FROM target_tx tt\n    CROSS JOIN mcc_meta mm\n    WHERE c.id = tt.merchant_id\n      AND mm.mcc_code IS NOT NULL\n    RETURNING c.id, c.mcc_code, c.mcc_description, c.category\n),\nupsert_missing_cache AS (\n    INSERT INTO merchant_mcc_cache (\n      merchant_normalized,\n      merchant_original,\n      mcc_code,\n      mcc_description,\n      category,\n      manual_override,\n      updated_at\n    )\n    SELECT\n      COALESCE(NULLIF(tt.merchant_normalized, ''), 'TX-' || tt.id::TEXT),\n      tt.merchant,\n      mm.mcc_code,\n      mm.mcc_description,\n      mm.mcc_description,\n      TRUE,\n      NOW()\n    FROM target_tx tt\n    CROSS JOIN mcc_meta mm\n    WHERE tt.merchant_id IS NULL\n      AND mm.mcc_code IS NOT NULL\n    ON CONFLICT (merchant_normalized) DO UPDATE\n    SET\n      merchant_original = EXCLUDED.merchant_original,\n      mcc_code = EXCLUDED.mcc_code,\n      mcc_description = EXCLUDED.mcc_description,\n      category = EXCLUDED.category,\n      manual_override = TRUE,\n      updated_at = NOW()\n    RETURNING id, mcc_code, mcc_description, category\n),\nresolved_cache AS (\n    SELECT id, mcc_code, mcc_description, category FROM update_existing_cache\n    UNION ALL\n    SELECT id, mcc_code, mcc_description, category FROM upsert_missing_cache\n),\nupdate_tx AS (\n    UPDATE transactions t\n    SET\n      merchant_id = rc.id,\n      mcc_code = rc.mcc_code,\n      category = COALESCE(rc.category, rc.mcc_description)\n    FROM resolved_cache rc\n    WHERE t.id = (SELECT tx_id FROM input)\n    RETURNING t.id AS transaction_id, t.merchant_id, t.mcc_code, t.category\n),\nstatus_calc AS (\n    SELECT\n      i.tx_id AS transaction_id,\n      i.requested_mcc AS requested_mcc,\n      COALESCE((SELECT ut.mcc_code FROM update_tx ut LIMIT 1), i.requested_mcc) AS mcc_code,\n      COALESCE((SELECT ut.category FROM update_tx ut LIMIT 1), (SELECT mm.mcc_description FROM mcc_meta mm LIMIT 1)) AS category,\n      CASE\n        WHEN i.requested_mcc IS NULL THEN 'invalid_mcc'\n        WHEN EXISTS (SELECT 1 FROM update_tx) THEN 'updated'\n        WHEN EXISTS (SELECT 1 FROM target_tx) THEN 'no_cache_updated'\n        ELSE 'transaction_not_found'\n      END AS status\n    FROM input i\n)\nSELECT\n  s.status,\n  s.transaction_id,\n  s.requested_mcc,\n  s.mcc_code,\n  s.category,\n  a.bank,\n  a.identifier,\n  a.card_alias,\n  t.merchant,\n  t.currency,\n  t.amount,\n  t.transaction_timestamp\nFROM status_calc s\nLEFT JOIN transactions t ON t.id = s.transaction_id\nLEFT JOIN accounts a ON a.id = t.account_id;",
        "options": {}
      },
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "position": [
        352,
        352
      ],
      "id": "458a5cc9-0192-4f89-a94c-9e89d8f3e494",
      "name": "UpdateMCCInDB",
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "a4b65405-e7fd-4922-ab55-70f95ee2e0f1",
              "leftValue": "={{ $json.status }}",
              "rightValue": "updated",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        480,
        352
      ],
      "id": "0d866b19-d9c0-4326-b991-b190a5fb0a3b",
      "name": "IfUpdateSuccess"
    },
    {
      "parameters": {
        "chatId": "={{ $('TelegramToken').item.json.group_id }}",
        "text": "=Spend Card: {{ ($json.card_alias && $json.card_alias.trim() !== '') ? $json.card_alias : ($json.bank + ' ' + $json.identifier) }}\nSpend Amount: {{ $json.currency }} {{ $json.amount }}\nMerchant: {{ $json.merchant.replace(/[*_`[~]/g, '\\\\$&') }}\nMCC: {{ $json.mcc_code }}\nCategory: {{ $json.category }}\nTime: {{ DateTime.fromISO($json.transaction_timestamp).setZone('Asia/Singapore').toFormat('yyyy-MM-dd HH:mm:ss') }}\nTransaction ID: {{ $json.transaction_id }}",
        "replyMarkup": "inlineKeyboard",
        "inlineKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "UpdateMCC",
                    "additionalFields": {
                      "switch_inline_query_current_chat": "=/updatemcc {{ $json.transaction_id }} "
                    }
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        704,
        256
      ],
      "id": "75877c22-06d0-4b27-8130-c56a5f367bff",
      "name": "Send Updated Transaction Details",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('TelegramToken').item.json.group_id }}",
        "text": "={{ $json.status === 'transaction_not_found'\n  ? `\u26a0\ufe0f Transaction ${$json.transaction_id} not found. Usage: /updatemcc (transaction_id) (mcc_code)`\n  : ($json.status === 'invalid_mcc'\n    ? '\u26a0\ufe0f Invalid MCC code. Usage: /updatemcc (transaction_id) (4-digit_mcc_code)'\n    : `\u26a0\ufe0f MCC cache update skipped for transaction ${$json.transaction_id}.`) }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        592,
        352
      ],
      "id": "fe8c4489-58b4-438d-9f80-52b026664264",
      "name": "Send a text message2",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('TelegramToken').item.json.group_id }}",
        "text": "=`\u26a0\ufe0f Invalid command format.\nUsage: /updatemcc (transaction_id) (4-digit_mcc_code)\nExample: /updatemcc 1234 5411`",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        480,
        464
      ],
      "id": "383f5dab-660f-4754-ac99-3a48942a6008",
      "name": "SendInvalidUpdateMCCUsage",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "update",
        "dataTableId": {
          "__rl": true,
          "value": "telegram_offset",
          "mode": "name"
        },
        "filters": {
          "conditions": [
            {
              "keyValue": "1"
            }
          ]
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "offset": "={{ $('HTTP Request').item.json.result[0].update_id+1 }}"
          },
          "matchingColumns": [
            "offset"
          ],
          "schema": [
            {
              "id": "offset",
              "displayName": "offset",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "readOnly": false,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.dataTable",
      "typeVersion": 1.1,
      "position": [
        816,
        352
      ],
      "id": "741de94f-eda0-4115-ae81-dc2e367f6fa5",
      "name": "Update row(s)4"
    }
  ],
  "connections": {
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Create a data table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TelegramOffset": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Execute a SQL query1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Code in JavaScript",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update row(s)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message": {
      "main": [
        [
          {
            "node": "Update row(s)1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query1": {
      "main": [
        [
          {
            "node": "Send a text message1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message1": {
      "main": [
        [
          {
            "node": "Update row(s)3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If1": {
      "main": [
        [
          {
            "node": "Update row(s)2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create a data table": {
      "main": [
        [
          {
            "node": "TelegramToken",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If row does not exist": {
      "main": [
        [
          {
            "node": "Insert row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TelegramToken": {
      "main": [
        [
          {
            "node": "TelegramOffset",
            "type": "main",
            "index": 0
          },
          {
            "node": "If row does not exist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in JavaScript": {
      "main": [
        [
          {
            "node": "IfUpdateMCCInputValid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IfUpdateMCCInputValid": {
      "main": [
        [
          {
            "node": "UpdateMCCInDB",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "SendInvalidUpdateMCCUsage",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "UpdateMCCInDB": {
      "main": [
        [
          {
            "node": "IfUpdateSuccess",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IfUpdateSuccess": {
      "main": [
        [
          {
            "node": "Send Updated Transaction Details",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a text message2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Updated Transaction Details": {
      "main": [
        [
          {
            "node": "Update row(s)4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message2": {
      "main": [
        [
          {
            "node": "Update row(s)4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SendInvalidUpdateMCCUsage": {
      "main": [
        [
          {
            "node": "Update row(s)4",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "1ae17071-5c73-4b22-b33c-0cf3d15cd354",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "telegramquery",
  "tags": []
}