Telegram Business Assistant
Telegram Business Assistant (AZiPA/telegram-chat-automation) is a personal, single-owner automation assistant (also known as a Telegram Secretary) for Telegram Business accounts. Built for Telegram Serverless (@tgcloud/cli ^0.2.0), it runs inside V8 isolates with a built-in SQLite database (sdk/db Drizzle-style DSL), zero runtime node_modules, and no VPS, containers, or external hosting required.
High-Level Update Flow
Section titled “High-Level Update Flow”flowchart LR Biz["Telegram Business Chat"] --> Isolate["Telegram Serverless (V8 Isolate)"] Isolate <--> SQLite["Built-in SQLite (sdk/db)"] Isolate --> Owner["Owner Private Chat / Per-Contact Topics"]
Core Capabilities
Section titled “Core Capabilities”1. Deleted Message Recovery & Batch Digests
Section titled “1. Deleted Message Recovery & Batch Digests”Handled via deleted_business_messages:
- Atomic Deduplication & Neutral Headers: Atomically claims incoming deleted messages to prevent duplicate alerts on retried webhooks, using neutral headers (
🗑 Message Deleted/🗑 Messages Deleted) without guessing which participant deleted the message. - Rich Fidelity: Preserves original formatting (
entities/caption_entities), structured content summaries (contact,location,venue,poll,dice, etc.), andforward_originmetadata. - Smart Batch Grouping: Groups simultaneous multi-message deletions under one batch header—text and structured items merge into 4,000-character-safe digests, while media albums (
media_group_id) reconstruct in batches of 2–10 items viasendMediaGroup. - Safe Bounds & Zero Self-Noise: Processes up to
25archived incoming messages per update (MAX_DELETE_BATCH = 25) with an overflow notice, and summarizes pre-connection or unarchived deletions (NOTIFY_UNARCHIVED_DELETES = true). Deleting your own outgoing messages (is_incoming = false) never generates alert noise.
2. Silent Edit Tracking
Section titled “2. Silent Edit Tracking”Handled via edited_business_message:
- Concurrency-Safe Snapshots: Stores edit snapshots in
messages.edit_historywith optimisticedit_dateconcurrency checks and delivers reports silently (disable_notification: true). - Noise Filtering: Filters out false-positive Telegram updates—such as automatic link-preview generation where text, entities, media, and metadata did not change—via
isMeaningfulEdit. - Compact 1-Message Mode: Renders an expandable
<blockquote expandable>diff for plain, unformatted text edits up to3,000combined characters. - 3-Message Entity-Preserving Mode: Sends
🔴 Before:and🟢 After:messages with shifted UTF-16 entity offsets—plus before/after media copies whenfileUniqueIdchanges—for formatted text, long messages, captions, and media replacements.
3. View-Once Media Capture & Hybrid Smart Trigger
Section titled “3. View-Once Media Capture & Hybrid Smart Trigger”- Capture Before Opening: Saves incoming View-Once
photo,video,voice, andvideo_notemedia when you reply before opening the message—even when Telegram initially delivers aview_once_placeholderwithout afile_id. - Resilient Delivery Fallback: If sending directly by
file_idfails, the bot downloads the file (up to the20 MBBot API limit,MAX_GETFILE_BYTES = 20971520) and re-uploads viaInputFile. If delivery still fails, it rolls back the DB capture claim so you can retry. - Hybrid Smart Trigger: Any reply captures uncaptured View-Once media. Replies matching your configured trigger text (default
., case-insensitive) are also automatically deleted viadeleteBusinessMessageswhen/autodeleteisonandcan_delete_all_messagesis granted.
4. Per-Contact Private Chat Topics (Threaded Mode)
Section titled “4. Per-Contact Private Chat Topics (Threaded Mode)”- Isolated Contact Threads: With Threaded Mode enabled in
@BotFather, the bot creates one Forum Topic per contact (👤 Name (@username)or👤 Name [id]) using atomic DB lease locking (contact_topics, status'creating'/'ready') to prevent duplicate topics. It also syncs contact renames and self-heals closed or deleted topics. - Automatic Fallback & Cooldown: If Threaded Mode is unavailable, alerts fall back to your main private bot chat with a 10-minute cooldown (
topics_disabled_until). Running/startclears the cooldown immediately.
5. Fail-Closed Single-Owner Security & Clean Rotation
Section titled “5. Fail-Closed Single-Owner Security & Clean Rotation”- Strict Ownership Enforcement: Ownership resolves strictly from
owner_idinsettings. If missing or invalid,getOwnerId()returnsnulland all commands, business connections, and handlers refuse access. - Zero Hardcoded Secrets & Clean Handoff: Neither
TGCLOUD_TOKENnorOWNER_IDis hardcoded in source code. UpdatingOWNER_IDtriggerssetOwnerId(), cleaning up stalecontact_topics,connections, andtopics_disabled_untilfor the previous owner while keeping archivedmessagesintact. - Platform-Level Access Restriction: Enabling Restrict bot usage (with the
OWNER_IDaccount in Allowed users) in the@BotFatherMini App adds Telegram platform-level protection on top of the bot’s fail-closedOWNER_IDcheck—see Setup & Deployment.
6. Opportunistic Retention & Opt-In Debug Logging
Section titled “6. Opportunistic Retention & Opt-In Debug Logging”- Automatic Pruning: Prunes
messagesolder than30days (RETENTION_DAYS) andraw_updatesolder than3days (RAW_UPDATES_RETENTION_DAYS) opportunistically on ~2% ofbusiness_messageupdates. - Privacy-First Logging:
DEBUG_RAW_UPDATESisfalseby default so raw payloads are not stored unless explicitly enabled.
Next Steps
Section titled “Next Steps”- Setup & Deployment — Configure
@BotFather, deploy via GitHub Actions CI or@tgcloud/cli, bootstrapOWNER_ID, and activate Telegram Business rights. - Commands & Configuration — Review private owner commands and
tgcloud/lib/config.jsconstants. - Architecture & Schema — Inspect V8 isolate runtime rules, the 5 handlers, 5 SQLite tables, and maintenance workflows.