| 5 |
| and **32 KB uncompressed**. It travels as message media of its own since API |
| 6 | layer 233 (shipped together with widget-sandbox v0.1.0): the payload is in the |
| 7 | media, also when the widget is a shared object ("The widget's state and |
| 8 | requests", below). The server never runs, parses or renders the HTML: it checks |
| 9 | the bytes and packs them. A widget is fixed code plus changeable state: the HTML |
| 10 | never changes after sending, and with it travel the widget's state, which only |
| 11 | the bot writes, and the flags that say where the widget may ask the bot ("The |
| 12 | widget's state and requests", below), all in layer 233 from the launch. |
| 13 |
| 14 | The layer's widget TL, as the server's final spec of 2026-10-26 has it |
| 15 | (verbatim; the sections below refer to it): |
| 16 |
| 17 | ```tl |
| 18 | inputMediaWidget flags:# allow_queries:flags.0?true fwd_allow_queries:flags.1?true shared_widget:flags.2?true width:int height:int html:string public_data:flags.3?bytes chat_data:flags.4?bytes = InputMedia; |
| 19 | inputMediaWidgetId flags:# widget_id:long chat_data:flags.0?bytes = InputMedia; |
| 20 | messageMediaWidget flags:# is_copy:flags.0?true can_query:flags.1?true background_query:flags.2?true width:int height:int payload:bytes public_data:flags.3?bytes chat_data:flags.4?bytes widget_id:flags.5?long = MessageMedia; |
| 21 |
| 22 | inputBotInlineMessageWidget flags:# allow_queries:flags.0?true fwd_allow_queries:flags.1?true shared_widget:flags.2?true width:int height:int html:string public_data:flags.3?bytes chat_data:flags.4?bytes reply_markup:flags.5?ReplyMarkup = InputBotInlineMessage; |
| 23 | inputBotInlineMessageWidgetId flags:# widget_id:long chat_data:flags.0?bytes reply_markup:flags.1?ReplyMarkup = InputBotInlineMessage; |
| 24 | botInlineMessageWidget flags:# width:int height:int payload:bytes public_data:flags.0?bytes chat_data:flags.1?bytes widget_id:flags.2?long reply_markup:flags.3?ReplyMarkup = BotInlineMessage; |
| 25 |
| 26 | updateBotWidgetQuery flags:# query_id:long user_id:long peer:Peer msg_id:int chat_instance:long chat_type:string data:bytes widget_id:flags.0?long = Update; |
| 27 |
| 28 | updateInlineBotWidgetQuery flags:# query_id:long user_id:long inline_message_id:InputBotInlineMessageID chat_instance:long chat_type:string data:bytes widget_id:flags.0?long = Update; |
| 29 |
| 30 | updateBotWidgetCopyQuery flags:# query_id:long user_id:long data:bytes widget_id:long = Update; |
| 31 |
| 32 | updateMessageWidgetData flags:# peer:Peer msg_id:int public_data:flags.0?bytes chat_data:flags.1?bytes pts:int pts_count:int = Update; |
| 33 |
| 34 | messages.botWidgetQueryAnswer flags:# result:flags.0?bytes = messages.BotWidgetQueryAnswer; |
| 35 |
| 36 | messages.widgetPublicDataNotModified period:int = messages.WidgetPublicData; |
| 37 | messages.widgetPublicData hash:long data:bytes period:int = messages.WidgetPublicData; |
| 38 |
| 39 | ---functions--- |
| 40 |
| 41 | messages.getBotWidgetQueryAnswer peer:InputPeer msg_id:int data:bytes = messages.BotWidgetQueryAnswer; |
| 42 | messages.setBotWidgetQueryAnswer flags:# query_id:long result:flags.0?bytes = Bool; |
| 43 |
| 44 | messages.editMessageWidgetData flags:# peer:InputPeer id:int public_data:flags.0?bytes chat_data:flags.1?bytes = Updates; |
| 45 | messages.editInlineBotMessageWidgetData flags:# id:InputBotInlineMessageID public_data:flags.0?bytes chat_data:flags.1?bytes = Bool; |
| 46 |
| 47 | messages.editWidgetData flags:# widget_id:long public_data:bytes cache_time:flags.0?int = Bool; |
| 48 |
| 49 | messages.getWidgetPublicData peer:InputPeer msg_id:int hash:long = messages.WidgetPublicData; |
| 50 | ``` |
| 51 |
| 52 | ## Sending and delivery |
| 53 |
| 54 | - A bot sends `messages.sendMedia` / `messages.uploadMedia` with |
| 55 | `inputMediaWidget`: `html` is the UTF-8 source of the widget (required); |
| 56 | `width` and `height` are the optional size anchors in CSS px, `0` meaning |
| 57 | "auto". Bots never send compressed bytes. A widget that already exists as a |
| 58 | shared object is sent by reference instead (`inputMediaWidgetId`, below). |
| 59 | - The server checks the source (valid UTF-8, no NUL bytes, at most 32 KB), |
| 60 | compresses it itself with zlib at level 9 (PHP `gzcompress($html, 9)`), |
| 61 | checks the **packed** payload (at most 4 KB) and puts the bytes into the |
| 62 | message (or into the shared object it makes). No document, no file id: a |
| 63 | widget is reused only as a shared object, by `widget_id`. |
| 64 | - The client receives `messageMediaWidget`: `payload` is a **zlib stream** |
| 65 | (RFC 1950: a 2-byte header, raw deflate, a 4-byte Adler-32), **not gzip** |
| 66 | (RFC 1952, magic `1f 8b`) and not raw deflate (RFC 1951). The client inflates |
| 67 | it (below) and hands the UTF-8 HTML to `WidgetHost` (`docs/integration.md`). |
| 68 | The client keeps **no size limits of its own**: the server enforces both, so |
| 69 | a payload the server produced always decodes to HTML within them, and the sandbox, the validator and the runtime limits |
| 70 | are the security boundary, not the size. |
| 71 | - The HTML never changes after sending, in a message or in a shared object: no |
| 72 | edit replaces it (an edit may change the caption, as for any media); a new |
| 73 | layout is a new widget, and old messages keep the old one. What changes is the |
| 74 | widget's state, `chat_data` and `public_data`, which only the bot writes ("The |
| 75 | widget's state and requests"). What the widget keeps for itself lives in the |
| 76 | message's storage on the device and is deleted with the message |
| 77 | (`docs/sdk-v1.md` §2.8). |
| 78 |
| 79 | | Error | Cause | |
| 80 | |---|---| |
| 81 | | `INPUT_MEDIA_WIDGET_TOO_LARGE` | HTML over 32 KB, or the compressed payload over 4 KB | |
| 82 | | `INPUT_MEDIA_WIDGET_HTML_INVALID` | not text HTML: broken UTF-8, binary data, compressed bytes sent by the bot | |
| 83 | | `INPUT_MEDIA_WIDGET_NOT_ALLOWED` | the sender is not a bot, the chat is not one a bot may send widgets to (below), or a draft or ephemeral message | |
| 84 |
| 85 | ### Inflating the payload |
| 86 |
| 87 | What reads RFC 1950 on each platform, and the traps: |
| 88 |
| 89 | | Platform | Inflate with | Not with | |
| 90 | |---|---|---| |
| 91 | | Web | `new Response(new Blob([payload]).stream().pipeThrough(new DecompressionStream('deflate'))).text()`: `'deflate'` is RFC 1950 | `'gzip'`, `'deflate-raw'` | |
| 92 | | Android, JVM | `java.util.zip.Inflater()` (the default, `nowrap = false`) or `InflaterInputStream` | `GZIPInputStream`, `Inflater(true)` | |
| 93 | | Apple | zlib's `uncompress` / `inflate` (libz) | `Compression`'s `COMPRESSION_ZLIB` and `NSData.decompressed(using: .zlib)`: raw deflate despite the name; with them, skip the 2-byte header and drop the 4-byte trailer | |
| 94 | | Qt (tdesktop) | zlib's `uncompress` / `inflate` | `qUncompress` alone: it expects a 4-byte big-endian length before the stream | |
| 95 | | C, C++ (zlib) | `inflateInit` / `uncompress`; `inflateInit2(&s, 15 + 32)` also accepts gzip, if a client has to read both | `inflateInit2(&s, 31)` (gzip only), `-15` (raw) | |
| 96 |
| 97 | A payload the server produced inflates to at most 32 KB of valid UTF-8, so a |
| 98 | 32 KB output buffer is enough; a stream that fails to inflate is a broken |
| 99 | message: show the unsupported-media fallback. |
| 100 |
| 101 | ## Who may send, and where it may go |
| 102 |
| 103 | - **Directly**, a bot sends widgets to its private chat with a user and to the |
| 104 | basic groups and supergroups it is a member of (it needs the right to send |
| 105 | media: the widget is checked like a photo, `CHAT_SEND_PHOTOS_FORBIDDEN`). The |
| 106 | allowed chat types are server config; channels are not among them yet. |
| 107 | - **Via the bot** (inline mode): a bot's inline result may be a widget, in the |
| 108 | same two forms as a direct send: `inputBotInlineMessageWidget` (the HTML, the |
| 109 | size anchors, the state, `allow_queries` / `fwd_allow_queries` / |
| 110 | `shared_widget`, `reply_markup`; the server checks the HTML and the state |
| 111 | once and packs them when it saves the results) or |
| 112 | `inputBotInlineMessageWidgetId` (a `widget_id`, its own `chat_data`, |
| 113 | `reply_markup`). The results a client lists are `botInlineMessageWidget`: the |
| 114 | payload, the size anchors, `reply_markup`, `public_data`, `chat_data` and |
| 115 | `widget_id` (a result by reference takes the payload, the anchors and the |
| 116 | shared state from its object and shows its id). A result is not a message, so |
| 117 | it has no `is_copy` / `can_query` / `background_query`: the client shows it as a |
| 118 | preview, the widget with its state, no input, no requests, nothing about the |
| 119 | user (`docs/sdk-v1.md` §2.16, `context.preview`). Each send |
| 120 | (`messages.sendInlineBotResult`) becomes an ordinary message with `via_bot_id` |
| 121 | and its own `messageMediaWidget` and `chat_data`; a result with |
| 122 | `shared_widget` / `fwd_allow_queries` makes a new object on every send, a |
| 123 | result by reference binds the message to its `widget_id`, and the result's |
| 124 | flags become the message's. Widget results need a layer-233 client: older ones |
| 125 | do not get them; a stale result id is `INLINE_RESULT_EXPIRED`. |
| 126 | - **Forwarding is allowed to any chat**, groups and channels included. A |
| 127 | forward is a copy (`is_copy`): it runs the same HTML (isolated, no network) |
| 128 | with no chat state (`chat_data`), its own shared state (`public_data`, below) |
| 129 | and its own storage on the device, which starts empty. A widget is never |
| 130 | forwarded without its author: the server keeps the forward header even when |
| 131 | the user asks to hide the sender, so a widget always shows which bot it is |
| 132 | from (directly or via). |
| 133 | - `copyMessage` by the bot sends a new widget by the same rules: a shared |
| 134 | widget stays bound to its `widget_id`, a static one gets the HTML and the |
| 135 | shared state (`public_data`) by value, and the chat state (`chat_data`) is |
| 136 | never copied. |
| 137 |
| 138 | ## The widget's state and requests |
| 139 |
| 140 | Part of layer 233, as the server's final spec of 2026-10-26 describes it. The |
| 141 | widget-facing side is `docs/sdk-v1.md` §2.13–§2.16, the client's |
| 142 | `docs/sdk-v1.md` §5.11 and `docs/integration.md` "The widget's state and |
| 143 | requests"; the framework does not depend on the TL names: it has its own host |
| 144 | interface. |
| 145 |
| 146 | **Fixed code, changeable state.** The widget's code, its HTML, is set when the |
| 147 | bot sends it and never changes. Its state changes, and only the bot writes it. |
| 148 | The widget reads its state and learns of changes; it cannot change it itself, |
| 149 | only ask the bot (a request, below). |
| 150 |
| 151 | **Two kinds of widget.** A **static** widget keeps its HTML and its state in |
| 152 | the message; a forward copies the HTML and the shared state, frozen. A **shared |
| 153 | widget** is a server object, like a poll: the HTML (payload), the size anchors, |
| 154 | the shared state (`public_data`), its refresh `period` and its request flags, in |
| 155 | one object that any number of messages refer to by `widget_id`. The media shows |
| 156 | `widget_id` to everyone, the bot that owns it included; like a poll, the object |
| 157 | outlives every message that refers to it. |
| 158 | One object is one behaviour and one network marker in every carrier: its |
| 159 | flags, layout and size do not differ between messages. |
| 160 |
| 161 | **Two kinds of state.** The chat state, `chat_data`, and the shared state, |
| 162 | `public_data`: each optional, **a text of up to 1024 bytes (UTF-8)** without NUL |
| 163 | bytes (the server checks it; otherwise `*_DATA_TOO_LARGE` / `*_DATA_INVALID`), |
| 164 | written only by the bot: when the widget is made and with the edit methods |
| 165 | below, never in an answer to a request. In an edit a field not passed is |
| 166 | unchanged and an empty string clears it. |
| 167 |
| 168 | - The **chat state** (`chat_data`) is tied to one original message and seen |
| 169 | only in its chat (the private chat; a group, where all its members share it; |
| 170 | or the chat of a via-bot message); it is set with each send separately and is |
| 171 | **never copied** by a forward. For example a game in progress, whose turn it |
| 172 | is. |
| 173 | - The **shared state** (`public_data`) is seen wherever the widget is shown, |
| 174 | in the original and in every forward: a static widget keeps it in the message |
| 175 | (a forward gets a frozen copy as of the forward; an edit changes only the |
| 176 | original); a shared widget has one shared state, the object's, for all its |
| 177 | messages and forwards: every read of any carrier gives its current value, and |
| 178 | an edit changes it for all of them. For example poll results, a price. |
| 179 |
| 180 | **Sending, in two forms** (`inputMediaWidget` / `inputMediaWidgetId` directly, |
| 181 | `inputBotInlineMessageWidget` / `inputBotInlineMessageWidgetId` in inline |
| 182 | results; the TL above): |
| 183 |
| 184 | - **With `html`**, the bot's choices: |
| 185 | 1. `allow_queries`: the widget may ask the bot from its original. No |
| 186 | whitelist (the permission model of inline buttons: every request names who |
| 187 | pressed). Without it the server refuses **every** request to the widget |
| 188 | (`WIDGET_QUERY_NOT_ALLOWED`) and never sets `can_query` or |
| 189 | `background_query`. |
| 190 | 2. `fwd_allow_queries`: forwards may ask too (on a tap; a copy never asks in |
| 191 | the background). It implies `allow_queries` and makes the widget a shared |
| 192 | object (its `widget_id` in the sent media); whitelisted bots only |
| 193 | (`INPUT_MEDIA_WIDGET_FWD_ALLOW_QUERIES_NOT_ALLOWED`). |
| 194 | 3. `shared_widget`: make the widget a shared object, whatever the requests (a |
| 195 | ticker without requests is `shared_widget` without `allow_queries`: |
| 196 | forwards follow its shared state but cannot ask). |
| 197 |
| 198 | With `shared_widget` or `fwd_allow_queries` the HTML, the size anchors and |
| 199 | the shared state (`public_data`) go into the new object (the anchors are |
| 200 | fixed, `public_data` is its first shared state); without them the widget is |
| 201 | static. The chat state (`chat_data`) stays the message's. |
| 202 | - **With `widget_id`**: a reference to an existing object of the same bot, with |
| 203 | only the message's own chat state (`chat_data`; and `reply_markup` in an |
| 204 | inline result). The HTML, the anchors, the shared state and the flags come |
| 205 | from the object; the constructor has no fields for them, and the send does not |
| 206 | change the object's shared state. An unknown or another bot's id is |
| 207 | `WIDGET_ID_INVALID`. |
| 208 |
| 209 | There is no method to make an object: the first send makes it. A bot that |
| 210 | needs a `widget_id` in advance (for a mailing, for inline results) sends the |
| 211 | widget with `shared_widget` or `fwd_allow_queries` to any chat and reads the id |
| 212 | from the sent media. An object's `period` starts at the server's 30 s and changes |
| 213 | only with `editWidgetData` (below). |
| 214 |
| 215 | | sent with | the case | the shared state (`public_data`) | forwards may ask the bot | edited | |
| 216 | |---|---|---|---|---| |
| 217 | | `html`, no flags | a static widget with state | in the message; forwards get a copy as of the forward | – | by the original's coordinates; only the original changes | |
| 218 | | `html` + `fwd_allow_queries` | live forwards (a poll) | an object the server makes; `widget_id` in the media | ✓, on a tap | by id or by coordinates | |
| 219 | | `html` + `shared_widget` | a ticker, one shared state for all | an object; `widget_id` in the media | –; forwards follow its shared state | by id or by coordinates | |
| 220 | | `html` + both | a live ticker | an object | ✓, on a tap | both | |
| 221 | | `widget_id` | a mailing, one object reused | the object's, unchanged by the send | as the object's flags say | by id | |
| 222 |
| 223 | **Who may request** (the server computes the flags; the client passes them on |
| 224 | and the framework derives nothing). For a widget with `allow_queries` (a static |
| 225 | widget's message, a shared widget's object; copies: also |
| 226 | `fwd_allow_queries`, which a static widget never has); without |
| 227 | `allow_queries`, `can_query` and `background_query` are never set: |
| 228 |
| 229 | | message | `is_copy` | `can_query` | `background_query` | |
| 230 | |---|:-:|:-:|:-:| |
| 231 | | the original in the private chat with the bot | – | ✓ | ✓ | |
| 232 | | an original via the bot (in any chat) | – | ✓ | – | |
| 233 | | an original in a group the bot sent it to | – | ✓ | – | |
| 234 | | a forward of a `fwd_allow_queries` widget (in any chat) | ✓ | ✓ | – | |
| 235 | | a forward of any other widget | ✓ | – | – | |
| 236 |
| 237 | The framework's policy from these flags: a request on a tap whenever |
| 238 | `can_query`; without a tap only with `background_query`: one when the widget |
| 239 | appears on screen, then at most one per 5 s while it stays visible; a copy |
| 240 | never has a chat state and is told nothing about its chat (no chat type, no |
| 241 | chat instance); the sandbox never goes to the network itself. **The network |
| 242 | marker:** where `can_query` is set, the client marks the widget as one whose |
| 243 | tap may send data to the bot's server; without it there is no marker, and the |
| 244 | server guarantees no request can go out (`docs/integration.md`, "The network |
| 245 | marker"). Widgets with the same `widget_id` on screen are one widget: a client |
| 246 | may cache the checked payload and the shared state per `widget_id`. |
| 247 |
| 248 | **A request** is a callback button inside the widget: |
| 249 | `messages.getBotWidgetQueryAnswer` (the client's call, not the Bot API's) with |
| 250 | the message in the user's own coordinates and `data`, answered with |
| 251 | `messages.botWidgetQueryAnswer`. `data` is 1–64 opaque bytes |
| 252 | (`WIDGET_QUERY_DATA_INVALID` otherwise); at most **5 requests per 5 s** per user |
| 253 | and bot, as for callback buttons (`FLOOD_WAIT`); the server waits for the bot |
| 254 | 15 s (`WIDGET_RESPONSE_TIMEOUT`); a widget without `allow_queries`, or a copy of |
| 255 | one without `fwd_allow_queries`, is `WIDGET_QUERY_NOT_ALLOWED`, an unknown |
| 256 | message `MESSAGE_ID_INVALID`. The server does not tell a tap from a background |
| 257 | request: that is the client's policy. |
| 258 |
| 259 | The bot gets one of three updates, by where the widget was tapped; the Bot |
| 260 | API has one `widget_query`, shaped like `callback_query`, with the fields |
| 261 | filled per kind: |
| 262 |
| 263 | | update (MTProto) | tapped in | the bot gets | |
| 264 | |---|---|---| |
| 265 | | `updateBotWidgetQuery` | an original (the private chat with the bot, a group it is in) | `from`, the message in the bot's coordinates with its current state, `chat_instance`, the chat type, `data`, `widget_id` when the widget is shared | |
| 266 | | `updateInlineBotWidgetQuery` | a via-bot message | `from`, `inline_message_id`, `chat_instance`, the chat type, `data`, `widget_id` when shared | |
| 267 | | `updateBotWidgetCopyQuery` | any forward | **only** `from`, `data` and `widget_id`: no message, no `chat_instance`, no chat type: the bot learns nothing about where it was forwarded | |
| 268 |
| 269 | A copy's `widget_id` is always there (a live forward exists only for a |
| 270 | `fwd_allow_queries` widget, which is shared) and the server takes it from the |
| 271 | tapped message, so it cannot be forged: the bot counts "one vote" by |
| 272 | (`widget_id`, `from`). The bot answers by the query's id |
| 273 | (`answerWidgetQuery { widget_query_id, result? }`, MTProto |
| 274 | `messages.setBotWidgetQueryAnswer`), also after the original is deleted: |
| 275 | `result` at most 1024 bytes of text (`WIDGET_RESULT_TOO_LARGE`, |
| 276 | `WIDGET_DATA_INVALID`), only for the client that asked; none is an empty |
| 277 | answer. **An answer carries no state, no toast, alert, URL or `cache_time`.** |
| 278 |
| 279 | **The bot's edits of the state**, all silent (no "edited" mark, no |
| 280 | notification): |
| 281 |
| 282 | - `editMessageWidgetData` (`messages.editMessageWidgetData`): by the |
| 283 | original's coordinates as the bot knows them, only the bot that sent it and |
| 284 | only the original; `public_data` and/or `chat_data` (a `public_data` edit of |
| 285 | a shared widget writes its object). A message's state edits go through the |
| 286 | flood control of `sendMessage`, each counted as one message. |
| 287 | - The same with `inline_message_id` for a via-bot message |
| 288 | (`messages.editInlineBotMessageWidgetData`): `chat_data` is allowed there, a |
| 289 | via-bot message is an original. |
| 290 | - `editWidgetData` (`messages.editWidgetData`): an object by its `widget_id`, |
| 291 | which works always, even when every carrier is deleted or unknown to the bot; |
| 292 | its optional `cache_time` is the only way to set the object's `period` (0 sets |
| 293 | it back to 30 s). At most one edit per second per object. |
| 294 |
| 295 | A change of the chat state (`chat_data`) reaches every holder of the message as |
| 296 | a light update without the HTML (`updateMessageWidgetData`). In private chats |
| 297 | and basic groups it comes in the owner's pts sequence, as `updateEditMessage` |
| 298 | does; in a supergroup the post goes through the usual edit of channel posts, |
| 299 | with the channel's pts (`updateEditChannelMessage` and the like). The client |
| 300 | applies it by the rules of that pts sequence. |
| 301 |
| 302 | A shared widget's shared state (`public_data`) reaches its carriers by |
| 303 | polling, **only for widgets that ask to follow it live**: the messenger (never |
| 304 | the sandbox) polls a widget on screen that called |
| 305 | `TelegramWidget.watchPublicData()` (the host reports it: `docs/integration.md`); |
| 306 | every other widget gets its shared state with the message (and a static |
| 307 | widget's edits by the update above). A bot may make a |
| 308 | widget shared only to send it by `widget_id`, and polling every such card would |
| 309 | ask the server for nothing (the proposal to the server of 2026-10-26, agreed |
| 310 | with Artem; the protocol does not change, the client's policy does). The |
| 311 | messenger calls `messages.getWidgetPublicData` for **one** message at a time, |
| 312 | with the hash of the shared state it has (0 at first), and applies a new one to |
| 313 | every widget on screen with the same `widget_id` (the shared state is the |
| 314 | object's, so one visible carrier per object is enough): |
| 315 |
| 316 | - `widgetPublicDataNotModified { period }`: the message has no object (a |
| 317 | static widget always answers so; `period` is then the server's 30 s) or |
| 318 | the hash matched (the object's `period`); |
| 319 | - `widgetPublicData { hash, data, period }`: the object's current shared state. |
| 320 |
| 321 | `period` is the recommended cadence of the poll, not a delay: an edit is |
| 322 | visible at once to every read (history, the update, the poll). It is the |
| 323 | server's: the bot's `cache_time` clamped to 5–3600 s, 30 s without one (the |
| 324 | server's numbers of 2026-10-26, after the final spec's 2–3600 s and 5 s); the |
| 325 | next poll is planned by it (30 s before the first answer). The client |
| 326 | does not poll after an answer: a bot that wants the widget to have the new |
| 327 | state at once returns it in `result`. |
| 328 |
| 329 | | Error | Cause | |
| 330 | |---|---| |
| 331 | | `INPUT_MEDIA_WIDGET_DATA_TOO_LARGE`, `INPUT_MEDIA_WIDGET_DATA_INVALID` | `chat_data` / `public_data` at sending or in an inline result: over 1024 bytes, or not UTF-8 text (NUL bytes) | |
| 332 | | `WIDGET_DATA_TOO_LARGE`, `WIDGET_DATA_INVALID` | the same in an edit; `WIDGET_DATA_INVALID` also for a `result` that is not text | |
| 333 | | `WIDGET_RESULT_TOO_LARGE` | `result` over 1024 bytes | |
| 334 | | `WIDGET_QUERY_DATA_INVALID` | `data` empty or over 64 bytes | |
| 335 | | `WIDGET_QUERY_NOT_ALLOWED` | the widget has no `allow_queries`, a forward of a widget without `fwd_allow_queries`, or requests are not allowed there | |
| 336 | | `WIDGET_RESPONSE_TIMEOUT` | the bot did not answer in 15 s | |
| 337 | | `WIDGET_ID_INVALID` | an unknown `widget_id`, or another bot's object | |
| 338 | | `INPUT_MEDIA_WIDGET_FWD_ALLOW_QUERIES_NOT_ALLOWED` | `fwd_allow_queries` from a bot outside the whitelist (at sending or in an inline result) | |
| 339 | | `CHAT_SEND_PHOTOS_FORBIDDEN` | the bot may not send media in that group | |
| 340 | | `MESSAGE_ID_INVALID` | no such widget message, or not in this chat, or not this bot's, or the original is gone | |
| 341 | | `QUERY_ID_INVALID` | the `widget_query_id` is unknown, expired (15 s) or of another kind | |
| 342 | | `INLINE_RESULT_EXPIRED` | a widget result sent by an invalid or expired id (as for any inline result) | |
| 343 | | `FLOOD_WAIT_<N>` | more than 5 requests per 5 s per user and bot; a message's state edits over the `sendMessage` flood control; more than one object edit per second | |
| 344 |
| 345 | **Old clients.** Below layer 233: `messageMediaUnsupported` with the caption, |
| 346 | as for any widget, and no widget results in inline lists. |
| 347 |
| 348 | ## The Bot API (10.4) |
| 349 |
| 350 | Bots see the same widget under Bot API names (checked against the beta, |
| 351 | `https://api.telegram.org/beta/bot<token>/test/<method>`, on 2026-10-28; |
| 352 | `scripts/conformance/test-bot.ts` speaks it): |
| 353 |
| 354 | | Layer 233 (this document) | Bot API 10.4 | |
| 355 | |---|---| |
| 356 | | `inputMediaWidget` | `sendWidget`; in inline mode an article with `InputWidgetMessageContent` | |
| 357 | | `allow_queries` | `allow_queries` | |
| 358 | | `shared_widget` | `is_shared` (the sent `Widget` has `id`) | |
| 359 | | `fwd_allow_queries` | none of its own: a widget sent with `is_shared` and `allow_queries` is one whose forwards may ask | |
| 360 | | `inputMediaWidgetId`, `inputBotInlineMessageWidgetId` | `sendSharedWidget`, `InputSharedWidgetMessageContent` | |
| 361 | | `messageMediaWidget`: `is_copy`, `can_query`, `background_query` | `Widget`: `is_copy`, `can_send_queries`, `can_send_background_queries` | |
| 362 | | the request from an original (also via the bot) | update `widget_query`: `id`, `from`, `chat` and `message_id` or `inline_message_id`, `chat_instance`, `chat_type` (via the bot), `data`, `widget_id` (a shared widget's) | |
| 363 | | the request from a copy of a shared widget | update `shared_widget_query`: `id`, `from`, `data`, `widget_id` | |
| 364 | | `result` | `answerWidgetQuery`'s `data` | |
| 365 | | the message's state edit | `editMessageWidgetData` (`chat_id` + `message_id` or `inline_message_id`) | |
| 366 | | `messages.editWidgetData` | `editSharedWidgetData` (`widget_id`, `public_data`, `cache_time`) | |
| 367 |
| 368 | What the beta does: the HTML is refused past 32768 bytes or past 4096 bytes of |
| 369 | PHP `gzcompress($html, 9)` (`INPUT_MEDIA_WIDGET_TOO_LARGE`; a Node build's own |
| 370 | zlib compresses random text about 18 bytes tighter, `src/shared/widget-size.ts`); |
| 371 | `chat_data`, `public_data` and the answer's `data` take 1024 bytes each, counted |
| 372 | in bytes; `cache_time` is clamped to 3600 above but 4 is refused; `copyMessage` |
| 373 | of a widget is refused; a forward carries `is_copy` and the `public_data` of the |
| 374 | moment, no `chat_data`; a forward of a shared widget keeps its `id` and |
| 375 | `can_send_queries`. `can_send_background_queries` was not set on any message |
| 376 | yet, the private chat with `allow_queries` included. An `editMessageWidgetData` |
| 377 | that changes nothing is refused with `message is not modified`, as |
| 378 | `editMessageText` is; a bot takes it as done and still answers the query (a |
| 379 | query left unanswered ends in the widget as `TIMEOUT`). |
| 380 |
| 381 | ## Caption, preview, old clients |
| 382 |
| 383 | The message's ordinary media caption is the text for the preview, the chat |
| 384 | list and search; without a caption the client shows the localized "Widget" |
| 385 | stub, as for other media. Clients below layer 233 receive |
| 386 | `messageMediaUnsupported` with the caption text and show their usual |
| 387 | placeholder; a new client keeps that fallback for messages it decides not to |
| 388 | render. |
| 389 |
| 390 | The server has no flag for widgets: each client enables rendering itself, by |
| 391 | platform and version (a debug or rollout flag on the client side). |
| 392 |
| 393 | ## Size anchors |
| 394 |
| 395 | `width` and `height` are always present in the media; `0` means auto, and |
| 396 | each axis is independent. The server passes them through unchanged; the client |
| 397 | keeps them within sensible bounds. How the client turns them into the bubble |
| 398 | before the widget has loaded is in `docs/integration.md` ("Bubble sizing"). |
| 399 |
| 400 | ## Writing a mini-widget |
| 401 |
| 402 | 4 KB compressed is about 12–20 KB of ordinary HTML, CSS and JS: a card with a |
| 403 | few controls, a chart drawn on a canvas, an animation. What does not fit: |
| 404 | embedded images beyond tiny icons (a `data:` PNG compresses badly), bundled |
| 405 | libraries, long text. Widget profile 1 (`docs/profile-v1.md`) and SDK v1 |
| 406 | (`docs/sdk-v1.md`) exist partly to keep widgets small: the client provides |
| 407 | the theme, the button style and the APIs every widget would otherwise embed, |
| 408 | and there are no bundled libraries. |
| 409 |
| 410 | The server compresses with zlib at level 9 (PHP `gzcompress($html, 9)`), so an |
| 411 | author can measure what it counts: |
| 412 | `python3 -c 'import sys,zlib; print(len(zlib.compress(sys.stdin.buffer.read(), 9)))' < widget.html` |
| 413 | (`gzip -9 -n` counts about 12 bytes more: its header and trailer are longer), the |
| 414 | harness's `size:` line or the dev app's stats panel (both warn above 3800 B, |
| 415 | the margin the conformance set keeps, and flag anything over a limit). A bot |
| 416 | that gets `INPUT_MEDIA_WIDGET_TOO_LARGE` for HTML under 32 KB has hit the |
| 417 | compressed limit. |
| 418 | How to write widgets that fit, and everything else an author needs: |
| 419 | `docs/widget-runtime-profile.md`. |
| 420 |
| 421 | ## Testing |
| 422 |
| 423 | Widgets come only as `messageMediaWidget`: an `.html` a user uploads as a |
| 424 | document is a file, not a widget. Testing goes through real layer-233 |
| 425 | messages, the conformance set included. Every conformance widget fits a mini-widget (`packages/conformance` |
| 426 | builds each one under 3800 bytes after zlib at level 9). A test bot sends |
| 427 | it to a tester's private chat with its reference picture as a photo next to |
| 428 | it, in the order of `packages/conformance/channel.json`, and the messages are |
| 429 | forwarded into the shared test channel. That also covers the corpus widgets |
| 430 | that fit, forward-copy storage and the size anchors. The channel holds |
| 431 | forwards, so there every widget is a copy (`is_copy`; requests only for a |
| 432 | widget sent with `fwd_allow_queries`): requests in the background and the chat |
| 433 | state are tested in the tester's private chat with the bot, and a group's in a |
| 434 | test group the bot is a member of. |
| 435 |
| 436 | ## Profile and versions |
| 437 |
| 438 | The HTML is written against widget profile 1 (`docs/profile-v1.md`; |
| 439 | `packages/core/profile.json` is the source). A widget learns the client's |
| 440 | profile at runtime through `TelegramWidget.profile`, `isProfileAtLeast(n)`, |
| 441 | `CSS.supports` and `@supports`, all answered from the same tables. The payload |
| 442 | itself has no version field: the profile is the framework build's, the media |
| 443 | layout is the layer's. |
| 444 |