Overview
Message entities allow you to apply rich formatting to portions of message text. Each entity specifies a range of text (by start index and length) and a formatting style or semantic meaning.Proto Definition
Common Fields
All entity types share these base fields:The 0-based index of the first character in the entity range. Measured in Unicode code points, not bytes.Example: For “Hello world”, the word “world” starts at index 6.
The number of characters (Unicode code points) covered by this entity.Example: For “Hello world”, the word “world” has length 5.
Entity Types
Text Formatting
Bold
Renders the text in bold/strong formatting.
Italic
Renders the text in italic/emphasized formatting.
Underline
Renders the text with underline formatting.
Strikethrough
Renders the text with strikethrough formatting (crossed out).
Code Formatting
Inline Code
Renders the text as inline code with monospace font and background highlighting.
console.log() for debugging”, formats “console.log()” as inline code.
Code Block (Pre)
Renders the text as a code block with optional syntax highlighting.Fields:
language(optional string) - Programming language for syntax highlighting (e.g., “javascript”, “python”, “rust”)
Links and URLs
Implicit URL
Marks a portion of text as a clickable URL where the displayed text is the URL itself.Used when the message content itself is the URL (e.g., “https://example.com”).
Text URL (Masked Link)
Creates a hyperlink where the displayed text differs from the actual URL.Fields:
url(string, required) - The actual URL to link to
Mentions
User Mention (By ID)
Mentions a user by their ID. The text should be in the format “@username” or similar.The user ID is determined from context (typically the text content or the message’s author).
Username Mention (By Name)
Mentions a user by their username. Similar to
user_mention but references the user by name instead of ID.Special Entities
Spoiler
Hides the text behind a spoiler warning. Users must click/tap to reveal the content.
Custom Emoji
Replaces text with a custom emoji image.Fields:
emoji_id(fixed64, required) - Snowflake ID of the custom emoji
Multiple Entities
Multiple entities can be applied to the same message, including overlapping ranges: Example: Bold and ItalicComplex Example
- A user mention (“@alice”)
- Inline code (“console.log(‘Hello’)”)
- A clickable URL (“https://docs.example.com”)
Important Notes
Index Calculation
- Start indices are 0-based: The first character is at index 0
- Indices count Unicode code points, not bytes or UTF-16 code units
- Emoji count as one code point (in most cases)
- Combining characters may affect index calculations
Entity Ordering
- Entities can be specified in any order
- Multiple entities can overlap or nest within each other
- The rendering order is determined by the client implementation
Entity Validation
- Ensure
start_index + lengthdoes not exceed the message length - Invalid indices may cause the entity to be ignored or the message to be rejected
- Empty entities (length = 0) are typically ignored
Supported Combinations
Most formatting entities can be combined:- ✅ Bold + Italic
- ✅ Bold + Underline
- ✅ Link + Bold/Italic
- ✅ Code + Spoiler (code block hidden by spoiler)
- ❌ Code block (pre) typically doesn’t combine with inline formatting
Use in Edits
When editing a message:- Providing new entities replaces all existing entities
- Omitting entities when changing text clears all entities
- To preserve entities, include them in the edit request