Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Messages and Embeds

Formatting Text and Escaping

Format Telegram and Discord messages safely - HTML and MarkdownV2 parse modes, escaping user text, Discord markdown, mentions, timestamps and allowed_mentions - with runnable helper functions.

Plain text works, but bold headings, code blocks, links and mentions make bot replies readable. Both platforms support formatting, and both punish you in the same way when you forget to escape user-supplied text: a Telegram message with a stray _ fails with 400 can't parse entities, and a Discord message with an unescaped @everyone pings the whole server. This lesson gives you the rules and the helper functions to apply them.

Telegram: three ways to format

sendMessage accepts a parse_mode:

Mode Example Notes
(none) plain text No escaping needed. Safest default.
HTML <b>bold</b> <i>italic</i> <code>x</code> <a href="https://example.com">link</a> Only <, > and & need escaping. Recommended.
MarkdownV2 *bold* _italic_ [link](https://example.com) Many characters must be escaped with a backslash. Easy to get wrong.
Markdown legacy Do not use.

The third option is an alternative to parse_mode entirely: send the plain text plus an entities array (type, offset, length) and no parsing happens at all. That is the most robust method for text you assemble from user input, but the offsets are in UTF-16 units and are tedious to compute by hand.

HTML mode supports b, strong, i, em, u, s, code, pre (with <code class="language-js">), a, blockquote, tg-spoiler and tg-emoji. The rule: escape every piece of dynamic text with & -> &amp;, < -> &lt;, > -> &gt;.

Node.js Escaping for Telegram HTML and MarkdownV2

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Prefer HTML mode: its escaping rule is tiny and hard to forget, so it breaks less often than MarkdownV2. A message may have at most 4096 characters after entity parsing, and a media caption at most 1024; both limits count the visible text.

Escape every variable you interpolate: names, messages quoted back to users, filenames, API results. A user whose first name is <b> is not hypothetical, and an unescaped value in HTML mode can break your formatting or let a user inject links.

  • A link: <a href="https://example.com">text</a>.
  • Mention a user without a username: <a href="tg://user?id=123456">Ana</a>; the user gets a notification.
  • Mention by username: @username (plain text works).
  • disable_notification: true sends silently. link_preview_options: { is_disabled: true } suppresses link previews.

Discord: markdown that is almost standard

Discord renders a Markdown dialect in content and in embed descriptions and field values:

You write Result
**bold**, *italic*, __underline__, ~~strike~~ Styled text
`code` and triple-backtick blocks with a language Code (blocks get syntax highlighting)
> quote, >>> multi-line quote Quotes
# Heading, ## Heading, -# small text Headers and subtext (start of a line)
`
[text](https://example.com) Masked link (not in plain bot content for every context, works in embeds)
\* Escape a formatting character

Special tokens for objects use ids in angle brackets:

Token Meaning
<@USER_ID> Mention a user
<#CHANNEL_ID> Link to a channel
<@&ROLE_ID> Mention a role
</roll:COMMAND_ID> Clickable slash command
<:name:ID> and <a:name:ID> Custom and animated emoji
<t:UNIX_SECONDS:R> Timestamp shown in each reader's own time zone

The timestamp token is wonderful: <t:1760000000:R> renders as "2 hours ago" (updating live), :F gives a full date and :t a short time, all in the reader's time zone and language, so you never need to format dates yourself.

Node.js Discord formatting helpers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Controlling who gets pinged: allowed_mentions

When your bot repeats user text, a user can make it ping @everyone. Discord lets you decide, per message, which mentions are allowed to ping with the allowed_mentions object. The safest default is to allow none:

JSON
{ "content": "Hello <@111>, welcome!", "allowed_mentions": { "parse": [] } }

With parse: [] mentions still appear as highlighted text but nobody is notified. To ping a specific user only, use { "users": ["111"] }. In discord.js you can set allowedMentions once in the Client options so every message inherits it. A moderation or quoting bot should never run without it.

Node.js Applying a mention policy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Putting formatting in one place

Build small functions per platform (fmt.bold, fmt.code, fmt.user) and have your commands return structured replies such as { title, lines, ... }. A platform adapter turns that into HTML for Telegram or markdown for Discord. This keeps escaping in exactly one module, and it makes cross-platform bots maintainable. Discord Embeds shows the richer structure Discord offers beyond plain markdown.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which characters must be escaped in Telegram HTML mode?

    HTML mode only needs the three characters that form markup: < to <, > to >, & to &.
  2. 2. Why is HTML parse mode usually preferred over MarkdownV2?

    MarkdownV2 requires escaping many characters, including dots and dashes. Forgetting one gives a parse error.
  3. 3. What does <t:1760000000:R> do in a Discord message?

    Timestamp tokens render per reader, with style letters such as R for relative and F for a full date.
  4. 4. How do you stop a bot message from pinging @everyone?

    allowed_mentions with an empty parse list prevents all pings while leaving the mention text visible.

Summary

  • Telegram: prefer parse_mode: "HTML" and escape every dynamic value with &amp;, &lt;, &gt;; MarkdownV2 needs many escapes; entities avoids parsing.
  • Limits: 4096 characters per message and 1024 per caption on Telegram, 2000 for Discord content.
  • Discord markdown plus id tokens (<@id>, <#id>, <@&id>, <t:unix:R>) cover mentions, channel links and time.
  • Use allowed_mentions (usually parse: []) whenever you repeat user text.
  • Keep escaping in one module and have commands return structured replies.

Next: Discord Embeds