|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/td/tg" |
| 7 | +) |
| 8 | + |
| 9 | +// PreparedInlineMessage describes an inline message to be sent by a user of a Mini |
| 10 | +// App. It is the result of SavePreparedInlineMessage. |
| 11 | +type PreparedInlineMessage struct { |
| 12 | + // ID is the unique identifier of the prepared message. |
| 13 | + ID string `json:"id"` |
| 14 | + // ExpirationDate is the Unix time when the prepared message can no longer be |
| 15 | + // used. |
| 16 | + ExpirationDate int `json:"expiration_date"` |
| 17 | +} |
| 18 | + |
| 19 | +// PreparedInlineMessageOption configures the set of chat types a prepared inline |
| 20 | +// message may be sent to. |
| 21 | +type PreparedInlineMessageOption func(*preparedInlineConfig) |
| 22 | + |
| 23 | +type preparedInlineConfig struct { |
| 24 | + allowUsers bool |
| 25 | + allowBots bool |
| 26 | + allowGroups bool |
| 27 | + allowChannels bool |
| 28 | +} |
| 29 | + |
| 30 | +// WithAllowUserChats permits the message to be sent to private chats with users. |
| 31 | +func WithAllowUserChats() PreparedInlineMessageOption { |
| 32 | + return func(c *preparedInlineConfig) { c.allowUsers = true } |
| 33 | +} |
| 34 | + |
| 35 | +// WithAllowBotChats permits the message to be sent to private chats with bots. |
| 36 | +func WithAllowBotChats() PreparedInlineMessageOption { |
| 37 | + return func(c *preparedInlineConfig) { c.allowBots = true } |
| 38 | +} |
| 39 | + |
| 40 | +// WithAllowGroupChats permits the message to be sent to group and supergroup chats. |
| 41 | +func WithAllowGroupChats() PreparedInlineMessageOption { |
| 42 | + return func(c *preparedInlineConfig) { c.allowGroups = true } |
| 43 | +} |
| 44 | + |
| 45 | +// WithAllowChannelChats permits the message to be sent to channel chats. |
| 46 | +func WithAllowChannelChats() PreparedInlineMessageOption { |
| 47 | + return func(c *preparedInlineConfig) { c.allowChannels = true } |
| 48 | +} |
| 49 | + |
| 50 | +// peerTypes turns the allow-* flags into MTProto inline query peer types. |
| 51 | +func (c preparedInlineConfig) peerTypes() []tg.InlineQueryPeerTypeClass { |
| 52 | + var out []tg.InlineQueryPeerTypeClass |
| 53 | + |
| 54 | + if c.allowUsers { |
| 55 | + out = append(out, &tg.InlineQueryPeerTypePM{}) |
| 56 | + } |
| 57 | + |
| 58 | + if c.allowBots { |
| 59 | + out = append(out, &tg.InlineQueryPeerTypeBotPM{}) |
| 60 | + } |
| 61 | + |
| 62 | + if c.allowGroups { |
| 63 | + out = append(out, &tg.InlineQueryPeerTypeChat{}, &tg.InlineQueryPeerTypeMegagroup{}) |
| 64 | + } |
| 65 | + |
| 66 | + if c.allowChannels { |
| 67 | + out = append(out, &tg.InlineQueryPeerTypeBroadcast{}) |
| 68 | + } |
| 69 | + |
| 70 | + return out |
| 71 | +} |
| 72 | + |
| 73 | +// SavePreparedInlineMessage stores a message that can later be sent by a user of a |
| 74 | +// Mini App. By default the message may not be sent anywhere; use the WithAllow* |
| 75 | +// options to permit specific chat types. |
| 76 | +func (b *Bot) SavePreparedInlineMessage( |
| 77 | + ctx context.Context, userID int64, result InlineQueryResult, opts ...PreparedInlineMessageOption, |
| 78 | +) (*PreparedInlineMessage, error) { |
| 79 | + if result == nil { |
| 80 | + return nil, errNilInlineResult() |
| 81 | + } |
| 82 | + |
| 83 | + var cfg preparedInlineConfig |
| 84 | + |
| 85 | + for _, o := range opts { |
| 86 | + o(&cfg) |
| 87 | + } |
| 88 | + |
| 89 | + user, err := b.resolveInputUser(ctx, userID) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + converted, err := result.toTg(ctx, b) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + res, err := b.raw.MessagesSavePreparedInlineMessage(ctx, &tg.MessagesSavePreparedInlineMessageRequest{ |
| 100 | + Result: converted, |
| 101 | + UserID: user, |
| 102 | + PeerTypes: cfg.peerTypes(), |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + return nil, asAPIError(err) |
| 106 | + } |
| 107 | + |
| 108 | + return &PreparedInlineMessage{ID: res.ID, ExpirationDate: res.ExpireDate}, nil |
| 109 | +} |
0 commit comments