forked from AmarnathCJD/gogram
/
channels.go
executable file
·639 lines (601 loc) · 17.8 KB
/
channels.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Copyright (c) 2024 RoseLoverX
package telegram
import (
"github.com/pkg/errors"
)
// GetChatPhotos returns the profile photos of a chat
//
// Params:
// - chatID: The ID of the chat
// - limit: The maximum number of photos to be returned
func (c *Client) GetChatPhotos(chatID interface{}, limit ...int32) ([]Photo, error) {
if limit == nil {
limit = []int32{1}
}
messages, err := c.GetMessages(chatID, &SearchOption{Limit: limit[0],
Filter: &InputMessagesFilterChatPhotos{}})
if err != nil {
return nil, err
}
var photos []Photo
for _, message := range messages {
if message.Action != nil {
switch action := message.Action.(type) {
case *MessageActionChatEditPhoto:
photos = append(photos, action.Photo)
case *MessageActionChatDeletePhoto:
}
}
}
return photos, nil
}
// GetChatPhoto returns the current chat photo
//
// Params:
// - chatID: chat id
func (c *Client) GetChatPhoto(chatID interface{}) (Photo, error) {
photos, err := c.GetChatPhotos(chatID)
if err != nil {
return &PhotoObj{}, err
}
if len(photos) > 0 {
return photos[0], nil
}
return &PhotoObj{}, nil // GetFullChannel TODO
}
// JoinChannel joins a channel or chat by its username or id
//
// Params:
// - Channel: the username or id of the channel or chat
func (c *Client) JoinChannel(Channel interface{}) error {
switch p := Channel.(type) {
case string:
if TG_JOIN_RE.MatchString(p) {
_, err := c.MessagesImportChatInvite(TG_JOIN_RE.FindStringSubmatch(p)[2])
if err != nil {
return err
}
}
default:
channel, err := c.GetSendablePeer(Channel)
if err != nil {
return err
}
if chat, ok := channel.(*InputPeerChannel); ok {
_, err = c.ChannelsJoinChannel(&InputChannelObj{ChannelID: chat.ChannelID, AccessHash: chat.AccessHash})
if err != nil {
return err
}
} else if chat, ok := channel.(*InputPeerChat); ok {
_, err = c.MessagesAddChatUser(chat.ChatID, &InputUserEmpty{}, 0)
if err != nil {
return err
}
} else {
return errors.New("peer is not a channel or chat")
}
}
return nil
}
// LeaveChannel leaves a channel or chat
//
// Params:
// - Channel: Channel or chat to leave
// - Revoke: If true, the channel will be deleted
func (c *Client) LeaveChannel(Channel interface{}, Revoke ...bool) error {
revokeChat := getVariadic(Revoke, false).(bool)
channel, err := c.GetSendablePeer(Channel)
if err != nil {
return err
}
if chat, ok := channel.(*InputPeerChannel); ok {
_, err = c.ChannelsLeaveChannel(&InputChannelObj{ChannelID: chat.ChannelID, AccessHash: chat.AccessHash})
if err != nil {
return err
}
} else if chat, ok := channel.(*InputPeerChat); ok {
_, err = c.MessagesDeleteChatUser(revokeChat, chat.ChatID, &InputUserEmpty{})
if err != nil {
return err
}
} else {
return errors.New("peer is not a channel or chat")
}
return nil
}
const (
Admin = "admin"
Creator = "creator"
Member = "member"
Restricted = "restricted"
Left = "left"
Kicked = "kicked"
)
type Participant struct {
User *UserObj `json:"user,omitempty"`
Participant ChannelParticipant `json:"participant,omitempty"`
Status string `json:"status,omitempty"`
Rights *ChatAdminRights `json:"rights,omitempty"`
Rank string `json:"rank,omitempty"`
}
// GetChatMember returns the members of a chat
//
// Params:
// - chatID: The ID of the chat
// - userID: The ID of the user
func (c *Client) GetChatMember(chatID interface{}, userID interface{}) (*Participant, error) {
channel, err := c.GetSendablePeer(chatID)
if err != nil {
return nil, err
}
user, err := c.GetSendablePeer(userID)
if err != nil {
return nil, err
}
chat, ok := channel.(*InputPeerChannel)
if !ok {
return nil, errors.New("peer is not a channel")
}
participant, err := c.ChannelsGetParticipant(&InputChannelObj{ChannelID: chat.ChannelID, AccessHash: chat.AccessHash}, user)
if err != nil {
return nil, err
}
c.Cache.UpdatePeersToCache(participant.Users, participant.Chats)
var (
status string = Member
rights *ChatAdminRights = &ChatAdminRights{}
rank string = ""
UserID int64 = 0
)
switch p := participant.Participant.(type) {
case *ChannelParticipantCreator:
status = Creator
rights = p.AdminRights
rank = p.Rank
UserID = p.UserID
case *ChannelParticipantAdmin:
status = Admin
rights = p.AdminRights
rank = p.Rank
UserID = p.UserID
case *ChannelParticipantObj:
status = Member
UserID = p.UserID
case *ChannelParticipantSelf:
status = Member
UserID = p.UserID
case *ChannelParticipantBanned:
status = Restricted
UserID = c.GetPeerID(p.Peer)
case *ChannelParticipantLeft:
status = Left
UserID = c.GetPeerID(p.Peer)
}
partUser, err := c.GetUser(UserID)
if err != nil {
return nil, err
}
return &Participant{
User: partUser,
Participant: participant.Participant,
Status: status,
Rights: rights,
Rank: rank,
}, nil
}
type ParticipantOptions struct {
Query string `json:"query,omitempty"`
Filter ChannelParticipantsFilter `json:"filter,omitempty"`
Offset int32 `json:"offset,omitempty"`
Limit int32 `json:"limit,omitempty"`
}
// GetChatMembers returns the members of a chat
//
// Params:
// - chatID: The ID of the chat
// - filter: The filter to use
// - offset: The offset to use
// - limit: The limit to use
func (c *Client) GetChatMembers(chatID interface{}, Opts ...*ParticipantOptions) ([]*Participant, int32, error) {
channel, err := c.GetSendablePeer(chatID)
if err != nil {
return nil, 0, err
}
chat, ok := channel.(*InputPeerChannel)
if !ok {
return nil, 0, errors.New("peer is not a channel")
}
opts := getVariadic(Opts, &ParticipantOptions{Filter: &ChannelParticipantsSearch{}, Limit: 1}).(*ParticipantOptions)
if opts.Query != "" {
opts.Filter = &ChannelParticipantsSearch{Q: opts.Query}
}
participants, err := c.ChannelsGetParticipants(&InputChannelObj{ChannelID: chat.ChannelID, AccessHash: chat.AccessHash}, opts.Filter, opts.Offset, opts.Limit, 0)
if err != nil {
return nil, 0, err
}
cParts, ok := participants.(*ChannelsChannelParticipantsObj)
if !ok {
return nil, 0, errors.New("could not get participants")
}
c.Cache.UpdatePeersToCache(cParts.Users, cParts.Chats)
var (
status string = Member
rights *ChatAdminRights = &ChatAdminRights{}
rank string = ""
UserID int64 = 0
)
participantsList := make([]*Participant, 0)
for _, p := range cParts.Participants {
switch p := p.(type) {
case *ChannelParticipantCreator:
status = Creator
rights = p.AdminRights
rank = p.Rank
UserID = p.UserID
case *ChannelParticipantAdmin:
status = Admin
rights = p.AdminRights
rank = p.Rank
UserID = p.UserID
case *ChannelParticipantObj:
status = Member
case *ChannelParticipantSelf:
status = Member
UserID = p.UserID
case *ChannelParticipantBanned:
status = Restricted
UserID = c.GetPeerID(p.Peer)
case *ChannelParticipantLeft:
status = Left
UserID = c.GetPeerID(p.Peer)
}
partUser, err := c.GetUser(UserID)
if err != nil {
return nil, 0, err
}
participantsList = append(participantsList, &Participant{
User: partUser,
Participant: p,
Status: status,
Rights: rights,
Rank: rank,
})
}
return participantsList, cParts.Count, nil
}
type AdminOptions struct {
IsAdmin bool `json:"is_admin,omitempty"`
Rights *ChatAdminRights `json:"rights,omitempty"`
Rank string `json:"rank,omitempty"`
}
// Edit Admin rights of a user in a chat,
// returns true if successfull
func (c *Client) EditAdmin(PeerID interface{}, UserID interface{}, Opts ...*AdminOptions) (bool, error) {
opts := getVariadic(Opts, &AdminOptions{IsAdmin: true, Rights: &ChatAdminRights{}, Rank: "Admin"}).(*AdminOptions)
peer, err := c.GetSendablePeer(PeerID)
if err != nil {
return false, err
}
u, err := c.GetSendablePeer(UserID)
if err != nil {
return false, err
}
user, ok := u.(*InputPeerUser)
if !ok {
return false, errors.New("peer is not a user")
}
switch p := peer.(type) {
case *InputPeerChannel:
if opts.IsAdmin {
_, err := c.ChannelsEditAdmin(&InputChannelObj{ChannelID: p.ChannelID, AccessHash: p.AccessHash}, &InputUserObj{UserID: user.UserID, AccessHash: user.AccessHash}, opts.Rights, opts.Rank)
if err != nil {
return false, err
}
} else {
_, err := c.ChannelsEditAdmin(&InputChannelObj{ChannelID: p.ChannelID, AccessHash: p.AccessHash}, &InputUserObj{UserID: user.UserID, AccessHash: user.AccessHash}, &ChatAdminRights{}, "")
if err != nil {
return false, err
}
}
case *InputPeerChat:
_, err := c.MessagesEditChatAdmin(p.ChatID, &InputUserObj{UserID: user.UserID, AccessHash: user.AccessHash}, opts.IsAdmin)
if err != nil {
return false, err
}
default:
return false, errors.New("peer is not a chat or channel")
}
return true, nil
}
type BannedOptions struct {
Ban bool `json:"ban,omitempty"`
Unban bool `json:"unban,omitempty"`
Mute bool `json:"mute,omitempty"`
Unmute bool `json:"unmute,omitempty"`
Rights *ChatBannedRights `json:"rights,omitempty"`
Revoke bool `json:"revoke,omitempty"`
}
// Edit Restricted rights of a user in a chat,
// returns true if successfull
func (c *Client) EditBanned(PeerID interface{}, UserID interface{}, opts ...*BannedOptions) (bool, error) {
o := getVariadic(opts, &BannedOptions{Ban: true, Rights: &ChatBannedRights{}}).(*BannedOptions)
if o.Rights == nil {
o.Rights = &ChatBannedRights{}
}
peer, err := c.GetSendablePeer(PeerID)
if err != nil {
return false, err
}
u, err := c.GetSendablePeer(UserID)
if err != nil {
return false, err
}
switch p := peer.(type) {
case *InputPeerChannel:
if o.Ban {
o.Rights.ViewMessages = true
}
if o.Unban {
o.Rights.ViewMessages = false
}
if o.Mute {
o.Rights.SendMessages = true
}
if o.Unmute {
o.Rights.SendMessages = false
}
_, err := c.ChannelsEditBanned(&InputChannelObj{ChannelID: p.ChannelID, AccessHash: p.AccessHash}, u, o.Rights)
if err != nil {
return false, err
}
case *InputPeerChat:
if o.Ban || o.Unban || o.Mute || o.Unmute {
if u, ok := u.(*InputPeerUser); ok {
_, err := c.MessagesDeleteChatUser(o.Revoke, p.ChatID, &InputUserObj{UserID: u.UserID, AccessHash: u.AccessHash})
return err == nil, err
}
} else {
if u, ok := u.(*InputPeerUser); ok {
_, err := c.MessagesAddChatUser(p.ChatID, &InputUserObj{UserID: u.UserID, AccessHash: u.AccessHash}, 0)
return err == nil, err
}
}
default:
return false, errors.New("peer is not a chat or channel")
}
return true, nil
}
func (c *Client) KickParticipant(PeerID interface{}, UserID interface{}) (bool, error) {
peer, err := c.GetSendablePeer(PeerID)
if err != nil {
return false, err
}
u, err := c.GetSendablePeer(UserID)
if err != nil {
return false, err
}
switch p := peer.(type) {
case *InputPeerChannel:
_, err := c.EditBanned(p, u, &BannedOptions{Ban: true})
if err != nil {
return false, err
}
_, err = c.EditBanned(p, u, &BannedOptions{Unban: true})
if err != nil {
return false, err
}
case *InputPeerChat:
user, ok := u.(*InputPeerUser)
if !ok {
return false, errors.New("peer is not a user")
}
_, err := c.MessagesDeleteChatUser(false, c.GetPeerID(p), &InputUserObj{UserID: user.UserID, AccessHash: user.AccessHash})
if err != nil {
return false, err
}
default:
return false, errors.New("peer is not a chat or channel")
}
return true, nil
}
type TitleOptions struct {
LastName string `json:"last_name,omitempty"`
About string `json:"about,omitempty"`
}
// Edit the title of a chat, channel or self,
// returns true if successfull
func (c *Client) EditTitle(PeerID interface{}, Title string, Opts ...*TitleOptions) (bool, error) {
opts := getVariadic(Opts, &TitleOptions{}).(*TitleOptions)
peer, err := c.GetSendablePeer(PeerID)
if err != nil {
return false, err
}
switch p := peer.(type) {
case *InputPeerChannel:
_, err := c.ChannelsEditTitle(&InputChannelObj{ChannelID: p.ChannelID, AccessHash: p.AccessHash}, Title)
if err != nil {
return false, err
}
case *InputPeerChat:
_, err := c.MessagesEditChatTitle(p.ChatID, Title)
if err != nil {
return false, err
}
case *InputPeerSelf:
_, err := c.AccountUpdateProfile(opts.LastName, Title, opts.About)
if err != nil {
return false, err
}
default:
return false, errors.New("peer is not a chat or channel or self")
}
return true, nil
}
// GetStats returns the stats of the channel or message
//
// Params:
// - channelID: the channel ID
// - messageID: the message ID
func (c *Client) GetStats(channelID interface{}, messageID ...interface{}) (*StatsBroadcastStats, *StatsMessageStats, error) {
peerID, err := c.GetSendablePeer(channelID)
if err != nil {
return nil, nil, err
}
channelPeer, ok := peerID.(*InputPeerChannel)
if !ok {
return nil, nil, errors.New("could not convert peer to channel")
}
var MessageID int32 = getVariadic(messageID, 0).(int32)
if MessageID > 0 {
resp, err := c.StatsGetMessageStats(true, &InputChannelObj{
ChannelID: channelPeer.ChannelID,
AccessHash: channelPeer.AccessHash,
}, MessageID)
if err != nil {
return nil, nil, err
}
return nil, resp, nil
}
resp, err := c.StatsGetBroadcastStats(true, &InputChannelObj{
ChannelID: channelPeer.ChannelID,
AccessHash: channelPeer.AccessHash,
})
if err != nil {
return nil, nil, err
}
return resp, nil, nil
}
type InviteLinkOptions struct {
LegacyRevokePermanent bool `json:"legacy_revoke_permanent,omitempty"`
Expire int32 `json:"expire,omitempty"`
Limit int32 `json:"limit,omitempty"`
Title string `json:"title,omitempty"`
RequestNeeded bool `json:"request_needed,omitempty"`
}
// GetChatInviteLink returns the invite link of a chat
//
// Params:
// - peerID : The ID of the chat
// - LegacyRevoke : If true, the link will be revoked
// - Expire: The time in seconds after which the link will expire
// - Limit: The maximum number of users that can join the chat using the link
// - Title: The title of the link
// - RequestNeeded: If true, join requests will be needed to join the chat
func (c *Client) GetChatInviteLink(peerID interface{}, LinkOpts ...*InviteLinkOptions) (ExportedChatInvite, error) {
LinkOptions := getVariadic(LinkOpts, &InviteLinkOptions{}).(*InviteLinkOptions)
peer, err := c.GetSendablePeer(peerID)
if err != nil {
return nil, err
}
link, err := c.MessagesExportChatInvite(&MessagesExportChatInviteParams{
Peer: peer,
LegacyRevokePermanent: LinkOptions.LegacyRevokePermanent,
RequestNeeded: LinkOptions.RequestNeeded,
UsageLimit: LinkOptions.Limit,
Title: LinkOptions.Title,
ExpireDate: LinkOptions.Expire,
})
return link, err
}
type ChannelOptions struct {
About string `json:"about,omitempty"`
NotBroadcast bool `json:"broadcast,omitempty"`
Megagroup bool `json:"megagroup,omitempty"`
Address string `json:"address,omitempty"`
ForImport bool `json:"for_import,omitempty"`
GeoPoint InputGeoPoint `json:"geo_point,omitempty"`
}
func (c *Client) CreateChannel(title string, opts ...ChannelOptions) (*Channel, error) {
opt := getVariadic(opts, &ChannelOptions{}).(*ChannelOptions)
u, err := c.ChannelsCreateChannel(&ChannelsCreateChannelParams{
Broadcast: !opt.NotBroadcast,
GeoPoint: opt.GeoPoint,
About: opt.About, ForImport: opt.ForImport,
Address: opt.Address,
Megagroup: opt.Megagroup,
Title: title,
})
if err != nil {
return nil, err
}
switch u := u.(type) {
case *UpdatesObj:
chat := u.Chats[0]
if ch, ok := chat.(*Channel); ok {
return ch, nil
}
}
return nil, errors.New("empty reply from server")
}
func (c *Client) DeleteChannel(channelID interface{}) (*Updates, error) {
peer, err := c.GetSendablePeer(channelID)
if err != nil {
return nil, err
}
channelPeer, ok := peer.(*InputPeerChannel)
if !ok {
return nil, errors.New("could not convert peer to channel")
}
u, err := c.ChannelsDeleteChannel(&InputChannelObj{
ChannelID: channelPeer.ChannelID,
AccessHash: channelPeer.AccessHash,
})
if err != nil {
return nil, err
}
return &u, nil
}
func (c *Client) GetChatJoinRequests(channelID interface{}, lim int) ([]*UserObj, error) {
perLimit := 100
var currentOffsetUser InputUser = &InputUserEmpty{}
currentOffsetDate := 0
// Set active limit to lim if it's less than perLimit, else set it to perLimit
activeLimit := perLimit
if lim < perLimit {
activeLimit = lim
}
// Get sendable peer
peer, err := c.GetSendablePeer(channelID)
if err != nil {
return nil, err
}
// Initialize empty slice to store all users
var allUsers []*UserObj
// Loop until lim is reached
for {
// Get chat invite importers
chatInviteImporters, err := c.MessagesGetChatInviteImporters(&MessagesGetChatInviteImportersParams{
Requested: true,
Peer: peer,
Q: "",
OffsetDate: int32(currentOffsetDate),
OffsetUser: currentOffsetUser,
Limit: int32(activeLimit),
})
if err != nil {
return nil, err
}
// Add all UserObj objects to allUsers slice
for _, user := range chatInviteImporters.Users {
if u, ok := user.(*UserObj); ok {
allUsers = append(allUsers, u)
}
}
// Decrement lim by activeLimit
lim -= activeLimit
// Break out of loop if lim is reached
if lim <= 0 {
break
}
// Set current offset user and date for next iteration
if len(chatInviteImporters.Users) > 0 {
if u, ok := chatInviteImporters.Users[len(chatInviteImporters.Users)-1].(*UserObj); ok {
currentOffsetUser = &InputUserObj{UserID: u.ID, AccessHash: u.AccessHash}
}
}
if len(chatInviteImporters.Importers) > 0 {
currentOffsetDate = int(chatInviteImporters.Importers[len(chatInviteImporters.Importers)-1].Date)
}
}
return allUsers, nil
}