|
| 1 | +package botapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gotd/td/telegram/peers" |
| 7 | + "github.com/gotd/td/tg" |
| 8 | +) |
| 9 | + |
| 10 | +// errUnexpectedInvite is returned when an invite RPC answers with an unexpected |
| 11 | +// constructor. |
| 12 | +func errUnexpectedInvite() *Error { |
| 13 | + return &Error{Code: 500, Description: "Internal Server Error: unexpected invite response"} |
| 14 | +} |
| 15 | + |
| 16 | +// inviteLinkName resolves the optional name from invite-link options, ignoring |
| 17 | +// the other fields that subscription links do not support. |
| 18 | +func inviteLinkName(opts []InviteLinkOption) string { |
| 19 | + var o peers.ExportLinkOptions |
| 20 | + |
| 21 | + for _, opt := range opts { |
| 22 | + opt(&o) |
| 23 | + } |
| 24 | + |
| 25 | + return o.Title |
| 26 | +} |
| 27 | + |
| 28 | +// inviteLinkFromExported converts an MTProto exported invite into the Bot API |
| 29 | +// type. The bot is always the creator of the links it makes. |
| 30 | +func (b *Bot) inviteLinkFromExported(inv *tg.ChatInviteExported) *ChatInviteLink { |
| 31 | + out := &ChatInviteLink{ |
| 32 | + InviteLink: inv.Link, |
| 33 | + Creator: userFromTgUser(b.self), |
| 34 | + CreatesJoinRequest: inv.RequestNeeded, |
| 35 | + IsPrimary: inv.Permanent, |
| 36 | + IsRevoked: inv.Revoked, |
| 37 | + Name: inv.Title, |
| 38 | + ExpireDate: inv.ExpireDate, |
| 39 | + MemberLimit: inv.UsageLimit, |
| 40 | + PendingJoinRequestCount: inv.Requested, |
| 41 | + } |
| 42 | + |
| 43 | + if pricing, ok := inv.GetSubscriptionPricing(); ok { |
| 44 | + out.SubscriptionPeriod = pricing.Period |
| 45 | + out.SubscriptionPrice = int(pricing.Amount) |
| 46 | + } |
| 47 | + |
| 48 | + return out |
| 49 | +} |
| 50 | + |
| 51 | +// CreateChatSubscriptionInviteLink creates a subscription invite link for a |
| 52 | +// channel chat. Users joining through the link pay subscriptionPrice Telegram |
| 53 | +// Stars every subscriptionPeriod seconds (currently only 2592000, 30 days, is |
| 54 | +// supported). The bot must have the can_invite_users administrator right. |
| 55 | +func (b *Bot) CreateChatSubscriptionInviteLink( |
| 56 | + ctx context.Context, chat ChatID, subscriptionPeriod, subscriptionPrice int, opts ...InviteLinkOption, |
| 57 | +) (*ChatInviteLink, error) { |
| 58 | + peer, err := b.resolveInputPeer(ctx, chat) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + req := &tg.MessagesExportChatInviteRequest{Peer: peer} |
| 64 | + req.SetSubscriptionPricing(tg.StarsSubscriptionPricing{ |
| 65 | + Period: subscriptionPeriod, |
| 66 | + Amount: int64(subscriptionPrice), |
| 67 | + }) |
| 68 | + |
| 69 | + if name := inviteLinkName(opts); name != "" { |
| 70 | + req.SetTitle(name) |
| 71 | + } |
| 72 | + |
| 73 | + res, err := b.raw.MessagesExportChatInvite(ctx, req) |
| 74 | + if err != nil { |
| 75 | + return nil, asAPIError(err) |
| 76 | + } |
| 77 | + |
| 78 | + exported, ok := res.(*tg.ChatInviteExported) |
| 79 | + if !ok { |
| 80 | + return nil, errUnexpectedInvite() |
| 81 | + } |
| 82 | + |
| 83 | + return b.inviteLinkFromExported(exported), nil |
| 84 | +} |
| 85 | + |
| 86 | +// EditChatSubscriptionInviteLink edits the name of a subscription invite link |
| 87 | +// created by the bot. The price and period of a subscription link cannot be |
| 88 | +// changed. |
| 89 | +func (b *Bot) EditChatSubscriptionInviteLink( |
| 90 | + ctx context.Context, chat ChatID, inviteLink string, opts ...InviteLinkOption, |
| 91 | +) (*ChatInviteLink, error) { |
| 92 | + peer, err := b.resolveInputPeer(ctx, chat) |
| 93 | + if err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + req := &tg.MessagesEditExportedChatInviteRequest{Peer: peer, Link: inviteLink} |
| 98 | + req.SetTitle(inviteLinkName(opts)) |
| 99 | + |
| 100 | + res, err := b.raw.MessagesEditExportedChatInvite(ctx, req) |
| 101 | + if err != nil { |
| 102 | + return nil, asAPIError(err) |
| 103 | + } |
| 104 | + |
| 105 | + edited, ok := res.(*tg.MessagesExportedChatInvite) |
| 106 | + if !ok { |
| 107 | + return nil, errUnexpectedInvite() |
| 108 | + } |
| 109 | + |
| 110 | + exported, ok := edited.Invite.(*tg.ChatInviteExported) |
| 111 | + if !ok { |
| 112 | + return nil, errUnexpectedInvite() |
| 113 | + } |
| 114 | + |
| 115 | + return b.inviteLinkFromExported(exported), nil |
| 116 | +} |
0 commit comments