|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/td/tg" |
| 7 | +) |
| 8 | + |
| 9 | +// VerificationOption configures a verify call. |
| 10 | +type VerificationOption func(*verificationConfig) |
| 11 | + |
| 12 | +type verificationConfig struct { |
| 13 | + description string |
| 14 | +} |
| 15 | + |
| 16 | +// WithVerificationDescription sets a custom description for the verification |
| 17 | +// shown instead of the bot's default verification description. |
| 18 | +func WithVerificationDescription(text string) VerificationOption { |
| 19 | + return func(c *verificationConfig) { c.description = text } |
| 20 | +} |
| 21 | + |
| 22 | +// setCustomVerification enables or disables the bot's custom verification of a |
| 23 | +// peer. |
| 24 | +func (b *Bot) setCustomVerification(ctx context.Context, peer tg.InputPeerClass, enabled bool, opts []VerificationOption) error { |
| 25 | + var cfg verificationConfig |
| 26 | + |
| 27 | + for _, o := range opts { |
| 28 | + o(&cfg) |
| 29 | + } |
| 30 | + |
| 31 | + req := &tg.BotsSetCustomVerificationRequest{Peer: peer} |
| 32 | + req.SetEnabled(enabled) |
| 33 | + |
| 34 | + if enabled && cfg.description != "" { |
| 35 | + req.SetCustomDescription(cfg.description) |
| 36 | + } |
| 37 | + |
| 38 | + if _, err := b.raw.BotsSetCustomVerification(ctx, req); err != nil { |
| 39 | + return asAPIError(err) |
| 40 | + } |
| 41 | + |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +// VerifyUser verifies a user on behalf of the organization represented by the |
| 46 | +// bot. |
| 47 | +func (b *Bot) VerifyUser(ctx context.Context, userID int64, opts ...VerificationOption) error { |
| 48 | + user, err := b.resolveInputUser(ctx, userID) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + return b.setCustomVerification(ctx, userToInputPeer(user), true, opts) |
| 54 | +} |
| 55 | + |
| 56 | +// VerifyChat verifies a chat on behalf of the organization represented by the |
| 57 | +// bot. |
| 58 | +func (b *Bot) VerifyChat(ctx context.Context, chat ChatID, opts ...VerificationOption) error { |
| 59 | + peer, err := b.resolveInputPeer(ctx, chat) |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + |
| 64 | + return b.setCustomVerification(ctx, peer, true, opts) |
| 65 | +} |
| 66 | + |
| 67 | +// RemoveUserVerification removes the verification from a user that is verified on |
| 68 | +// behalf of the organization represented by the bot. |
| 69 | +func (b *Bot) RemoveUserVerification(ctx context.Context, userID int64) error { |
| 70 | + user, err := b.resolveInputUser(ctx, userID) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + return b.setCustomVerification(ctx, userToInputPeer(user), false, nil) |
| 76 | +} |
| 77 | + |
| 78 | +// RemoveChatVerification removes the verification from a chat that is verified on |
| 79 | +// behalf of the organization represented by the bot. |
| 80 | +func (b *Bot) RemoveChatVerification(ctx context.Context, chat ChatID) error { |
| 81 | + peer, err := b.resolveInputPeer(ctx, chat) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + return b.setCustomVerification(ctx, peer, false, nil) |
| 87 | +} |
0 commit comments