# ja-pa/tbot architecture

> Architecture diagram of the GitHub repository ja-pa/tbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ja-pa/tbot
- Repository: https://github.com/ja-pa/tbot

## Overview

tbot is a Telegram bot for running configured scripts and delivering device notifications. Its main workflow loads configuration and commands, polls Telegram, authorizes each user (including optional TOTP checks), dispatches a command, runs its script, and sends the result back. Separate notification paths accept local Unix-socket messages or optional HTTP messages and deliver them through the daemon. The repository also includes an OpenWrt administration backend and QR-code support for 2FA setup. Some subsystem links are based on README documentation because their implementation was not excerpted.

Main entry points: [`src/main.cpp`](https://github.com/ja-pa/tbot/blob/main/src/main.cpp) (CLI entry), [`src/application.cpp`](https://github.com/ja-pa/tbot/blob/main/src/application.cpp) (Bot application), [`src/notification_service.cpp`](https://github.com/ja-pa/tbot/blob/main/src/notification_service.cpp) (Notification service), [`src/commands.cpp`](https://github.com/ja-pa/tbot/blob/main/src/commands.cpp) (Command registry)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_bot["Bot runtime"]
  node_entry["CLI entry<br/>[main.cpp]"]
  node_config["Configuration<br/>[config.cpp]"]
  node_app["Bot application<br/>[application.cpp]"]
  node_commands["Command registry<br/>[commands.cpp]"]
  node_runner["Script runner<br/>[script_runner.cpp]"]
  node_tgclient["Telegram client"]
end

subgraph group_security["Access and setup"]
  node_twofa["TOTP access<br/>[two_factor.cpp]"]
  node_qrcode["QR rendering<br/>[qr_code.cpp]"]
end

subgraph group_notify["Notifications"]
  node_notifyclient["Local send client<br/>[tbot_send.cpp]"]
  node_protocol["Socket protocol"]
  node_http["HTTP notification"]
  node_service["Notification service"]
  node_queue["Message queue"]
end

subgraph group_admin["OpenWrt administration"]
  node_admincli["Admin CLI<br/>[admin_main.cpp]"]
  node_admin["Admin backend<br/>[admin.cpp]"]
end

node_operator(("Telegram user"))
node_telegram(("Telegram API"))

node_operator -->|"sends commands"| node_telegram
node_telegram -->|"delivers updates"| node_tgclient
node_entry -->|"loads settings"| node_config
node_entry -->|"registers commands"| node_commands
node_entry -->|"starts runtime"| node_app
node_app -->|"polls and replies"| node_tgclient
node_app -->|"dispatches commands"| node_commands
node_commands -->|"runs scripts"| node_runner
node_app -->|"checks sessions"| node_twofa
node_entry -->|"generates secrets"| node_twofa
node_entry -->|"renders setup QR"| node_qrcode
node_notifyclient -->|"uses framing"| node_protocol
node_notifyclient -->|"sends over socket"| node_service
node_app -->|"starts endpoint"| node_http
node_http -->|"enqueues messages"| node_service
node_service -->|"queues messages"| node_queue
node_service -.->|"delivers notifications"| node_tgclient
node_admincli -->|"invokes backend"| node_admin

click node_entry "https://github.com/ja-pa/tbot/blob/main/src/main.cpp"
click node_config "https://github.com/ja-pa/tbot/blob/main/src/config.cpp"
click node_app "https://github.com/ja-pa/tbot/blob/main/src/application.cpp"
click node_commands "https://github.com/ja-pa/tbot/blob/main/src/commands.cpp"
click node_runner "https://github.com/ja-pa/tbot/blob/main/src/script_runner.cpp"
click node_tgclient "https://github.com/ja-pa/tbot/blob/main/src/telegram_client.cpp"
click node_twofa "https://github.com/ja-pa/tbot/blob/main/src/two_factor.cpp"
click node_qrcode "https://github.com/ja-pa/tbot/blob/main/src/qr_code.cpp"
click node_notifyclient "https://github.com/ja-pa/tbot/blob/main/src/tbot_send.cpp"
click node_protocol "https://github.com/ja-pa/tbot/blob/main/include/tbot/notification_protocol.hpp"
click node_http "https://github.com/ja-pa/tbot/blob/main/src/notification_http.cpp"
click node_service "https://github.com/ja-pa/tbot/blob/main/src/notification_service.cpp"
click node_queue "https://github.com/ja-pa/tbot/blob/main/src/notification_service.cpp"
click node_admincli "https://github.com/ja-pa/tbot/blob/main/src/openwrt/admin_main.cpp"
click node_admin "https://github.com/ja-pa/tbot/blob/main/src/openwrt/admin.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_config,node_app,node_commands,node_runner,node_tgclient,node_operator toneBlue
class node_twofa,node_qrcode toneAmber
class node_notifyclient,node_protocol,node_http,node_service,node_queue,node_telegram toneMint
class node_admincli,node_admin toneRose
```

## Components

### Bot runtime

- [CLI entry](https://github.com/ja-pa/tbot/blob/main/src/main.cpp): `src/main.cpp`
- [Configuration](https://github.com/ja-pa/tbot/blob/main/src/config.cpp): `src/config.cpp`
- [Bot application](https://github.com/ja-pa/tbot/blob/main/src/application.cpp): `src/application.cpp`
- [Command registry](https://github.com/ja-pa/tbot/blob/main/src/commands.cpp): `src/commands.cpp`
- [Script runner](https://github.com/ja-pa/tbot/blob/main/src/script_runner.cpp): `src/script_runner.cpp`
- [Telegram client](https://github.com/ja-pa/tbot/blob/main/src/telegram_client.cpp): `src/telegram_client.cpp`

### Access and setup

- [TOTP access](https://github.com/ja-pa/tbot/blob/main/src/two_factor.cpp): `src/two_factor.cpp`
- [QR rendering](https://github.com/ja-pa/tbot/blob/main/src/qr_code.cpp): `src/qr_code.cpp`

### Notifications

- [Local send client](https://github.com/ja-pa/tbot/blob/main/src/tbot_send.cpp): `src/tbot_send.cpp`
- [Socket protocol](https://github.com/ja-pa/tbot/blob/main/include/tbot/notification_protocol.hpp): `include/tbot/notification_protocol.hpp`
- [HTTP notification](https://github.com/ja-pa/tbot/blob/main/src/notification_http.cpp): `src/notification_http.cpp`
- [Notification service](https://github.com/ja-pa/tbot/blob/main/src/notification_service.cpp): `src/notification_service.cpp`
- [Message queue](https://github.com/ja-pa/tbot/blob/main/src/notification_service.cpp): `src/notification_service.cpp`

### OpenWrt administration

- [Admin CLI](https://github.com/ja-pa/tbot/blob/main/src/openwrt/admin_main.cpp): `src/openwrt/admin_main.cpp`
- [Admin backend](https://github.com/ja-pa/tbot/blob/main/src/openwrt/admin.cpp): `src/openwrt/admin.cpp`

### Other components

- **Telegram user**
- **Telegram API**

## Connections

- Telegram user → Telegram API: sends commands (evidence: `README.md`)
- Telegram API → Telegram client: delivers updates (evidence: `README.md`)
- CLI entry → Configuration: loads settings (evidence: `src/main.cpp`)
- CLI entry → Command registry: registers commands (evidence: `src/main.cpp`)
- CLI entry → Bot application: starts runtime (evidence: `src/main.cpp`)
- Bot application → Telegram client: polls and replies (evidence: `src/application.cpp`)
- Bot application → Command registry: dispatches commands (evidence: `src/application.cpp`)
- Command registry → Script runner: runs scripts (evidence: `src/commands.cpp`)
- Bot application → TOTP access: checks sessions (evidence: `src/application.cpp`)
- CLI entry → TOTP access: generates secrets (evidence: `src/main.cpp`)
- CLI entry → QR rendering: renders setup QR (evidence: `src/main.cpp`)
- Local send client → Socket protocol: uses framing (evidence: `src/tbot_send.cpp`)
- Local send client → Notification service: sends over socket (evidence: `README.md`)
- Bot application → HTTP notification: starts endpoint (evidence: `src/application.cpp`)
- HTTP notification → Notification service: enqueues messages
- Notification service → Message queue: queues messages (evidence: `src/notification_service.cpp`)
- Notification service → Telegram client: delivers notifications
- Admin CLI → Admin backend: invokes backend (evidence: `src/openwrt/admin_main.cpp`)
