|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/td/telegram/message/rich" |
| 7 | + "github.com/gotd/td/tg" |
| 8 | +) |
| 9 | + |
| 10 | +// SendRichMessage sends a rich message (Bot API 10.1): structured content — |
| 11 | +// headings, paragraphs, lists, tables, block quotes, media, math and more — as |
| 12 | +// a tree of page blocks rather than a flat string with entity ranges. |
| 13 | +// |
| 14 | +// Build the content with github.com/gotd/td/telegram/message/rich, for example: |
| 15 | +// |
| 16 | +// msg := rich.New( |
| 17 | +// rich.Heading1(rich.Plain("Title")), |
| 18 | +// rich.Paragraph(rich.Bold(rich.Plain("Hello"))), |
| 19 | +// ).Input() |
| 20 | +// bot.SendRichMessage(ctx, chat, msg) |
| 21 | +// |
| 22 | +// For whole-document HTML or Markdown, prefer SendRichHTML / SendRichMarkdown. |
| 23 | +// The usual SendOptions (reply, silent, protect, reply markup) apply. |
| 24 | +func (b *Bot) SendRichMessage(ctx context.Context, chat ChatID, msg tg.InputRichMessageClass, opts ...SendOption) (*Message, error) { |
| 25 | + var cfg sendConfig |
| 26 | + for _, o := range opts { |
| 27 | + o(&cfg) |
| 28 | + } |
| 29 | + |
| 30 | + peer, err := b.resolveInputPeer(ctx, chat) |
| 31 | + if err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + |
| 35 | + builder := &b.sender.To(peer).Builder |
| 36 | + builder, err = b.applySendConfig(builder, cfg) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + resp, err := builder.RichMessage(ctx, msg) |
| 42 | + return b.sentMessage(ctx, peer, resp, err) |
| 43 | +} |
| 44 | + |
| 45 | +// SendRichHTML sends a rich message whose content is the given HTML document, |
| 46 | +// parsed by Telegram's servers. |
| 47 | +func (b *Bot) SendRichHTML(ctx context.Context, chat ChatID, html string, opts ...SendOption) (*Message, error) { |
| 48 | + return b.SendRichMessage(ctx, chat, rich.HTML(html), opts...) |
| 49 | +} |
| 50 | + |
| 51 | +// SendRichMarkdown sends a rich message whose content is the given Markdown |
| 52 | +// document, parsed by Telegram's servers. |
| 53 | +func (b *Bot) SendRichMarkdown(ctx context.Context, chat ChatID, markdown string, opts ...SendOption) (*Message, error) { |
| 54 | + return b.SendRichMessage(ctx, chat, rich.Markdown(markdown), opts...) |
| 55 | +} |
0 commit comments