rich

package
v0.157.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package rich builds Telegram rich messages.

Rich messages (layer 227, Bot API 10.1) carry highly structured content: headings, paragraphs, lists, tables, block quotes, media, anchors, footnotes, mathematical expressions and more. Unlike ordinary messages — which are a flat string plus a list of tg.MessageEntityClass ranges — a rich message is a tree of tg.PageBlockClass blocks whose inline text is a tree of tg.RichTextClass nodes (the same model Telegram uses for Instant View pages).

This package provides three things:

The result is a tg.InputRichMessageClass that can be sent or edited through the message sender (see (*message.Builder).RichMessage).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Anchor

func Anchor(text tg.RichTextClass, name string) *tg.TextAnchor

Anchor marks the given text as the target of an in-document anchor with the given name (textAnchor). See AnchorLink to link to it.

func AnchorBlock

func AnchorBlock(name string) *tg.PageBlockAnchor

AnchorBlock returns an anchor block with the given name (pageBlockAnchor), usable as the target of an AnchorLink.

func AnchorLink(text tg.RichTextClass, name string) *tg.TextURL

AnchorLink links the given text to an in-document Anchor with the given name, rendered as a relative URL.

func Audio

func Audio(audioID int64, caption tg.PageCaption) *tg.PageBlockAudio

Audio returns an audio block referencing a document by ID with an optional caption (pageBlockAudio).

func AuthorDate

func AuthorDate(author tg.RichTextClass, publishedDate int) *tg.PageBlockAuthorDate

AuthorDate returns an author and publication date block (pageBlockAuthorDate).

func AutoEmail

func AutoEmail(texts ...tg.RichTextClass) *tg.TextAutoEmail

AutoEmail formats the given text as an automatically detected email address (textAutoEmail).

func AutoPhone

func AutoPhone(texts ...tg.RichTextClass) *tg.TextAutoPhone

AutoPhone formats the given text as an automatically detected phone number (textAutoPhone).

func AutoURL

func AutoURL(texts ...tg.RichTextClass) *tg.TextAutoURL

AutoURL formats the given text as an automatically detected URL (textAutoUrl).

func BankCard

func BankCard(texts ...tg.RichTextClass) *tg.TextBankCard

BankCard formats the given text as a bank card number (textBankCard).

func Blockquote

func Blockquote(text, caption tg.RichTextClass) *tg.PageBlockBlockquote

Blockquote returns a block quotation with an optional caption (pageBlockBlockquote). Use BlockquoteBlocks to quote multiple blocks.

func BlockquoteBlocks

func BlockquoteBlocks(caption tg.RichTextClass, blocks ...tg.PageBlockClass) *tg.PageBlockBlockquoteBlocks

BlockquoteBlocks returns a block quotation of multiple blocks with an optional caption (pageBlockBlockquoteBlocks).

func Bold

func Bold(texts ...tg.RichTextClass) *tg.TextBold

Bold formats the given text as bold (textBold).

func BotCommand

func BotCommand(texts ...tg.RichTextClass) *tg.TextBotCommand

BotCommand formats the given text as a bot command (textBotCommand).

func Caption

func Caption(text, credit tg.RichTextClass) tg.PageCaption

Caption returns a page caption with optional credit (pageCaption). Pass Empty for an absent text or credit.

func Cashtag

func Cashtag(texts ...tg.RichTextClass) *tg.TextCashtag

Cashtag formats the given text as a cashtag (textCashtag).

func Cell

func Cell(text tg.RichTextClass) tg.PageTableCell

Cell returns a table cell with the given text (pageTableCell). Set Header, alignment, Colspan and Rowspan on the result as needed.

func CheckListItem

func CheckListItem(checked bool, text tg.RichTextClass) *tg.PageListItemText

CheckListItem returns an unordered task-list item holding inline text, with a checkbox in the given checked state (pageListItemText).

func Collage

func Collage(caption tg.PageCaption, items ...tg.PageBlockClass) *tg.PageBlockCollage

Collage returns a collage of blocks with an optional caption (pageBlockCollage).

func Concat

func Concat(texts ...tg.RichTextClass) *tg.TextConcat

Concat concatenates the given rich text nodes (textConcat).

func Cover

func Cover(cover tg.PageBlockClass) *tg.PageBlockCover

Cover wraps a block as a cover (pageBlockCover).

func CustomEmoji

func CustomEmoji(documentID int64, alt string) *tg.TextCustomEmoji

CustomEmoji returns an inline custom emoji referencing a document by ID, with the given fallback alt text (textCustomEmoji).

func Date

func Date(text tg.RichTextClass, date int, flags DateFlags) *tg.TextDate

Date formats the given text as the Unix timestamp date, rendered according to flags (textDate).

