|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/td/telegram/message" |
| 7 | + "github.com/gotd/td/telegram/message/entity" |
| 8 | + "github.com/gotd/td/telegram/message/html" |
| 9 | + "github.com/gotd/td/telegram/message/markdown" |
| 10 | + "github.com/gotd/td/telegram/message/styling" |
| 11 | + "github.com/gotd/td/telegram/message/unpack" |
| 12 | + "github.com/gotd/td/tg" |
| 13 | +) |
| 14 | + |
| 15 | +// SendOption configures an outgoing send. Options are shared across the send |
| 16 | +// methods; pass any combination. |
| 17 | +type SendOption func(*sendConfig) |
| 18 | + |
| 19 | +type sendConfig struct { |
| 20 | + disableWebPreview bool |
| 21 | + silent bool |
| 22 | + protect bool |
| 23 | + replyTo int |
| 24 | + markup ReplyMarkup |
| 25 | + parseMode ParseMode |
| 26 | +} |
| 27 | + |
| 28 | +// DisableWebPagePreview disables the link preview for messages with links. |
| 29 | +func DisableWebPagePreview() SendOption { return func(c *sendConfig) { c.disableWebPreview = true } } |
| 30 | + |
| 31 | +// Silent sends the message without a notification sound. |
| 32 | +func Silent() SendOption { return func(c *sendConfig) { c.silent = true } } |
| 33 | + |
| 34 | +// ProtectContent prevents the message content from being forwarded or saved. |
| 35 | +func ProtectContent() SendOption { return func(c *sendConfig) { c.protect = true } } |
| 36 | + |
| 37 | +// ReplyTo makes the message a reply to the message with the given id. |
| 38 | +func ReplyTo(messageID int) SendOption { return func(c *sendConfig) { c.replyTo = messageID } } |
| 39 | + |
| 40 | +// WithReplyMarkup attaches an inline/reply keyboard (or removes one). |
| 41 | +func WithReplyMarkup(m ReplyMarkup) SendOption { return func(c *sendConfig) { c.markup = m } } |
| 42 | + |
| 43 | +// WithParseMode selects the formatting mode for the text or caption. |
| 44 | +func WithParseMode(m ParseMode) SendOption { return func(c *sendConfig) { c.parseMode = m } } |
| 45 | + |
| 46 | +// styledText turns text + parse mode into styling options. |
| 47 | +// |
| 48 | +// The switch over ParseMode is exhaustive (exhaustive lint). |
| 49 | +func styledText(text string, mode ParseMode, resolver entity.UserResolver) ([]styling.StyledTextOption, error) { |
| 50 | + switch mode { |
| 51 | + case ParseModeNone: |
| 52 | + return []styling.StyledTextOption{styling.Plain(text)}, nil |
| 53 | + case ParseModeHTML: |
| 54 | + return []styling.StyledTextOption{html.String(resolver, text)}, nil |
| 55 | + case ParseModeMarkdownV2, ParseModeMarkdown: |
| 56 | + return []styling.StyledTextOption{markdown.String(resolver, text)}, nil |
| 57 | + default: |
| 58 | + return nil, &Error{Code: 400, Description: "Bad Request: unsupported parse mode"} |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// applySendConfig applies the common builder options to a message builder. |
| 63 | +func (b *Bot) applySendConfig(builder *message.Builder, cfg sendConfig) (*message.Builder, error) { |
| 64 | + if cfg.disableWebPreview { |
| 65 | + builder = builder.NoWebpage() |
| 66 | + } |
| 67 | + if cfg.silent { |
| 68 | + builder = builder.Silent() |
| 69 | + } |
| 70 | + if cfg.protect { |
| 71 | + builder = builder.NoForwards() |
| 72 | + } |
| 73 | + if cfg.replyTo != 0 { |
| 74 | + builder = builder.Reply(cfg.replyTo) |
| 75 | + } |
| 76 | + if cfg.markup != nil { |
| 77 | + mkp, err := replyMarkupToTg(cfg.markup) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + builder = builder.Markup(mkp) |
| 82 | + } |
| 83 | + return builder, nil |
| 84 | +} |
| 85 | + |
| 86 | +// sentMessage unpacks a send response into a Bot API Message, backfilling the |
| 87 | +// peer id when the server omitted it. |
| 88 | +func (b *Bot) sentMessage(ctx context.Context, peer tg.InputPeerClass, resp tg.UpdatesClass, sendErr error) (*Message, error) { |
| 89 | + m, err := unpack.MessageClass(resp, sendErr) |
| 90 | + if err != nil { |
| 91 | + return nil, asAPIError(err) |
| 92 | + } |
| 93 | + msg, ok := m.(*tg.Message) |
| 94 | + if !ok { |
| 95 | + return &Message{}, nil |
| 96 | + } |
| 97 | + if msg.PeerID == nil { |
| 98 | + switch p := peer.(type) { |
| 99 | + case *tg.InputPeerChat: |
| 100 | + msg.PeerID = &tg.PeerChat{ChatID: p.ChatID} |
| 101 | + case *tg.InputPeerUser: |
| 102 | + msg.PeerID = &tg.PeerUser{UserID: p.UserID} |
| 103 | + case *tg.InputPeerChannel: |
| 104 | + msg.PeerID = &tg.PeerChannel{ChannelID: p.ChannelID} |
| 105 | + } |
| 106 | + } |
| 107 | + return b.convertMessage(ctx, msg) |
| 108 | +} |
| 109 | + |
| 110 | +// SendMessage sends a text message to a chat and returns the sent message. |
| 111 | +func (b *Bot) SendMessage(ctx context.Context, chat ChatID, text string, opts ...SendOption) (*Message, error) { |
| 112 | + var cfg sendConfig |
| 113 | + for _, o := range opts { |
| 114 | + o(&cfg) |
| 115 | + } |
| 116 | + |
| 117 | + peer, err := b.resolveInputPeer(ctx, chat) |
| 118 | + if err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + styled, err := styledText(text, cfg.parseMode, b.peers.UserResolveHook(ctx)) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + builder := &b.sender.To(peer).Builder |
| 128 | + builder, err = b.applySendConfig(builder, cfg) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + resp, err := builder.StyledText(ctx, styled...) |
| 134 | + return b.sentMessage(ctx, peer, resp, err) |
| 135 | +} |
0 commit comments