How everythingsuckz/tg-filestreambot works
Purpose and Runtime
TG-FileStreamBot is a Go service that turns Telegram-hosted files into direct HTTP streaming/download links. It combines a Telegram bot interface with an HTTP server and a Telegram user-session client used to retrieve file bytes.
Primary executable: cmd/fsb/main.go. Process startup, bot/session initialization, and service orchestration are organized in cmd/fsb/run.go and cmd/fsb/session.go.
Bootstrap and Configuration
cmd/fsb/main.go launches the application; cmd/fsb/run.go composes the major subsystems. config/config.go is the central configuration boundary, likely sourcing bot credentials, Telegram API/session settings, server configuration, and cache behavior from environment variables.
Deployment targets include containerized and process-platform environments via Dockerfile, docker-compose.yaml, and Procfile.
Telegram Interaction Layer
The Telegram-facing application is implemented in internal/bot/userbot.go, internal/bot/client.go, and internal/bot/middleware.go. This layer owns Telegram client connectivity, incoming update handling, and cross-cutting bot validation/error behavior.
Command dispatch lives in internal/commands/commands.go. User-facing workflows include startup/onboarding in internal/commands/start.go and generation of stream links from Telegram messages/files in internal/commands/stream.go.
File-Link and Response Flow
A user submits or references a Telegram file through the bot. The command layer resolves file metadata and produces a stream-link response using shared models in internal/types/file.go and internal/types/response.go.
Link generation and file-access identity depend on utility functions such as hashing in internal/utils/hashing.go. This separates externally exposed stream identifiers from Telegram’s underlying file references.
HTTP Routing and Stream Delivery
The public HTTP boundary is defined by internal/routes/routes.go, with stream-specific endpoint handling in internal/routes/stream.go. These handlers accept a generated link/request, resolve the corresponding Telegram file, and return an HTTP response suitable for browser/media-client consumption.
Actual transfer is handled by internal/stream/pipe.go; stream failures are modeled in internal/stream/errors.go. This is the key protocol boundary: HTTP request/response semantics on one side, Telegram file retrieval on the other.
Session, Caching, and Background Work
Telegram user authorization/session lifecycle is coordinated from cmd/fsb/session.go; QR-login support is packaged in pkg/qrlogin/qrcode.go and pkg/qrlogin/encoder.go. This supports authenticated Telegram file access independently of the bot command channel.
internal/cache/cache.go provides a state/cache boundary for reusable file or link-related data. internal/bot/workers.go contains asynchronous/background bot work, keeping long-running or deferred tasks outside the immediate update handler path.
External Dependencies and Operational Boundaries
The system integrates with Telegram through bot and user-client APIs, including the credited gotgproto ecosystem. Telegram is the authoritative file source; the service exposes the derived HTTP streaming surface. Runtime observability/logging utilities are centralized in internal/utils/logger.go, while releases are built and packaged through .goreleaser.yaml and GitHub release automation.
Main entry points: run.go Service orchestration · userbot.go Bot client and updates · stream.go Stream-link workflow · session.go User session lifecycle
Components 18
Runtime
- Application entrypoint
cmd/fsb/main.go - Service orchestration · Composes bot, session, and HTTP services.
cmd/fsb/run.go - Configuration · Supplies credentials, server, session, and cache settings.
config/config.go - User session lifecycle
cmd/fsb/session.go - QR login
pkg/qrlogin/qrcode.go
Telegram interaction
- Bot client and updates · Owns connectivity, update handling, and middleware.
internal/bot/userbot.go - Command dispatch
internal/commands/commands.go - Stream-link workflow · Resolves Telegram files and returns stream links.
internal/commands/stream.go - Background workers
internal/bot/workers.go
HTTP delivery
- HTTP routing
internal/routes/routes.go - Stream endpoint · Resolves generated stream requests.
internal/routes/stream.go - HTTP-to-Telegram pipe · Bridges HTTP response semantics to Telegram file retrieval.
internal/stream/pipe.go
State and support
- File/link cache
internal/cache/cache.go - File and response models
internal/types/file.go - Stream identity hashing · Separates public identifiers from Telegram file references.
internal/utils/hashing.go - Logging
internal/utils/logger.go
Other components
- Telegram APIs · Bot updates and authoritative file source.
- Browsers and media clients
Connections 21
- Application entrypoint → Service orchestration: starts
- Service orchestration → Configuration: loads
- Service orchestration → User session lifecycle: initializes
- User session lifecycle → QR login: uses when authorizing
- User session lifecycle → Telegram APIs: authenticates user client
- Service orchestration → Bot client and updates: starts
- Bot client and updates → Telegram APIs: receives updates
- Bot client and updates → Command dispatch: dispatches
- Bot client and updates → Background workers: queues deferred work
- Command dispatch → Stream-link workflow: routes stream requests
- Stream-link workflow → File and response models: uses
- Stream-link workflow → Stream identity hashing: creates link identity
- Stream-link workflow → File/link cache: stores lookup state
- Service orchestration → HTTP routing: serves
- Browsers and media clients → HTTP routing: HTTP requests
- HTTP routing → Stream endpoint: matches stream endpoint
- Stream endpoint → File/link cache: resolves link state
- Stream endpoint → HTTP-to-Telegram pipe: streams file
- HTTP-to-Telegram pipe → Telegram APIs: retrieves file bytes
- HTTP-to-Telegram pipe → Browsers and media clients: HTTP response
- Service orchestration → Logging: configures