Skip to main content

Message entities

Message entities are Telegram's structured, non-visual way of marking up a message. Instead of embedding formatting characters in the text itself, the Bot API describes each styled span — bold, italic, a link, a mention, and similar — as its own entry naming the formatting type and the offset and length of the text it covers. It is an alternative to inline markup formats like MarkdownV2: when you send text using MarkdownV2 syntax, Telegram parses that syntax and converts it into entities before storing and delivering the message, so the plain text and its formatting end up represented separately rather than interleaved.

Spans over text, not characters in text

An inline markup language like MarkdownV2 keeps formatting and content in the same string — a bold word is wrapped in asterisks right where it appears. A message entity instead describes formatting as coordinates into the underlying plain text: which formatting applies, and where it starts and how long it runs, counted in UTF-16 code units rather than characters — which matters for anything containing emoji, since those count as two units rather than one. The text a client displays never contains markup characters at all; the formatting lives entirely in the accompanying list of spans.

Why Telegram represents it this way

Separating structure from content avoids a whole class of escaping problems: a plain-text span can contain any character, including ones that would otherwise need escaping in an inline syntax, because nothing in the text itself is being interpreted as a formatting instruction. It also makes formatting easy to strip or ignore — a client with no rich-text support can just read the plain text and skip the entity list — where stripping inline markup would require parsing it first.

How MarkdownV2 relates to entities

MarkdownV2 is one input format Telegram accepts and converts, not a competing representation. Send a message with MarkdownV2 formatting and Telegram parses the asterisks, underscores and brackets in the text, then stores the result as plain text plus entities. Autogram's posts go out this way — generated content is escaped and formatted as MarkdownV2 on the way out, and Telegram performs the conversion into entities on its side. Reading a message back exposes only the entity form; the original MarkdownV2 syntax the sender typed is not preserved anywhere.

A worked example with offsets

Take the plain text "Hello world". Suppose you want the word "world" in bold. Offsets start at zero, so "world" begins at position 6 and is 5 units long. The entity is then a bold type with offset 6 and length 5. Now put a single emoji at the start, followed by a space, then "Hello world". A typical emoji takes two UTF-16 units, so every offset shifts by three, counting the space. The word "world" now starts at 9. If you count visible characters you get 8, and the bold lands on the wrong letters.

Common mistake: bold in the wrong place

The classic sign of an offset bug is formatting that sits a few characters off, usually after an emoji or a non-Latin letter. It happens when code counts characters or bytes instead of UTF-16 code units. Another sign is formatting that vanishes after you edit the text. If you change the plain text but keep the old entity list, the spans point at the wrong place or past the end. Whenever the text changes, the spans must be recalculated. Test with a short sample that has an emoji before the styled word.

How to inspect entities on a real post

The Bot API returns entities with a message as a list of objects, each with a type, an offset and a length. Text links also carry a URL. When a message has a caption, its spans are kept in a separate list from the ones for plain text. To check a post, read the message back and compare each span with the text it covers. If a word you meant to style is not covered, the formatting was not applied. With MarkdownV2 input, an unescaped reserved character is a common cause of a rejected message, usually reported as a "can't parse entities" error.

Put these concepts to work

Automate your Telegram publishing with AI content, quality scoring, and smart scheduling.