# derain/tt-bot architecture

> Architecture diagram of the GitHub repository derain/tt-bot, 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/derain/tt-bot
- Repository: https://github.com/derain/tt-bot

## Overview

tt-bot is a stateless Telegram interface for managing qBittorrent downloads. Whitelisted users issue commands or interact with inline views; the bot lists and searches torrents, supports category-based adds, detail/file management and pause/resume/removal, then formats responses for Telegram. A background poller detects newly completed downloads and sends notifications. The graph reflects documented capabilities, but several detailed handler branches were not sampled; those feature links rely on the README rather than inferred call chains.

Main entry points: [`internal/bot/handler.go`](https://github.com/derain/tt-bot/blob/main/internal/bot/handler.go) (Update and message handler), [`cmd/bot/main.go`](https://github.com/derain/tt-bot/blob/main/cmd/bot/main.go) (Bot entry point), [`internal/qbt/client.go`](https://github.com/derain/tt-bot/blob/main/internal/qbt/client.go) (qBittorrent client interface), [`internal/bot/callback.go`](https://github.com/derain/tt-bot/blob/main/internal/bot/callback.go) (Inline callbacks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime setup"]
  node_config["Environment config<br/>[config.go]"]
  node_entry["Bot entry point<br/>[main.go]"]
end

subgraph group_telegram["Telegram interaction"]
  node_auth["User whitelist<br/>[auth.go]"]
  node_commands["Command registration<br/>[commands.go]"]
  node_handler["Update and message handler<br/>[handler.go]"]
  node_callbacks["Inline callbacks<br/>[callback.go]"]
  node_download["Torrent upload handling<br/>[download.go]"]
  node_sender["Telegram sender adapter<br/>[sender.go]"]
  node_formatter["Messages and keyboards<br/>[format.go]"]
end

subgraph group_qbt["Torrent service"]
  node_qbt_client["qBittorrent client interface<br/>[client.go]"]
  node_qbt_http["qBittorrent HTTP client<br/>[http.go]"]
  node_qbt_types["Torrent data types<br/>[types.go]"]
end

subgraph group_notify["Completion alerts"]
  node_poller["Completion poller<br/>[poller.go]"]
  node_notifier["Telegram completion notifier<br/>[main.go]"]
end

node_user(("Telegram user"))
node_telegram_api{{"Telegram API"}}
node_qbittorrent{{"qBittorrent Web API"}}

node_user -->|"sends updates"| node_telegram_api
node_telegram_api -->|"delivers updates"| node_entry
node_config -->|"configures"| node_entry
node_entry -->|"registers commands"| node_commands
node_entry -->|"dispatches updates"| node_handler
node_handler -.->|"checks access"| node_auth
node_handler -.->|"dispatches callbacks"| node_callbacks
node_handler -.->|"handles uploads"| node_download
node_handler -.->|"formats responses"| node_formatter
node_callbacks -->|"formats views"| node_formatter
node_handler -.->|"manages torrents"| node_qbt_client
node_callbacks -->|"controls torrents"| node_qbt_client
node_formatter -->|"formats torrent data"| node_qbt_types
node_entry -->|"creates and logs in"| node_qbt_http
node_qbt_http -->|"calls Web API"| node_qbittorrent
node_qbt_http -.->|"implements"| node_qbt_client
node_qbt_client -->|"uses data types"| node_qbt_types
node_entry -->|"starts poller"| node_poller
node_poller -->|"lists torrents"| node_qbt_client
node_poller -.->|"notifies completion"| node_notifier
node_notifier -->|"sends message"| node_telegram_api

click node_config "https://github.com/derain/tt-bot/blob/main/internal/config/config.go"
click node_entry "https://github.com/derain/tt-bot/blob/main/cmd/bot/main.go"
click node_auth "https://github.com/derain/tt-bot/blob/main/internal/bot/auth.go"
click node_commands "https://github.com/derain/tt-bot/blob/main/internal/bot/commands.go"
click node_handler "https://github.com/derain/tt-bot/blob/main/internal/bot/handler.go"
click node_callbacks "https://github.com/derain/tt-bot/blob/main/internal/bot/callback.go"
click node_download "https://github.com/derain/tt-bot/blob/main/internal/bot/download.go"
click node_sender "https://github.com/derain/tt-bot/blob/main/internal/bot/sender.go"
click node_formatter "https://github.com/derain/tt-bot/blob/main/internal/formatter/format.go"
click node_qbt_client "https://github.com/derain/tt-bot/blob/main/internal/qbt/client.go"
click node_qbt_http "https://github.com/derain/tt-bot/blob/main/internal/qbt/http.go"
click node_qbt_types "https://github.com/derain/tt-bot/blob/main/internal/qbt/types.go"
click node_poller "https://github.com/derain/tt-bot/blob/main/internal/poller/poller.go"
click node_notifier "https://github.com/derain/tt-bot/blob/main/cmd/bot/main.go"

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_config,node_entry,node_user toneBlue
class node_auth,node_commands,node_handler,node_callbacks,node_download,node_sender,node_formatter toneAmber
class node_qbt_client,node_qbt_http,node_qbt_types,node_telegram_api,node_qbittorrent toneMint
class node_poller,node_notifier toneRose
```

## Components

### Runtime setup

- [Environment config](https://github.com/derain/tt-bot/blob/main/internal/config/config.go): `internal/config/config.go`
- [Bot entry point](https://github.com/derain/tt-bot/blob/main/cmd/bot/main.go): `cmd/bot/main.go`

### Telegram interaction

- [User whitelist](https://github.com/derain/tt-bot/blob/main/internal/bot/auth.go): `internal/bot/auth.go`
- [Command registration](https://github.com/derain/tt-bot/blob/main/internal/bot/commands.go): `internal/bot/commands.go`
- [Update and message handler](https://github.com/derain/tt-bot/blob/main/internal/bot/handler.go): `internal/bot/handler.go`
- [Inline callbacks](https://github.com/derain/tt-bot/blob/main/internal/bot/callback.go): `internal/bot/callback.go`
- [Torrent upload handling](https://github.com/derain/tt-bot/blob/main/internal/bot/download.go): `internal/bot/download.go`
- [Telegram sender adapter](https://github.com/derain/tt-bot/blob/main/internal/bot/sender.go): `internal/bot/sender.go`
- [Messages and keyboards](https://github.com/derain/tt-bot/blob/main/internal/formatter/format.go): `internal/formatter/format.go`

### Torrent service

- [qBittorrent client interface](https://github.com/derain/tt-bot/blob/main/internal/qbt/client.go): `internal/qbt/client.go`
- [qBittorrent HTTP client](https://github.com/derain/tt-bot/blob/main/internal/qbt/http.go): `internal/qbt/http.go`
- [Torrent data types](https://github.com/derain/tt-bot/blob/main/internal/qbt/types.go): `internal/qbt/types.go`

### Completion alerts

- [Completion poller](https://github.com/derain/tt-bot/blob/main/internal/poller/poller.go): `internal/poller/poller.go`
- [Telegram completion notifier](https://github.com/derain/tt-bot/blob/main/cmd/bot/main.go): `cmd/bot/main.go`

### Other components

- **Telegram user**
- **Telegram API**
- **qBittorrent Web API**

## Connections

- Telegram user → Telegram API: sends updates (evidence: `README.md`)
- Telegram API → Bot entry point: delivers updates (evidence: `cmd/bot/main.go`)
- Environment config → Bot entry point: configures (evidence: `cmd/bot/main.go`)
- Bot entry point → Command registration: registers commands (evidence: `cmd/bot/main.go`)
- Bot entry point → Update and message handler: dispatches updates (evidence: `cmd/bot/main.go`)
- Update and message handler → User whitelist: checks access
- Update and message handler → Inline callbacks: dispatches callbacks
- Update and message handler → Torrent upload handling: handles uploads
- Update and message handler → Messages and keyboards: formats responses
- Inline callbacks → Messages and keyboards: formats views (evidence: `internal/bot/callback.go`)
- Update and message handler → qBittorrent client interface: manages torrents
- Inline callbacks → qBittorrent client interface: controls torrents (evidence: `internal/bot/callback.go`)
- Messages and keyboards → Torrent data types: formats torrent data (evidence: `internal/formatter/format.go`)
- Bot entry point → qBittorrent HTTP client: creates and logs in (evidence: `cmd/bot/main.go`)
- qBittorrent HTTP client → qBittorrent Web API: calls Web API (evidence: `internal/qbt/http.go`)
- qBittorrent HTTP client → qBittorrent client interface: implements
- qBittorrent client interface → Torrent data types: uses data types (evidence: `internal/qbt/client.go`)
- Bot entry point → Completion poller: starts poller (evidence: `cmd/bot/main.go`)
- Completion poller → qBittorrent client interface: lists torrents (evidence: `internal/poller/poller.go`)
- Completion poller → Telegram completion notifier: notifies completion
- Telegram completion notifier → Telegram API: sends message (evidence: `cmd/bot/main.go`)
