|
| 1 | +package botapi |
| 2 | + |
| 3 | +import "github.com/gotd/td/tg" |
| 4 | + |
| 5 | +// KeyboardButtonRequestUsers defines the criteria used to request suitable users |
| 6 | +// when pressed. Maps to a request-peer keyboard button. |
| 7 | +type KeyboardButtonRequestUsers struct { |
| 8 | + // RequestID is an identifier echoed back in the users_shared service message. |
| 9 | + RequestID int |
| 10 | + // UserIsBot, when set, requires the selected users to be bots (true) or |
| 11 | + // regular users (false). |
| 12 | + UserIsBot *bool |
| 13 | + // UserIsPremium, when set, requires the selected users to have Telegram |
| 14 | + // Premium (true) or not (false). |
| 15 | + UserIsPremium *bool |
| 16 | + // MaxQuantity is the maximum number of users to select (1-10, default 1). |
| 17 | + MaxQuantity int |
| 18 | + // RequestName, RequestUsername and RequestPhoto request that the selected |
| 19 | + // users' details be included client-side. They are not enforced over MTProto. |
| 20 | + RequestName bool |
| 21 | + RequestUsername bool |
| 22 | + RequestPhoto bool |
| 23 | +} |
| 24 | + |
| 25 | +// KeyboardButtonRequestChat defines the criteria used to request a suitable chat |
| 26 | +// when pressed. Maps to a request-peer keyboard button. |
| 27 | +type KeyboardButtonRequestChat struct { |
| 28 | + // RequestID is an identifier echoed back in the chat_shared service message. |
| 29 | + RequestID int |
| 30 | + // ChatIsChannel requires a channel (true) or a group/supergroup (false). |
| 31 | + ChatIsChannel bool |
| 32 | + // ChatIsForum, when set, requires a forum (true) or non-forum (false) chat. |
| 33 | + ChatIsForum *bool |
| 34 | + // ChatHasUsername, when set, requires a chat with (true) or without (false) a |
| 35 | + // public username. |
| 36 | + ChatHasUsername *bool |
| 37 | + // ChatIsCreated requires a chat owned by the user. |
| 38 | + ChatIsCreated bool |
| 39 | + // UserAdministratorRights requires the user to hold these rights in the chat. |
| 40 | + UserAdministratorRights *ChatAdminRights |
| 41 | + // BotAdministratorRights requires the bot to hold these rights in the chat. |
| 42 | + BotAdministratorRights *ChatAdminRights |
| 43 | + // BotIsMember requires the bot to be a member of the chat. |
| 44 | + BotIsMember bool |
| 45 | + // RequestTitle, RequestUsername and RequestPhoto request that the chat's |
| 46 | + // details be included client-side. They are not enforced over MTProto. |
| 47 | + RequestTitle bool |
| 48 | + RequestUsername bool |
| 49 | + RequestPhoto bool |
| 50 | +} |
| 51 | + |
| 52 | +// requestUsersToTg builds the MTProto request-peer button for a user request. |
| 53 | +func requestUsersToTg(text string, r *KeyboardButtonRequestUsers) *tg.KeyboardButtonRequestPeer { |
| 54 | + peerType := &tg.RequestPeerTypeUser{} |
| 55 | + if r.UserIsBot != nil { |
| 56 | + peerType.SetBot(*r.UserIsBot) |
| 57 | + } |
| 58 | + |
| 59 | + if r.UserIsPremium != nil { |
| 60 | + peerType.SetPremium(*r.UserIsPremium) |
| 61 | + } |
| 62 | + |
| 63 | + maxQuantity := r.MaxQuantity |
| 64 | + if maxQuantity == 0 { |
| 65 | + maxQuantity = 1 |
| 66 | + } |
| 67 | + |
| 68 | + return &tg.KeyboardButtonRequestPeer{ |
| 69 | + Text: text, |
| 70 | + ButtonID: r.RequestID, |
| 71 | + PeerType: peerType, |
| 72 | + MaxQuantity: maxQuantity, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// requestChatToTg builds the MTProto request-peer button for a chat request. |
| 77 | +func requestChatToTg(text string, r *KeyboardButtonRequestChat) *tg.KeyboardButtonRequestPeer { |
| 78 | + var peerType tg.RequestPeerTypeClass |
| 79 | + |
| 80 | + if r.ChatIsChannel { |
| 81 | + broadcast := &tg.RequestPeerTypeBroadcast{Creator: r.ChatIsCreated} |
| 82 | + if r.UserAdministratorRights != nil { |
| 83 | + broadcast.SetUserAdminRights(r.UserAdministratorRights.toTg()) |
| 84 | + } |
| 85 | + |
| 86 | + if r.BotAdministratorRights != nil { |
| 87 | + broadcast.SetBotAdminRights(r.BotAdministratorRights.toTg()) |
| 88 | + } |
| 89 | + |
| 90 | + if r.ChatHasUsername != nil { |
| 91 | + broadcast.SetHasUsername(*r.ChatHasUsername) |
| 92 | + } |
| 93 | + |
| 94 | + peerType = broadcast |
| 95 | + } else { |
| 96 | + chat := &tg.RequestPeerTypeChat{Creator: r.ChatIsCreated, BotParticipant: r.BotIsMember} |
| 97 | + if r.UserAdministratorRights != nil { |
| 98 | + chat.SetUserAdminRights(r.UserAdministratorRights.toTg()) |
| 99 | + } |
| 100 | + |
| 101 | + if r.BotAdministratorRights != nil { |
| 102 | + chat.SetBotAdminRights(r.BotAdministratorRights.toTg()) |
| 103 | + } |
| 104 | + |
| 105 | + if r.ChatHasUsername != nil { |
| 106 | + chat.SetHasUsername(*r.ChatHasUsername) |
| 107 | + } |
| 108 | + |
| 109 | + if r.ChatIsForum != nil { |
| 110 | + chat.SetForum(*r.ChatIsForum) |
| 111 | + } |
| 112 | + |
| 113 | + peerType = chat |
| 114 | + } |
| 115 | + |
| 116 | + return &tg.KeyboardButtonRequestPeer{ |
| 117 | + Text: text, |
| 118 | + ButtonID: r.RequestID, |
| 119 | + PeerType: peerType, |
| 120 | + } |
| 121 | +} |
0 commit comments