-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat.go
472 lines (383 loc) · 13.8 KB
/
chat.go
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package telebot
import (
"strconv"
"time"
)
// User object represents a Telegram user, bot.
type User struct {
ID int64 `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
LanguageCode string `json:"language_code"`
IsBot bool `json:"is_bot"`
IsPremium bool `json:"is_premium"`
AddedToMenu bool `json:"added_to_attachment_menu"`
Usernames []string `json:"active_usernames"`
CustomEmojiStatusID string `json:"emoji_status_custom_emoji_id"`
// Returns only in getMe
CanJoinGroups bool `json:"can_join_groups"`
CanReadMessages bool `json:"can_read_all_group_messages"`
SupportsInline bool `json:"supports_inline_queries"`
}
// Recipient returns user ID (see Recipient interface).
func (u *User) Recipient() string {
return strconv.FormatInt(u.ID, 10)
}
// Chat object represents a Telegram user, bot, group or a channel.
type Chat struct {
ID int64 `json:"id"`
// See ChatType and consts.
Type ChatType `json:"type"`
// Won't be there for ChatPrivate.
Title string `json:"title"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Username string `json:"username"`
ActiveUsernames []string `json:"active_usernames"`
// Returns only in getChat
Bio string `json:"bio,omitempty"`
Photo *ChatPhoto `json:"photo,omitempty"`
Description string `json:"description,omitempty"`
InviteLink string `json:"invite_link,omitempty"`
EmojiStatusId string `json:"emoji_status_custom_emoji_id,omitempty"`
EmojiStatusExpirationDate int64 `json:"emoji_status_expiration_date,omitempty"`
PinnedMessage *Message `json:"pinned_message,omitempty"`
Permissions *Rights `json:"permissions,omitempty"`
Topic *Topic `json:"forum_topic,omitempty"`
SlowMode int `json:"slow_mode_delay,omitempty"`
StickerSet string `json:"sticker_set_name,omitempty"`
CanSetStickerSet bool `json:"can_set_sticker_set,omitempty"`
LinkedChatID int64 `json:"linked_chat_id,omitempty"`
ChatLocation *ChatLocation `json:"location,omitempty"`
Private bool `json:"has_private_forwards,omitempty"`
IsForum bool `json:"is_forum,omitempty"`
Protected bool `json:"has_protected_content,omitempty"`
NoVoiceAndVideo bool `json:"has_restricted_voice_and_video_messages"`
HiddenMembers bool `json:"has_hidden_members,omitempty"`
AggressiveAntiSpam bool `json:"has_aggressive_anti_spam_enabled,omitempty"`
}
// Recipient returns chat ID (see Recipient interface).
func (c *Chat) Recipient() string {
return strconv.FormatInt(c.ID, 10)
}
// ChatType represents one of the possible chat types.
type ChatType string
const (
ChatPrivate ChatType = "private"
ChatGroup ChatType = "group"
ChatSuperGroup ChatType = "supergroup"
ChatChannel ChatType = "channel"
ChatChannelPrivate ChatType = "privatechannel"
)
// ChatLocation represents a location to which a chat is connected.
type ChatLocation struct {
Location Location `json:"location,omitempty"`
Address string `json:"address,omitempty"`
}
// ChatPhoto object represents a chat photo.
type ChatPhoto struct {
// File identifiers of small (160x160) chat photo
SmallFileID string `json:"small_file_id"`
SmallUniqueID string `json:"small_file_unique_id"`
// File identifiers of big (640x640) chat photo
BigFileID string `json:"big_file_id"`
BigUniqueID string `json:"big_file_unique_id"`
}
// ChatMember object represents information about a single chat member.
type ChatMember struct {
Rights
User *User `json:"user"`
Role MemberStatus `json:"status"`
Title string `json:"custom_title"`
Anonymous bool `json:"is_anonymous"`
// Date when restrictions will be lifted for the user, unix time.
//
// If user is restricted for more than 366 days or less than
// 30 seconds from the current time, they are considered to be
// restricted forever.
//
// Use tele.Forever().
//
RestrictedUntil int64 `json:"until_date,omitempty"`
JoinToSend string `json:"join_to_send_messages"`
JoinByRequest string `json:"join_by_request"`
}
// MemberStatus is one's chat status.
type MemberStatus string
const (
Creator MemberStatus = "creator"
Administrator MemberStatus = "administrator"
Member MemberStatus = "member"
Restricted MemberStatus = "restricted"
Left MemberStatus = "left"
Kicked MemberStatus = "kicked"
)
// ChatMemberUpdate object represents changes in the status of a chat member.
type ChatMemberUpdate struct {
// Chat where the user belongs to.
Chat *Chat `json:"chat"`
// Sender which user the action was triggered.
Sender *User `json:"from"`
// Unixtime, use Date() to get time.Time.
Unixtime int64 `json:"date"`
// Previous information about the chat member.
OldChatMember *ChatMember `json:"old_chat_member"`
// New information about the chat member.
NewChatMember *ChatMember `json:"new_chat_member"`
// (Optional) InviteLink which was used by the user to
// join the chat; for joining by invite link events only.
InviteLink *ChatInviteLink `json:"invite_link"`
}
// Time returns the moment of the change in local time.
func (c *ChatMemberUpdate) Time() time.Time {
return time.Unix(c.Unixtime, 0)
}
// ChatID represents a chat or an user integer ID, which can be used
// as recipient in bot methods. It is very useful in cases where
// you have special group IDs, for example in your config, and don't
// want to wrap it into *tele.Chat every time you send messages.
//
// Example:
//
// group := tele.ChatID(-100756389456)
// b.Send(group, "Hello!")
//
// type Config struct {
// AdminGroup tele.ChatID `json:"admin_group"`
// }
// b.Send(conf.AdminGroup, "Hello!")
type ChatID int64
// Recipient returns chat ID (see Recipient interface).
func (i ChatID) Recipient() string {
return strconv.FormatInt(int64(i), 10)
}
// ChatJoinRequest represents a join request sent to a chat.
type ChatJoinRequest struct {
// Chat to which the request was sent.
Chat *Chat `json:"chat"`
// Sender is the user that sent the join request.
Sender *User `json:"from"`
// Identifier of a private chat with the user who sent the join request. This number may have more than 32 significant bits and some programming languages may have difficulty/silent defects in interpreting it. But it has at most 52 significant bits, so a 64-bit integer or double-precision float type are safe for storing this identifier. The bot can use this identifier for 24 hours to send messages until the join request is processed, assuming no other administrator contacted the user.
UserID int64 `json:"user_chat_id"`
// Unixtime, use ChatJoinRequest.Time() to get time.Time.
Unixtime int64 `json:"date"`
// Bio of the user, optional.
Bio string `json:"bio"`
// InviteLink is the chat invite link that was used by
// the user to send the join request, optional.
InviteLink *ChatInviteLink `json:"invite_link"`
}
// ChatInviteLink object represents an invite for a chat.
type ChatInviteLink struct {
// The invite link.
InviteLink string `json:"invite_link"`
// Invite link name.
Name string `json:"name"`
// The creator of the link.
Creator *User `json:"creator"`
// If the link is primary.
IsPrimary bool `json:"is_primary"`
// If the link is revoked.
IsRevoked bool `json:"is_revoked"`
// (Optional) Point in time when the link will expire,
// use ExpireDate() to get time.Time.
ExpireUnixtime int64 `json:"expire_date,omitempty"`
// (Optional) Maximum number of users that can be members of
// the chat simultaneously.
MemberLimit int `json:"member_limit,omitempty"`
// (Optional) True, if users joining the chat via the link need to
// be approved by chat administrators. If True, member_limit can't be specified.
JoinRequest bool `json:"creates_join_request"`
// (Optional) Number of pending join requests created using this link.
PendingCount int `json:"pending_join_request_count"`
}
// ExpireDate returns the moment of the link expiration in local time.
func (c *ChatInviteLink) ExpireDate() time.Time {
return time.Unix(c.ExpireUnixtime, 0)
}
// Time returns the moment of chat join request sending in local time.
func (r ChatJoinRequest) Time() time.Time {
return time.Unix(r.Unixtime, 0)
}
// InviteLink should be used to export chat's invite link.
func (b *Bot) InviteLink(chat *Chat) (string, error) {
params := map[string]string{
"chat_id": chat.Recipient(),
}
data, err := b.Raw("exportChatInviteLink", params)
if err != nil {
return "", err
}
defer ReleaseBuffer(data)
var resp Response[string]
if err := b.json.NewDecoder(data).Decode(&resp); err != nil {
return "", wrapError(err)
}
return resp.Result, nil
}
// CreateInviteLink creates an additional invite link for a chat.
func (b *Bot) CreateInviteLink(chat Recipient, link *ChatInviteLink) (*ChatInviteLink, error) {
params := map[string]any{
"chat_id": chat.Recipient(),
}
if link != nil {
params["name"] = link.Name
if link.ExpireUnixtime != 0 {
params["expire_date"] = link.ExpireUnixtime
}
if link.MemberLimit > 0 {
params["member_limit"] = link.MemberLimit
} else if link.JoinRequest {
params["creates_join_request"] = true
}
}
data, err := b.Raw("createChatInviteLink", params)
if err != nil {
return nil, err
}
defer ReleaseBuffer(data)
var resp Response[ChatInviteLink]
if err := b.json.NewDecoder(data).Decode(&resp); err != nil {
return nil, wrapError(err)
}
return &resp.Result, nil
}
// EditInviteLink edits a non-primary invite link created by the bot.
func (b *Bot) EditInviteLink(chat Recipient, link *ChatInviteLink) (*ChatInviteLink, error) {
params := map[string]any{
"chat_id": chat.Recipient(),
}
if link != nil {
params["invite_link"] = link.InviteLink
params["name"] = link.Name
if link.ExpireUnixtime != 0 {
params["expire_date"] = link.ExpireUnixtime
}
if link.MemberLimit > 0 {
params["member_limit"] = link.MemberLimit
} else if link.JoinRequest {
params["creates_join_request"] = true
}
}
data, err := b.Raw("editChatInviteLink", params)
if err != nil {
return nil, err
}
defer ReleaseBuffer(data)
var resp Response[ChatInviteLink]
if err := b.json.NewDecoder(data).Decode(&resp); err != nil {
return nil, wrapError(err)
}
return &resp.Result, nil
}
// RevokeInviteLink revokes an invite link created by the bot.
func (b *Bot) RevokeInviteLink(chat Recipient, link string) (*ChatInviteLink, error) {
params := map[string]string{
"chat_id": chat.Recipient(),
"invite_link": link,
}
data, err := b.Raw("revokeChatInviteLink", params)
if err != nil {
return nil, err
}
defer ReleaseBuffer(data)
var resp Response[ChatInviteLink]
if err := b.json.NewDecoder(data).Decode(&resp); err != nil {
return nil, wrapError(err)
}
return &resp.Result, nil
}
// ApproveJoinRequest approves a chat join request.
func (b *Bot) ApproveJoinRequest(chat Recipient, user *User) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": user.Recipient(),
}
data, err := b.Raw("approveChatJoinRequest", params)
if err != nil {
return err
}
defer ReleaseBuffer(data)
return extractOk(data)
}
// DeclineJoinRequest declines a chat join request.
func (b *Bot) DeclineJoinRequest(chat Recipient, user *User) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"user_id": user.Recipient(),
}
data, err := b.Raw("declineChatJoinRequest", params)
if err != nil {
return err
}
defer ReleaseBuffer(data)
return extractOk(data)
}
// SetGroupTitle should be used to update group title.
func (b *Bot) SetGroupTitle(chat *Chat, title string) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"title": title,
}
r, err := b.Raw("setChatTitle", params)
ReleaseBuffer(r)
return err
}
// SetGroupDescription should be used to update group description.
func (b *Bot) SetGroupDescription(chat *Chat, description string) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"description": description,
}
r, err := b.Raw("setChatDescription", params)
ReleaseBuffer(r)
return err
}
// SetGroupPhoto should be used to update group photo.
func (b *Bot) SetGroupPhoto(chat *Chat, p *Photo) error {
params := map[string]any{
"chat_id": chat.Recipient(),
}
r, err := b.sendFiles("setChatPhoto", map[string]File{"photo": p.File}, params)
ReleaseBuffer(r)
return err
}
// SetGroupStickerSet should be used to update group's group sticker set.
func (b *Bot) SetGroupStickerSet(chat *Chat, setName string) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"sticker_set_name": setName,
}
r, err := b.Raw("setChatStickerSet", params)
ReleaseBuffer(r)
return err
}
// SetGroupPermissions sets default chat permissions for all members.
func (b *Bot) SetGroupPermissions(chat *Chat, perms Rights) error {
params := map[string]any{
"chat_id": chat.Recipient(),
"permissions": perms,
}
r, err := b.Raw("setChatPermissions", params)
ReleaseBuffer(r)
return err
}
// DeleteGroupPhoto should be used to just remove group photo.
func (b *Bot) DeleteGroupPhoto(chat *Chat) error {
params := map[string]string{
"chat_id": chat.Recipient(),
}
r, err := b.Raw("deleteChatPhoto", params)
ReleaseBuffer(r)
return err
}
// DeleteGroupStickerSet should be used to just remove group sticker set.
func (b *Bot) DeleteGroupStickerSet(chat *Chat) error {
params := map[string]string{
"chat_id": chat.Recipient(),
}
r, err := b.Raw("deleteChatStickerSet", params)
ReleaseBuffer(r)
return err
}