func Details

func Details(open bool, title tg.RichTextClass, blocks ...tg.PageBlockClass) *tg.PageBlockDetails

Details returns a collapsible details block (pageBlockDetails). When open is true the block is expanded by default.

func Divider

func Divider() *tg.PageBlockDivider

Divider returns a divider block (pageBlockDivider).

func Email

func Email(text tg.RichTextClass, email string) *tg.TextEmail

Email formats the given text as a link to an email address (textEmail).

func Empty

func Empty() *tg.TextEmpty

Empty returns an empty rich text node (textEmpty).

func Fixed

func Fixed(texts ...tg.RichTextClass) *tg.TextFixed

Fixed formats the given text as fixed-width / monospace (textFixed).

func Footer(text tg.RichTextClass) *tg.PageBlockFooter

Footer returns a footer block (pageBlockFooter).

func HTML

func HTML(html string) *tg.InputRichMessageHTML

HTML wraps an HTML source into an inputRichMessageHTML to be parsed by Telegram's servers. For attachments or flags, use Rich instead.

func Hashtag

func Hashtag(texts ...tg.RichTextClass) *tg.TextHashtag

Hashtag formats the given text as a hashtag (textHashtag).

func Header(text tg.RichTextClass) *tg.PageBlockHeader

Header returns a header block (pageBlockHeader).

func HeaderCell

func HeaderCell(text tg.RichTextClass) tg.PageTableCell

HeaderCell returns a header table cell with the given text (pageTableCell).

func Heading

func Heading(level int, text tg.RichTextClass) tg.PageBlockClass

Heading returns a heading block for the given level (1-6), clamped to that range.

func Heading1

func Heading1(text tg.RichTextClass) *tg.PageBlockHeading1

Heading1 returns a level-1 heading block (pageBlockHeading1).

func Heading2

func Heading2(text tg.RichTextClass) *tg.PageBlockHeading2

Heading2 returns a level-2 heading block (pageBlockHeading2).

func Heading3

func Heading3(text tg.RichTextClass) *tg.PageBlockHeading3

Heading3 returns a level-3 heading block (pageBlockHeading3).

func Heading4

func Heading4(text tg.RichTextClass) *tg.PageBlockHeading4

Heading4 returns a level-4 heading block (pageBlockHeading4).

func Heading5

func Heading5(text tg.RichTextClass) *tg.PageBlockHeading5

Heading5 returns a level-5 heading block (pageBlockHeading5).

func Heading6

func Heading6(text tg.RichTextClass) *tg.PageBlockHeading6

Heading6 returns a level-6 heading block (pageBlockHeading6).

func Image

func Image(documentID int64, w, h int) *tg.TextImage

Image returns an inline image referencing a document by ID with the given size in pixels (textImage).

func Italic

func Italic(texts ...tg.RichTextClass) *tg.TextItalic

Italic formats the given text as italic (textItalic).

func Kicker

func Kicker(text tg.RichTextClass) *tg.PageBlockKicker

Kicker returns a kicker block (pageBlockKicker).

func List

func List(items ...tg.PageListItemClass) *tg.PageBlockList

List returns an unordered list block (pageBlockList).

func ListItem

func ListItem(text tg.RichTextClass) *tg.PageListItemText

ListItem returns an unordered list item holding inline text (pageListItemText).

func ListItemBlocks

func ListItemBlocks(blocks ...tg.PageBlockClass) *tg.PageListItemBlocks

ListItemBlocks returns an unordered list item holding blocks (pageListItemBlocks).

func Map

func Map(geo tg.InputGeoPointClass, zoom, w, h int, caption tg.PageCaption) *tg.InputPageBlockMap

Map returns a map block centered on the given input location (inputPageBlockMap), with the given zoom level, size in pixels and an optional caption.

func Markdown

func Markdown(markdown string) *tg.InputRichMessageMarkdown

Markdown wraps a Markdown source into an inputRichMessageMarkdown to be parsed by Telegram's servers. For attachments or flags, use Rich instead.

func Marked

func Marked(texts ...tg.RichTextClass) *tg.TextMarked

Marked formats the given text as marked / highlighted (textMarked).

func Math

func Math(source string) *tg.TextMath

Math returns an inline mathematical expression with the given LaTeX source (textMath).

func MathBlock

func MathBlock(source string) *tg.PageBlockMath

MathBlock returns a block-level mathematical expression with the given LaTeX source (pageBlockMath).

func Mention

func Mention(texts ...tg.RichTextClass) *tg.TextMention

Mention formats the given text as a username mention (textMention).

func MentionName

func MentionName(text tg.RichTextClass, userID int64) *tg.TextMentionName

