-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot_profile.go
More file actions
282 lines (229 loc) · 8.3 KB
/
Copy pathbot_profile.go
File metadata and controls
282 lines (229 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package botapi
import (
"context"
"github.com/gotd/td/tg"
)
// BotInfoOption configures a bot-profile call (currently the language code the
// value applies to). An empty language code targets the default locale.
type BotInfoOption func(*botInfoConfig)
type botInfoConfig struct {
langCode string
}
// WithBotInfoLanguage restricts the call to a two-letter IETF language code. The
// empty code (default) sets the value shown to users with no localized value.
func WithBotInfoLanguage(code string) BotInfoOption {
return func(c *botInfoConfig) { c.langCode = code }
}
func newBotInfoConfig(opts []BotInfoOption) botInfoConfig {
var cfg botInfoConfig
for _, o := range opts {
o(&cfg)
}
return cfg
}
// setBotInfo updates a single bot-info field. The MTProto bots.setBotInfo edits
// the bot identified by InputUserSelf; only the populated flag fields change.
func (b *Bot) setBotInfo(ctx context.Context, langCode string, mut func(*tg.BotsSetBotInfoRequest)) error {
req := &tg.BotsSetBotInfoRequest{
Bot: &tg.InputUserSelf{},
LangCode: langCode,
}
mut(req)
if _, err := b.raw.BotsSetBotInfo(ctx, req); err != nil {
return asAPIError(err)
}
return nil
}
// SetMyName changes the bot's name for the given language. An empty name clears
// the localized value, falling back to the default.
func (b *Bot) SetMyName(ctx context.Context, name string, opts ...BotInfoOption) error {
cfg := newBotInfoConfig(opts)
return b.setBotInfo(ctx, cfg.langCode, func(r *tg.BotsSetBotInfoRequest) { r.SetName(name) })
}
// SetMyDescription changes the bot's description, shown in an empty chat with the
// bot, for the given language.
func (b *Bot) SetMyDescription(ctx context.Context, description string, opts ...BotInfoOption) error {
cfg := newBotInfoConfig(opts)
return b.setBotInfo(ctx, cfg.langCode, func(r *tg.BotsSetBotInfoRequest) { r.SetDescription(description) })
}
// SetMyShortDescription changes the bot's short description, shown on the bot's
// profile page and in the chat list, for the given language.
func (b *Bot) SetMyShortDescription(ctx context.Context, shortDescription string, opts ...BotInfoOption) error {
cfg := newBotInfoConfig(opts)
return b.setBotInfo(ctx, cfg.langCode, func(r *tg.BotsSetBotInfoRequest) { r.SetAbout(shortDescription) })
}
// getBotInfo fetches the bot's localized info via bots.getBotInfo.
func (b *Bot) getBotInfo(ctx context.Context, langCode string) (*tg.BotsBotInfo, error) {
info, err := b.raw.BotsGetBotInfo(ctx, &tg.BotsGetBotInfoRequest{
Bot: &tg.InputUserSelf{},
LangCode: langCode,
})
if err != nil {
return nil, asAPIError(err)
}
return info, nil
}
// GetMyName returns the bot's current name for the given language.
func (b *Bot) GetMyName(ctx context.Context, opts ...BotInfoOption) (string, error) {
cfg := newBotInfoConfig(opts)
info, err := b.getBotInfo(ctx, cfg.langCode)
if err != nil {
return "", err
}
return info.Name, nil
}
// GetMyDescription returns the bot's current description for the given language.
func (b *Bot) GetMyDescription(ctx context.Context, opts ...BotInfoOption) (string, error) {
cfg := newBotInfoConfig(opts)
info, err := b.getBotInfo(ctx, cfg.langCode)
if err != nil {
return "", err
}
return info.Description, nil
}
// GetMyShortDescription returns the bot's current short description for the given
// language.
func (b *Bot) GetMyShortDescription(ctx context.Context, opts ...BotInfoOption) (string, error) {
cfg := newBotInfoConfig(opts)
info, err := b.getBotInfo(ctx, cfg.langCode)
if err != nil {
return "", err
}
return info.About, nil
}
// ChatMenuButtonOption configures a chat-menu-button call.
type ChatMenuButtonOption func(*menuButtonConfig)
type menuButtonConfig struct {
userID int64
hasUserID bool
}
// WithMenuButtonChat targets the menu button of a single private chat with the
// given user instead of the bot-wide default.
func WithMenuButtonChat(userID int64) ChatMenuButtonOption {
return func(c *menuButtonConfig) {
c.userID = userID
c.hasUserID = true
}
}
// menuButtonUser resolves the menu-button target. The bot-wide default uses
// InputUserEmpty; a per-chat button resolves the user.
func (b *Bot) menuButtonUser(ctx context.Context, cfg menuButtonConfig) (tg.InputUserClass, error) {
if !cfg.hasUserID {
return &tg.InputUserEmpty{}, nil
}
return b.resolveInputUser(ctx, cfg.userID)
}
// menuButtonToTg converts a Bot API menu button to the MTProto representation.
// A nil button is treated as the default.
//
// The switch over the sealed MenuButton union is exhaustive (gochecksumtype).
func menuButtonToTg(button MenuButton) tg.BotMenuButtonClass {
switch v := button.(type) {
case MenuButtonCommands:
return &tg.BotMenuButtonCommands{}
case MenuButtonWebApp:
return &tg.BotMenuButton{Text: v.Text, URL: v.WebApp.URL}
case MenuButtonDefault:
return &tg.BotMenuButtonDefault{}
default:
return &tg.BotMenuButtonDefault{}
}
}
// menuButtonFromTg converts an MTProto menu button to the Bot API union.
func menuButtonFromTg(button tg.BotMenuButtonClass) MenuButton {
switch v := button.(type) {
case *tg.BotMenuButtonCommands:
return MenuButtonCommands{Type: MenuButtonCommandsType}
case *tg.BotMenuButton:
return MenuButtonWebApp{
Type: MenuButtonWebAppType,
Text: v.Text,
WebApp: WebAppInfo{URL: v.URL},
}
default:
return MenuButtonDefault{Type: MenuButtonDefaultType}
}
}
// SetChatMenuButton changes the bot's menu button in a private chat, or the
// default menu button when no chat is targeted.
func (b *Bot) SetChatMenuButton(ctx context.Context, button MenuButton, opts ...ChatMenuButtonOption) error {
var cfg menuButtonConfig
for _, o := range opts {
o(&cfg)
}
user, err := b.menuButtonUser(ctx, cfg)
if err != nil {
return err
}
if _, err := b.raw.BotsSetBotMenuButton(ctx, &tg.BotsSetBotMenuButtonRequest{
UserID: user,
Button: menuButtonToTg(button),
}); err != nil {
return asAPIError(err)
}
return nil
}
// GetChatMenuButton returns the current menu button of a private chat, or the
// default menu button when no chat is targeted.
func (b *Bot) GetChatMenuButton(ctx context.Context, opts ...ChatMenuButtonOption) (MenuButton, error) {
var cfg menuButtonConfig
for _, o := range opts {
o(&cfg)
}
user, err := b.menuButtonUser(ctx, cfg)
if err != nil {
return nil, err
}
button, err := b.raw.BotsGetBotMenuButton(ctx, user)
if err != nil {
return nil, asAPIError(err)
}
return menuButtonFromTg(button), nil
}
// SetMyDefaultAdministratorRights changes the default administrator rights
// requested by the bot when it is added to a chat. forChannels selects the
// rights used for channels; otherwise the rights for groups and supergroups are
// changed. Pass a zero ChatAdminRights to clear the defaults.
func (b *Bot) SetMyDefaultAdministratorRights(ctx context.Context, rights ChatAdminRights, forChannels bool) error {
if forChannels {
if _, err := b.raw.BotsSetBotBroadcastDefaultAdminRights(ctx, rights.toTg()); err != nil {
return asAPIError(err)
}
return nil
}
if _, err := b.raw.BotsSetBotGroupDefaultAdminRights(ctx, rights.toTg()); err != nil {
return asAPIError(err)
}
return nil
}
// GetMyDefaultAdministratorRights returns the bot's current default
// administrator rights. forChannels selects the channel rights; otherwise the
// group rights are returned.
func (b *Bot) GetMyDefaultAdministratorRights(ctx context.Context, forChannels bool) (ChatAdminRights, error) {
full, err := b.raw.UsersGetFullUser(ctx, &tg.InputUserSelf{})
if err != nil {
return ChatAdminRights{}, asAPIError(err)
}
if forChannels {
return chatAdminRightsFromTg(full.FullUser.BotBroadcastAdminRights), nil
}
return chatAdminRightsFromTg(full.FullUser.BotGroupAdminRights), nil
}
// chatAdminRightsFromTg converts MTProto admin rights to the Bot API
// representation. It is the inverse of ChatAdminRights.toTg.
func chatAdminRightsFromTg(r tg.ChatAdminRights) ChatAdminRights {
return ChatAdminRights{
IsAnonymous: r.Anonymous,
CanManageChat: r.Other,
CanDeleteMessages: r.DeleteMessages,
CanManageVideoChats: r.ManageCall,
CanRestrictMembers: r.BanUsers,
CanPromoteMembers: r.AddAdmins,
CanChangeInfo: r.ChangeInfo,
CanInviteUsers: r.InviteUsers,
CanPostMessages: r.PostMessages,
CanEditMessages: r.EditMessages,
CanPinMessages: r.PinMessages,
CanManageTopics: r.ManageTopics,
}
}