Files

5.0 KiB

Telegram for Ziran

Telegram account authorization and MTProto 2.0 requests implemented in Ziran. The package uses the Ziran crypto and compression packages; native sockets, entropy and private file operations use operating-system bindings. It does not load TDLib, run a Python bridge, or depend on a Telegram C/C++ implementation.

Exports are package-qualified: telegram/tl, telegram/mtproto, telegram/client, telegram/auth, telegram/messages, telegram/members and telegram/session. Use ziran add https://git.waozi.xyz/ziranlang/telegram.git once this repository is published. For development, resolve the canonical checkout through an ignored ziran.local.toml override and commit the generated lock file.

client owns an authenticated encrypted connection and accepts TL objects as JSON. Bytes use base64; int128 and int256 use hexadecimal. Integer fields are serialized without converting through floating point. auth adds phone number, code and SRP two-step verification, private session snapshots and authorization transfer between data centers. Account applications need their own API ID and hash from Telegram's app configuration.

session provides a single-account, single-thread event interface for CLI and native hosts: Create, Send, Receive, Execute. Its high-level JSON request names preserve compatibility with existing mirror callers. Account history, bot membership, text sends, exact callback bytes, reactions, read receipts, edits, deletions and service messages are normalized in Ziran. The same connection receives updates and recovers account and channel sequence gaps. It does not start a bot-token poller.

getChatFolders reads the account's saved dialog filters. Its chatFolders reply preserves folder order, titles, emoji, explicit peer membership and type/read/mute/archive rules, with an All chats entry. Access hashes stay inside the account session. telegram/folders also exposes pure Normalize, Matches and pinned-peer Position helpers; dialog announcements include contact and unread-marker/mention/reaction metadata for cached filtering.

Before setTdlibParameters, create an owner-controlled directory with mode 0700 and provide its path as database_directory. That historical request name initializes this package, without TDLib. The session pins the directory and stores account.json with mode 0600 using fsync and an atomic rename. Existing unsafe or corrupt account files are rejected and preserved. Codes and passwords are not included in account snapshots. Drain and apply received events before calling Receive again: update cursors are checkpointed after the queue is drained so interrupted processing can replay safely.

The high-level session covers the operations used by account chat mirrors; it is not a complete Telegram application. loadChats announces every chat with its full name, newest message (normalized, in last_message), mute and pin state, group size and profile photo, in Telegram's own order. downloadMessageFile saves a message's photo or document to a new owner-only file, connecting to the file's data center when it differs from the account's; max_edge picks the largest photo size that fits it; downloadChatPhoto does the same for a chat's small profile photo. sendMessage sends text, or uploads a local photo or document (inputMessagePhoto / inputMessageDocument with a path), optionally as a reply. forwardMessages forwards messages between chats and searchChats finds people, bots, groups and channels by name, @username or +phone. getChat reports a group's member count, description and the basic group it was upgraded from. searchChatMembers lists a group's recent members (up to 200) with their roles, custom titles and users; getUser gives one user's presence, bot message access, active username and profile photo; getChatMessageCount counts a chat's photos, videos, files, audio, links, voice messages or GIFs with TDLib's search filters. Normalized messages carry reply_to_message_id and forward_info. Other API methods can be called through client and the bundled TL schema. Secret chats, account registration and interactive email enrollment do not have high-level session helpers.

Run make check test for offline TL, SRP, normalization and update-sequence checks. make rpc-test is an explicit network test: it performs an encrypted help.getConfig request against Telegram's test DC without signing into an account or sending a message.

Protocol references: authorization, update sequences, SRP, and MTProto 2.0.

Forum dialogs expose is_forum; getForumTopics returns normalized titles, pins and unread counts. Topic history, read receipts and sends preserve message_thread_id, including reply targets. These paths are covered by offline native normalization and TL serialization tests.