MentionName formats the given text as a mention of a user by ID (textMentionName).

func OrderedList

func OrderedList(items ...tg.PageListOrderedItemClass) *tg.PageBlockOrderedList

OrderedList returns an ordered list block (pageBlockOrderedList).

func OrderedListItem

func OrderedListItem(num string, text tg.RichTextClass) *tg.PageListOrderedItemText

OrderedListItem returns an ordered list item holding inline text, labelled with num (pageListOrderedItemText).

func OrderedListItemBlocks

func OrderedListItemBlocks(num string, blocks ...tg.PageBlockClass) *tg.PageListOrderedItemBlocks

OrderedListItemBlocks returns an ordered list item holding blocks, labelled with num (pageListOrderedItemBlocks).

func Paragraph

func Paragraph(text tg.RichTextClass) *tg.PageBlockParagraph

Paragraph returns a paragraph block (pageBlockParagraph).

func ParseHTML

func ParseHTML(r io.Reader) ([]tg.PageBlockClass, error)

ParseHTML parses HTML from r into rich message blocks.

It is a best-effort local renderer that covers the text-expressible subset of rich content: paragraphs, headings (h1-h6), dividers (hr), block quotes, preformatted/code blocks, ordered and unordered lists (including task lists), tables, and inline formatting (bold, italic, underline, strikethrough, fixed-width, spoiler, subscript, superscript, marked, links, anchors, custom emoji and inline math). Unknown elements are unwrapped to their content.

For full server-side fidelity (media, maps, footnotes and every documented tag) send the HTML via HTML instead, which lets Telegram parse it.

func ParseMarkdown

func ParseMarkdown(r io.Reader) ([]tg.PageBlockClass, error)

ParseMarkdown parses Markdown from r into rich message blocks.

