Widgets, Commands & Uploads
Telegram Widget Bot ships with 3 built-in Profile 1 Mini-HTML Chat Widgets, dynamic launch menus, inline deep-link cards, and a live custom .html widget validator and sandbox. All bot messages, widget labels, and validation diagnostics render in English (en-US).
1. Built-In Interactive Widgets (tgcloud/lib/widgets/definitions/)
Section titled “1. Built-In Interactive Widgets (tgcloud/lib/widgets/definitions/)”Stopwatch (timer — /timer [title], Order 10)
Section titled “Stopwatch (timer — /timer [title], Order 10)”An offline client-only stopwatch engineered for maximum responsiveness with zero server round-trips (isShared: false, allowQueries: false, width: 0, height: 550):
- Zero SQLite Overhead:
sender.jsskips SQLitewidget_instancesrow insertion entirely fortimerbecause all runtime state runs locally inside the Telegram widget WebView. - Optional Caption: Any
[title]argument passed to/timer [title]is clamped to40characters. - Dual Swipeable / Tabbed Views: Switch between Analog and Digital pages using the top tabs (
#d0/#d1) or a horizontal pointer swipe (|dx| > 35px). - Analog Page: Renders an inline SVG dial (
analog-svg.js) with dual hands (#hbprimary elapsed hand and#hwlap-split shadow hand), live digital readout, and the top4laps. - Digital Page: Displays a tabular
HH:MM:SS.dclock and a paginated lap table (8laps per page). - Haptics & Local Persistence:
Start/Stop/ResumeandLap/Resetbuttons triggerHapticFeedback.impactOccurred('light'). Per-message timer state persists inlocalStorage['tw_sw_v1'](capped at100laps), while the active tab persists insessionStorage['tw_sw_pg'].
Live Shared Poll (poll — /poll Question | Option 1 | Option 2 ..., Order 20)
Section titled “Live Shared Poll (poll — /poll Question | Option 1 | Option 2 ..., Order 20)”A compact interactive voting widget supporting 2–5 pipe-separated options:
- Defaults & Normalization: Running
/pollwith no arguments creates'Which feature should we ship next?'with options['Widgets v1', 'Mini App Sync', 'Inline Mode']. Supplying 1 option automatically pads'Option 2'; options beyond5are truncated. - Dual-Mode Persistence: When
ALLOW_SHARED_WIDGETSisfalse(default),pollforcesis_shared: falseand stores per-message counts inwidget_instances(publicDatafor vote counts andchatData.votersserver-side). WhenALLOW_SHARED_WIDGETSis enabled, it persists inshared_widgetsand broadcasts updates viaeditSharedWidgetData(cacheTime: 15s). - Commit-After-Edit & Voter Highlight: Uses Commit-After-Edit (Telegram Bot API edit succeeds before writing SQLite) and monotonic revision
rto prevent stale UI regressions, while highlighting the viewer’s selected option locally vialocalStorage['vote'].
Tap Duel (duel — /duel [target], Order 30)
Section titled “Tap Duel (duel — /duel [target], Order 30)”A turn-based 2-player tap game with target score clamped to 3–15 (default 5):
- Turn & Player State: Stored in
chatData(p1,p2,turn,target,winner,r), paired with a forwarded summary snapshot inpublicData.summary. - Actions & Validation: Handles
duel:join:1(requires2distinctuser.idvalues) andduel:hit:1(enforcesturn === user.id). - Forwarded Snapshot View: Automatically renders a read-only snapshot summary whenever
TW.isForwardis true orchatDatais absent.
2. Bot Commands, Callbacks & Inline Deep Links
Section titled “2. Bot Commands, Callbacks & Inline Deep Links”| Trigger | Scope | Behavior |
|---|---|---|
/start / /help |
Private (or allowed group) | Lists registered widget commands, /share, /help, and private-chat .html upload instructions. |
/start launch_<type> or /start <type> |
Private-guarded (ALLOW_GROUP_WIDGETS) |
Deep-links directly into launching timer, poll, or duel. |
/timer [title] |
Private-guarded | Sends the offline Stopwatch widget with an optional title (max 40 chars). |
/poll [Question | Opt 1 | Opt 2 ...] |
Private-guarded | Sends a 2–5 option Poll widget (or default poll when omitted). |
/duel [target] |
Private-guarded | Sends a Tap Duel widget with target score clamped to 3–15 (default 5). |
/share |
Private chat only | Dynamically builds an inline keyboard from catalog.js with share_new:<type> buttons. |
Inline @bot [query] |
Inline mode | Filters registered widgets by type, title, or description and returns article cards (cache_time: 1, is_personal: true) with https://t.me/<bot>?start=launch_<type> buttons. |
Callback share_new:<type> |
Inline keyboard | Launches the selected widget type from the /share menu. |
Callback wg:<data> |
Inline keyboard bridge | Fallback callback bridge routed through widget-query-router.js when webhook widget_query delivery is unavailable. |
3. Live Custom .html Widget Uploader & Sandbox
Section titled “3. Live Custom .html Widget Uploader & Sandbox”upload-widget.js and html-inspector.js let developers test live HTML widgets directly inside a private chat with the bot:
-
Access & Pre-Download Guard: Private-chat only, optionally restricted by
CUSTOM_UPLOAD_ADMIN_IDS(empty array[]allows all private-chat users). Accepts.html,.htm, ortext/htmldocuments up to65,536B (64 KB) checked before file download to protect the V8 isolate’s 3-second CPU budget, followed by strict UTF-8 decoding. -
Leading Comment Directive: Start your HTML file with an optional directive comment to configure widget options:
<!-- @tg-widget { "width": 360, "height": 420, "allowQueries": true, "caption": "My Demo", "chatData": {}, "publicData": {}, "strict": false } --> -
Caption Override Flags: Pass flags in the upload message caption to override defaults:
| Caption Flag | Accepted Values | Effect |
|---|---|---|
--strict |
Flag | Upgrades Profile 1 warnings to blocking validation failures. |
width=<n> |
0 or 200–600 |
Sets widget width in pixels (0 lets Telegram choose). |
height=<n> |
0 or 50–600 |
Sets widget height in pixels (0 lets Telegram choose). |
queries=<val> |
queries=on|off|true|false|1|0 |
Enables or disables allowQueries for the uploaded sandbox widget. |
- Minification, Profile 1 Inspection & Launch: Minifies HTML, strips optional shell tags if the payload exceeds
28,000B, enforces Profile 1 blocking rules (<= 32,768B raw,<= 4,096B zlib/deflate, DOM depth<= 1,024, tags<= 2,000, attribute length<= 8,192, zero network APIs), reports warnings, and sends the widget viaapi.sendWidget(is_shared: false). - Sandbox Query Responder (
dispatcher.js): Queries from unregistered custom widgets route automatically to the sandbox responder: payloads prefixed withchat:<json>orstate:<json>persist JSON intochatDataviaeditMessageWidgetData, and every query returns{ ok: true, echo, ts }.
Next Steps
Section titled “Next Steps”- Widget Engine & Authoring — Inspect engine components, Profile 1 limits, and how to register new built-in widgets.
- Configuration, Schema & API — Review
WIDGET_CONFIGflags, handlers, SQLite tables, and Mini App endpoints. - Setup & Deployment — Deploy updates via GitHub Actions CI or
@tgcloud/cli.