Formatting

HTML & Markdown

gogram parses your text into typed MessageEntity objects before it goes on the wire. Pick the syntax you prefer — HTML or Markdown — and the result is identical.

Two parse modes

Telegram does not transmit your text as a styled string. It sends a plain UTF-16 body plus a list of entities(offset, length, kind)triples that say "these characters are bold," "these characters are a link to that URL." Parse modes are convenience layers on top: you write tags, gogram turns them into entities.

Two are supported: HTML and Markdown. Both are case-insensitive when set in config.

Setting a default

Set ParseMode on the client and you only have to think about it once:

client, _ := telegram.NewClient(telegram.ClientConfig{
	AppID:     12345,
	AppHash:   "...",
	Session:   "bot.session",
	ParseMode: "HTML",
})
client, _ := telegram.NewClient(telegram.ClientConfig{
	AppID:     12345,
	AppHash:   "...",
	Session:   "bot.session",
	ParseMode: "HTML",
})

Every SendMessage / SendMedia / EditMessage picks up the default. Override per-message via SendOptions.ParseMode; set it to "" to disable parsing for a single send.

HTML

client.SendMessage("me",
	"<b>build</b> <i>passing</i> &mdash; <a href=\"https://gogram.dev\">link</a>",
	&telegram.SendOptions{ParseMode: "HTML"})
client.SendMessage("me",
	"<b>build</b> <i>passing</i> &mdash; <a href=\"https://gogram.dev\">link</a>",
	&telegram.SendOptions{ParseMode: "HTML"})

Supported tags:

  • <b>, <strong> for bold
  • <i>, <em> for italic
  • <u>, <ins> for underline
  • <s>, <strike>, <del> for strikethrough
  • <code> for inline mono
  • <pre> for code blocks, with optional language="go" for syntax-highlighted blocks
  • <blockquote> for quotes, with optional expandable attribute
  • <span class="tg-spoiler"> or <tg-spoiler> for spoilers
  • <a href="..."> for links; tg://user?id=N for inline user mentions
  • <emoji id="..."> for custom (premium) emoji

Tags must nest properly. Self-closing variants are not accepted.

Markdown

client.SendMessage("me",
	"**ship it** — see [docs](https://gogram.dev)",
	&telegram.SendOptions{ParseMode: "Markdown"})
client.SendMessage("me",
	"**ship it** — see [docs](https://gogram.dev)",
	&telegram.SendOptions{ParseMode: "Markdown"})

Supported syntax:

  • **bold** — bold
  • __italic__ or _italic_ — italic
  • ~~strike~~ — strikethrough
  • --underline-- — underline
  • `inline code` — mono
  • ```lang\\n...\\n``` — code block with optional language
  • > quoted at line start — blockquote
  • ||spoiler|| — spoiler
  • [label](url) — link; tg://user?id=N for inline mentions

Escaping

In HTML, use the standard entity references: &lt;, &gt;, &amp;, &quot;. In Markdown, backslash-escape any of*, _, ~, `, |, [,], (, ), >, \\ when you want the literal character.

Converting between formats

The library exposes converters for one-shot transformations:

// HTML in, Markdown out
md := telegram.ToMarkdown("<b>hi</b>")

// Markdown in, HTML out is implicit when you set ParseMode
// to Markdown: gogram parses it, runs the entity layer, and ships
// MessageEntity objects on the wire.
// HTML in, Markdown out
md := telegram.ToMarkdown("<b>hi</b>")

// Markdown in, HTML out is implicit when you set ParseMode
// to Markdown: gogram parses it, runs the entity layer, and ships
// MessageEntity objects on the wire.