It is a best-effort local renderer following Rich Markdown (GitHub Flavored Markdown plus Telegram extensions):

  • Blocks: headings, paragraphs, thematic breaks, block quotes, fenced/indented code, ordered and unordered lists (including task lists), tables (with column alignment), and block math via $$...$$ or ```math.
  • Inline: **bold**, *italic*, ~~strikethrough~~, `code`, ==marked==, ||spoiler|| and $math$.
  • Links: [t](url), [t](mailto:..), [t](tel:..) and [t](tg://user?id=N); images ![alt](tg://emoji?id=N) and ![alt](tg://time?unix=N&format=F).
  • Any other rich inline styles (underline, subscript, superscript, anchors, ...) are recognized when written as embedded HTML tags (<u>, <sub>, <sup>, <a name>, ...), as Rich Markdown allows.

Media images referencing a URL (![](photo.jpg)) and footnotes cannot be resolved locally; for full server-side fidelity send the Markdown via Markdown instead, which lets Telegram parse it.

func Phone

func Phone(text tg.RichTextClass, phone string) *tg.TextPhone

Phone formats the given text as a link to a phone number (textPhone).

func Photo

func Photo(photoID int64, caption tg.PageCaption) *tg.PageBlockPhoto

Photo returns a photo block referencing a photo by ID with an optional caption (pageBlockPhoto).

func Plain

func Plain(s string) *tg.TextPlain

Plain returns a plain (unstyled) rich text node (textPlain).

func Preformatted

func Preformatted(text tg.RichTextClass, language string) *tg.PageBlockPreformatted

Preformatted returns a preformatted (code) block with optional language (pageBlockPreformatted).

func Pullquote

func Pullquote(text, caption tg.RichTextClass) *tg.PageBlockPullquote

Pullquote returns a pull quotation with an optional caption (pageBlockPullquote).

func RelatedArticles(title tg.RichTextClass, articles ...tg.PageRelatedArticle) *tg.PageBlockRelatedArticles

RelatedArticles returns a related-articles block (pageBlockRelatedArticles).

func Row

func Row(cells ...tg.PageTableCell) tg.PageTableRow

Row returns a table row of the given cells (pageTableRow).

func Slideshow

func Slideshow(caption tg.PageCaption, items ...tg.PageBlockClass) *tg.PageBlockSlideshow

Slideshow returns a slideshow of blocks with an optional caption (pageBlockSlideshow).

func Spoiler

func Spoiler(texts ...tg.RichTextClass) *tg.TextSpoiler

Spoiler formats the given text as a spoiler (textSpoiler).

func Strike

func Strike(texts ...tg.RichTextClass) *tg.TextStrike

Strike formats the given text as strikethrough (textStrike).

func Subheader

func Subheader(text tg.RichTextClass) *tg.PageBlockSubheader

Subheader returns a subheader block (pageBlockSubheader).

func Subscript

func Subscript(texts ...tg.RichTextClass) *tg.TextSubscript

Subscript formats the given text as subscript (textSubscript).

func Subtitle

func Subtitle(text tg.RichTextClass) *tg.PageBlockSubtitle

Subtitle returns a subtitle block (pageBlockSubtitle).

func Superscript

func Superscript(texts ...tg.RichTextClass) *tg.TextSuperscript

Superscript formats the given text as superscript (textSuperscript).

func Table

func Table(title tg.RichTextClass, rows ...tg.PageTableRow) *tg.PageBlockTable

Table returns a table block with the given title (may be Empty) and rows (pageBlockTable). Set Bordered and Striped on the result to control styling.

func Thinking

func Thinking(text tg.RichTextClass) *tg.PageBlockThinking

Thinking returns a thinking block (pageBlockThinking), used to show an AI model's reasoning.

func Title

func Title(text tg.RichTextClass) *tg.PageBlockTitle

Title returns a title block (pageBlockTitle).

func URL

func URL(text tg.RichTextClass, url string, webpageID int64) *tg.TextURL

URL formats the given text as a link to url (textUrl).

webpageID may be zero if the linked webpage is not previewed.

func Underline

func Underline(texts ...tg.RichTextClass) *tg.TextUnderline

Underline formats the given text as underlined (textUnderline).

func Video

func Video(videoID int64, caption tg.PageCaption) *tg.PageBlockVideo

Video returns a video block referencing a document by ID with an optional caption (pageBlockVideo).

Types

type DateFlags

type DateFlags struct {
	// Relative renders the date relative to now ("5 minutes ago").
	Relative bool
	// ShortTime renders a short time.
	ShortTime bool
	// LongTime renders a long time.
	LongTime bool
	// ShortDate renders a short date.
	ShortDate bool
	// LongDate renders a long date.
	LongDate bool
	// DayOfWeek renders the day of the week.
	DayOfWeek bool
}

DateFlags selects which components of a Date are rendered.

type Message

type Message struct {
	// contains filtered or unexported fields
}

Message assembles a tg.InputRichMessage from blocks and attachments.

The zero value is not usable; create one with New. Methods return the same pointer so they can be chained, and Message.Input produces the request value.

func New

func New(blocks ...tg.PageBlockClass) *Message

New creates a rich message from the given top-level blocks.

func (*Message) Block

func (m *Message) Block(blocks ...tg.PageBlockClass) *Message

Block appends top-level blocks to the message.

func (*Message) Documents

func (m *Message) Documents(documents ...tg.InputDocumentClass) *Message

Documents sets the documents referenced by the message blocks.

func (*Message) Input

func (m *Message) Input() *tg.InputRichMessage

Input returns the assembled input rich message.

func (m *Message) NoAutoLink() *Message

NoAutoLink disables automatic detection of links, mentions and similar entities in the message text.

func (*Message) Photos

func (m *Message) Photos(photos ...tg.InputPhotoClass) *Message

Photos sets the photos referenced by the message blocks.

func (*Message) RTL

func (m *Message) RTL() *Message

RTL marks the message as right-to-left.

func (*Message) Users

func (m *Message) Users(users ...tg.InputUserClass) *Message

Users sets the users referenced by the message blocks.

type Source

type Source struct {
	// contains filtered or unexported fields
}

Source describes an HTML or Markdown rich message source to be parsed by Telegram's servers, together with its attached files.

The zero value is a valid empty source; configure it with the methods and finalize it with Source.HTML or Source.Markdown.

func Rich

func Rich() *Source

Rich starts a server-parsed rich message source.

func (*Source) Document added in v0.153.0

func (s *Source) Document(id string, document tg.InputDocumentClass) *Source

Document attaches a document referenced by the given id in the message source (inputRichFileDocument).

func (*Source) Files added in v0.153.0

func (s *Source) Files(files ...tg.InputRichFileClass) *Source

Files sets the files referenced by the message source.

func (*Source) HTML

func (s *Source) HTML(html string) *tg.InputRichMessageHTML

HTML finalizes the source as an HTML rich message parsed by the server (inputRichMessageHTML).

func (*Source) Markdown

func (s *Source) Markdown(markdown string) *tg.InputRichMessageMarkdown

Markdown finalizes the source as a Markdown rich message parsed by the server (inputRichMessageMarkdown).

func (s *Source) NoAutoLink() *Source

NoAutoLink disables automatic detection of links, mentions and similar entities in the message text.

func (*Source) Photo added in v0.153.0

func (s *Source) Photo(id string, photo tg.InputPhotoClass) *Source

Photo attaches a photo referenced by the given id in the message source (inputRichFilePhoto).

func (*Source) RTL

func (s *Source) RTL() *Source

RTL marks the message as right-to-left.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL