How nuclight/teleperl works
Purpose and Entry Points
Teleperl is a pure-Perl client library for Telegram’s MTProto protocol and Telegram API, with example interactive, bot, transfer, CLI, and minimal GUI applications. Main runnable entry points include cli.pl, auth.pl, bot.pl, download.pl, upload.pl, and tkx.pl. Authentication creates a session used by applications.
Protocol Transport and MTProto Core
MTProto.pm is the low-level protocol layer: it owns Telegram transport/session behavior and depends on generated MTProto TL objects. Network integration is asynchronous through AnyEvent and SOCKS support, represented by AnyEvTkx.pm and AnyEventSocks.pm. This is the boundary between application code and Telegram data centers/proxies.
Telegram API Client Layer
Telegram.pm builds Telegram API operations above MTProto and generated schema constructors. Application-facing scripts and higher-level state managers should invoke Telegram API objects rather than construct transport messages directly. The selected generated schema layer materially determines compatibility with Telegram servers.
TL Schema, Parsing, and Code Generation
Telegram’s type language is parsed and compiled into Perl packages. tl.yp defines the Yapp parser; tl-gen.pl generates MTProto.pm-adjacent protocol types and Telegram.pm API types from schemas. Input schemas live in res/mtproto.tl and versioned API files such as res/layer82-tdesktop.tl. TL/Object.pm provides the common TL object representation/runtime. Generated files are a deliberate compatibility boundary: changing layers may require client logic changes.
Authentication and Cryptography
Authorization and secret-handling infrastructure is concentrated in TeleCrypt/Keys.pm and TeleCrypt/SRP.pm, supporting Telegram key exchange and SRP-related flows. auth.pl is the operational login entry point, obtaining an SMS/session code and persisting a reusable session. Cryptographic primitives come from OpenSSL Perl bindings and random/bignum dependencies.
Client State, Persistence, and Updates
The Teleperl namespace contains stateful client support around the raw API: Teleperl/Storage.pm persists local state/session-related data, Teleperl/PeerCache.pm resolves and caches Telegram peers, and Teleperl/UpdateManager.pm receives/processes update streams. These components isolate long-lived local client state and asynchronous server updates from one-shot API calls.
CLI Application Framework
cli.pl hosts an interactive command application built on CLI/Framework.pm. Command dispatch and command implementations are separated under CLI/Framework/Command.pm and CLI/Framework/Command/, including authentication-facing, inspection, configuration, and console commands. This framework is an application shell over the Teleperl/Telegram layers, not the protocol implementation itself.
Optional Interfaces and Utilities
tkx.pl/ClifTg.pm provide the minimal Tcl/Tk proof-of-work GUI path, while scripts such as cborlog.pl, cborpack.pl, and cborsee.pl support optional CBOR-based logging/inspection workflows. Runtime configuration is expected externally, based on teleperl.example.conf, including API credentials, phone number, and optional proxy settings.
Main entry points: Telegram.pm Telegram API client · tl-gen.pl TL generator · Storage.pm Local storage · auth.pl Authentication
Components 19
TL schema & generation
Schema compilation defines Telegram protocol compatibility.
- TL schemas · MTProto and versioned Telegram API layer definitions.
res/mtproto.tl - TL parser
tl.yp - TL generator
tl-gen.pl - TL object runtime
TL/Object.pm
Protocol & API core
- MTProto transport · Owns Telegram transport and session behavior.
MTProto.pm - Async & SOCKS integration
AnyEvTkx.pm - Telegram API client · Application-facing Telegram operations built above MTProto.
Telegram.pm - Key exchange
TeleCrypt/Keys.pm - SRP flows
TeleCrypt/SRP.pm
Client state
- Local storage · Persists session-related local state.
Teleperl/Storage.pm - Peer cache · Resolves and caches Telegram peers.
Teleperl/PeerCache.pm - Update manager · Processes asynchronous server update streams.
Teleperl/UpdateManager.pm
Applications
- Authentication · Obtains login codes and persists a reusable session.
auth.pl - Interactive CLI
cli.pl - CLI framework · Dispatches interactive commands and implementations.
CLI/Framework.pm - Bot & transfer scripts
bot.pl - Minimal Tcl/Tk GUI
tkx.pl - CBOR tools
cborlog.pl
Other components
- Telegram data centers
Connections 23
- TL schemas → TL generator: input
- TL parser → TL generator: parses
- TL generator → TL object runtime: generates types for
- TL generator → MTProto transport: generates protocol types
- TL generator → Telegram API client: generates API types
- Telegram API client → MTProto transport: API calls
- MTProto transport → Async & SOCKS integration: async transport
- Async & SOCKS integration → Telegram data centers: network / proxy
- MTProto transport → Telegram data centers: MTProto session
- Authentication → Telegram API client: login flow
- Authentication → Key exchange: key exchange
- Authentication → SRP flows: SRP
- Authentication → Local storage: persists session
- Interactive CLI → CLI framework: hosts
- CLI framework → Telegram API client: commands invoke
- CLI framework → Local storage: uses state
- Bot & transfer scripts → Telegram API client: API operations
- Minimal Tcl/Tk GUI → Telegram API client: API operations
- Telegram API client → Peer cache: peer resolution
- Telegram API client → Update manager: update stream
- Peer cache → Local storage: persists cache
- Update manager → Local storage: persists state
- CBOR tools → Local storage: logging / inspection