Developer docs

5. Media

On this page

Upload: CreateUpload(size, mime, name, sha256?, max_part_size?) → upload_id, part_size, part_count (or existing on dedupe), then UploadPart(upload_id, index, data) for part_count = ceil(size / part_size) parts (parallel allowed; every part is exactly part_size bytes except the last), then FinishUpload → FileRef. Always use the part_size from the response.

max_part_size (optional) is the largest part the client wants, e.g. smaller parts on a slow or lossy link so a lost packet stalls less data and a retried part costs less. The server uses the default (ServerConfig.upload_part_size, 512 KiB) when it is 0 or larger, otherwise the value rounded down to a multiple of 64 KiB, with a minimum of 64 KiB. The server may also choose something smaller than you asked for. Attach with SendMessage.media (InputPhoto{file_id} / InputDocument{file_id, kind, ...}).

Download: GetFile(file_id, offset, limit) (≤ 1 MiB per call), or GetFileUrl(file_id) for a short-lived signed HTTPS URL suitable for <img>, video streaming and HTTP range requests.

5.1 Stickers, custom emoji, reactions

  • Send a sticker: SendMessage{media: InputMedia.document{file_id, kind: KIND_STICKER}}. The server fills in Document.sticker (set, primary emoji, format) from its own records. The file_id must be a sticker’s file. Any other valid sticker file (512 px PNG/WebP, Lottie or WebM) is registered as a stand-alone sticker. Custom-emoji files can’t be sent as stickers.
  • Custom emoji in text: MessageEntity{type: TYPE_CUSTOM_EMOJI, custom_emoji_id}. The covered text must be the fallback emoji. Ids must belong to a CUSTOM_EMOJI set, at most 100 per message. Markup forms: MarkdownV2 ![👍](tg://emoji?id=<id>) and HTML <tg-emoji emoji-id="<id>">👍</tg-emoji>.
  • Reactions: a reaction is either a single standard emoji or "ce:<custom_emoji_id>" for a custom emoji. The same string comes back in Reaction.emoji. Anything else is REACTION_INVALID. Rules (Telegram’s):
    • SendReaction.reactions replaces all of my reactions on the message; an empty list removes them. The legacy single emoji field is a one-element list (empty = remove), so old clients keep working.
    • Free accounts have 1 reaction per message, premium accounts up to UserLimits.reactions_per_message (3). More is 400 REACTIONS_TOO_MANY with Error.capability = "limit:reactions_per_message" for free accounts (show the upsell). Clients replace a free user’s reaction when another one is tapped.
    • Custom-emoji reactions need Premium: 403 PREMIUM_ACCOUNT_REQUIRED, capability reactions. Reactions in Saved Messages are tags and need Premium (saved_tags).
    • Every added reaction must be allowed by Chat.available_reactions (ChatReactions: MODE_ALL with optional custom_disabled, MODE_SOME + list, MODE_NONE), else REACTION_INVALID. Admins with change_info set it with EditChat.available_reactions; private chats always allow all. Reactions I already have are kept when the chat setting (or my premium) changes, and resending a subset of them is always allowed.
    • Reaction.chosen / chosen_order (1-based, the order I sent them) are per viewer; the shared ReactionsChanged event carries only counts, so clients keep their own chosen state when applying it.
    • No reactions in a private chat across a block (YOU_BLOCKED_USER / USER_IS_BLOCKED).
    • Paid Star reactions are separate: PaymentsService/SendPaidReaction (§17), shown as Reaction{emoji: "⭐", paid: true}.
  • Animated-emoji interactions: when a user taps a single-emoji message, send SetTyping{action: ACTION_EMOJI_INTERACTION, emoji, message_id, interaction}. The peer answers with ACTION_EMOJI_SEEN. These are relayed as ephemeral Typing updates, only in private chats, only for messages whose whole text is that single emoji, and are rate-limited. interaction is JSON of at most 1 KiB.
  • Emoji matching ignores U+FE0F, so ❤️ and ❤ are the same.
  • Set short names: [A-Za-z][A-Za-z0-9_]{0,63}, case-insensitive unique, no __ and no trailing _. The prefixes televo, official, admin, system and support are reserved. Sets created by bots must end in _by_<bot_username>.

5.2 Video qualities (server renditions)

A cloud-chat Document of kind KIND_VIDEO may carry alternatives: lower-quality renditions the server made from file, smallest first. Each VideoQuality is a separate file (file: file_id/unique_id/size) with display width/height, average bitrate (bits/s) and codec ("h264"). Renditions are progressive H.264 High/AAC-LC MP4 with the moov atom first and a keyframe every 2 s; they are downloaded exactly like any file (GetFile ranges or a signed URL with HTTP Range).

  • Lifecycle. The message is sent and delivered with the original only. After the media job (probe, poster, faststart) the server queues the renditions; each one that finishes is appended and published as an edit_message event with edit_date unchanged (not a user edit; don’t show “edited”). Forwards and copies share the same files. Renditions are reference-counted with the message: deleting the message (or replacing its media) frees them.
  • Ladder. 480p and 720p (short side, so portrait video too), plus 1080p when the server is configured for it. A rung is produced only when the source’s short side is at least that size and its bitrate is clearly above the rung’s cap (480p: CRF 26, ≤ 1 Mbit/s video + 64 kbit/s audio; 720p: CRF 23, ≤ 2.5 Mbit/s + 96 kbit/s; 1080p: CRF 23, ≤ 5 Mbit/s + 128 kbit/s), and only if the result is smaller than the source. So a video the client already compressed to 720p gets at most a 480p rendition, and a small video none. Videos below the size threshold or above the duration limit, and videos over the sender’s daily transcode budget, get none. Animations, round video notes and stickers are never transcoded.
  • Secret chats never have alternatives: the server can’t see their media.
  • Choosing. Clients pick per playback: the smallest rendition whose short side covers the video’s displayed short side, stepped down while its bitrate exceeds ~80 % of the measured throughput; “data saver” = smallest, “maximum” = file. A quality switch re-opens the new file at the current position. Always download and save file (the original); the renditions are for streaming.
  • Switching during playback (auto mode; reference: SDK AbrController). The display cap is what the rule above picks with unlimited bandwidth; nothing ever switches above it, and data saver, maximum or a quality picked by hand never switch automatically.
    • Down: a stall (buffering > 3 s while playing) steps down one rung.
    • Up: one rung at a time, only when the throughput estimate (EWMA of the client’s 512 KiB range fetches, α = 0.3) has stayed ≥ 1.5 × the next rung’s bitrate (the real file bitrate from alternatives; for the original size × 8 / duration) for 10 s without a break, with ≥ 5 s buffered ahead, and not within 30 s of a step-down (or 10 s of the previous step-up). The 10 s window restarts after every switch, so short spikes never trigger it.
    • Backoff: a stall within 20 s of a step-up blocks that rung for the rest of the session.
    • No-stall step-up: pick the switch point at the next 2 s keyframe boundary ≥ 4 s ahead, prefetch the target’s head (moov, ≥ 256 KiB) and ~6 s of it from there (byte offsets estimated from the average bitrate, +25 %) into the local range cache, and re-open the target at exactly that position when the playhead gets there. libmpv can’t swap files seamlessly, so this is a re-open served from the local cache. Measured on macOS (media_kit 1.2.6, loopback televod, 60 s 1080p clip, 480p → 720p): 36–40 ms from re-open to playback past the switch point, with no upstream fetch during the open; an unprefetched switch took 68–449 ms.
  • Bot API: Video.qualities (Bot API 9.4 VideoQuality: file_id, file_unique_id, width, height, codec, file_size).

Why progressive MP4 and not HLS/DASH: a file per rendition reuses everything a file already has (per-file encryption, ranged GetFile/signed URLs, refcount/GC, unique_id caching, the clients’ chunk-cache proxy) with no playlist endpoint, no per-segment signed URLs and no segment objects in the blob store. libmpv (media_kit) only picks an HLS variant when it opens the stream and has no throughput-driven switching during playback, so HLS would not buy adaptive switching for our main player anyway; and renditions are short-form message videos (minutes, not hours), for which a re-open at the current position, with keyframes every 2 s, is a sub-second switch. Telegram does the same (video.qualities are separate MP4 files).

Generated from docs/PROTOCOL.md in the Telvoro repository. Wire names such as televo.v1 are unchanged.