forked from eatmoreapple/openwechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelations.go
406 lines (359 loc) · 8.78 KB
/
relations.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
package openwechat
import (
"fmt"
"os"
"time"
)
type Friend struct{ *User }
// implement fmt.Stringer
func (f Friend) String() string {
return fmt.Sprintf("<Friend:%s>", f.NickName)
}
// 重命名当前好友
func (f *Friend) SetRemarkName(name string) error {
return f.Self.SetRemarkNameToFriend(f, name)
}
// 发送自定义消息
func (f *Friend) SendMsg(msg *SendMessage) (*SentMessage, error) {
return f.Self.SendMessageToFriend(f, msg)
}
// 发送文本消息
func (f *Friend) SendText(content string) (*SentMessage, error) {
return f.Self.SendTextToFriend(f, content)
}
// 发送图片消息
func (f *Friend) SendImage(file *os.File) (*SentMessage, error) {
return f.Self.SendImageToFriend(f, file)
}
// 发送文件消息
func (f *Friend) SendFile(file *os.File) (*SentMessage, error) {
return f.Self.SendFileToFriend(f, file)
}
// 拉该好友入群
func (f *Friend) AddIntoGroup(groups ...*Group) error {
return f.Self.AddFriendIntoManyGroups(f, groups...)
}
type Friends []*Friend
// 获取好友的数量
func (f Friends) Count() int {
return len(f)
}
// 获取第一个好友
func (f Friends) First() *Friend {
if f.Count() > 0 {
return f[0]
}
return nil
}
// 获取最后一个好友
func (f Friends) Last() *Friend {
if f.Count() > 0 {
return f[f.Count()-1]
}
return nil
}
// 根据用户名查找好友
func (f Friends) SearchByUserName(limit int, username string) (results Friends) {
return f.Search(limit, func(friend *Friend) bool { return friend.User.UserName == username })
}
// 根据昵称查找好友
func (f Friends) SearchByNickName(limit int, nickName string) (results Friends) {
return f.Search(limit, func(friend *Friend) bool { return friend.User.NickName == nickName })
}
// 根据备注查找好友
func (f Friends) SearchByRemarkName(limit int, remarkName string) (results Friends) {
return f.Search(limit, func(friend *Friend) bool { return friend.User.RemarkName == remarkName })
}
// 根据自定义条件查找好友
func (f Friends) Search(limit int, condFuncList ...func(friend *Friend) bool) (results Friends) {
if condFuncList == nil {
return f
}
if limit <= 0 {
limit = f.Count()
}
for _, member := range f {
if results.Count() == limit {
break
}
var passCount int
for _, condFunc := range condFuncList {
if condFunc(member) {
passCount++
}
}
if passCount == len(condFuncList) {
results = append(results, member)
}
}
return
}
// 向slice的好友依次发送消息
func (f Friends) SendMsg(msg *SendMessage, delay ...time.Duration) error {
total := getTotalDuration(delay...)
var (
sentMessage *SentMessage
err error
self *Self
)
for _, friend := range f {
self = friend.Self
time.Sleep(total)
if sentMessage != nil {
err = self.ForwardMessageToFriends(sentMessage, f...)
return err
}
if sentMessage, err = friend.SendMsg(msg); err != nil {
return err
}
}
return nil
}
// 向slice的好友依次发送文本消息
func (f Friends) SendText(text string, delay ...time.Duration) error {
total := getTotalDuration(delay...)
var (
sentMessage *SentMessage
err error
self *Self
)
for _, friend := range f {
self = friend.Self
time.Sleep(total)
if sentMessage != nil {
err = self.ForwardMessageToFriends(sentMessage, f...)
return err
}
if sentMessage, err = friend.SendText(text); err != nil {
return err
}
}
return nil
}
// 向slice的好友依次发送图片消息
func (f Friends) SendImage(file *os.File, delay ...time.Duration) error {
total := getTotalDuration(delay...)
var (
sentMessage *SentMessage
err error
self *Self
)
for _, friend := range f {
self = friend.Self
time.Sleep(total)
if sentMessage != nil {
err = self.ForwardMessageToFriends(sentMessage, f...)
return err
}
if sentMessage, err = friend.SendImage(file); err != nil {
return err
}
}
return nil
}
type Group struct{ *User }
// implement fmt.Stringer
func (g Group) String() string {
return fmt.Sprintf("<Group:%s>", g.NickName)
}
// 发行消息给当前的群组
func (g *Group) SendMsg(msg *SendMessage) (*SentMessage, error) {
return g.Self.SendMessageToGroup(g, msg)
}
// 发行文本消息给当前的群组
func (g *Group) SendText(content string) (*SentMessage, error) {
return g.Self.SendTextToGroup(g, content)
}
// 发行图片消息给当前的群组
func (g *Group) SendImage(file *os.File) (*SentMessage, error) {
return g.Self.SendImageToGroup(g, file)
}
// 发送文件给当前的群组
func (g *Group) SendFile(file *os.File) (*SentMessage, error) {
return g.Self.SendFileToGroup(g, file)
}
// 获取所有的群成员
func (g *Group) Members() (Members, error) {
group, err := g.Detail()
if err != nil {
return nil, err
}
return group.MemberList, nil
}
// 拉好友入群
func (g *Group) AddFriendsIn(friends ...*Friend) error {
return g.Self.AddFriendsIntoGroup(g, friends...)
}
// 从群聊中移除用户
// Deprecated
// 无论是网页版,还是程序上都不起作用
func (g *Group) RemoveMembers(members Members) error {
return g.Self.RemoveMemberFromGroup(g, members)
}
type Groups []*Group
// 获取群组数量
func (g Groups) Count() int {
return len(g)
}
// 获取第一个群组
func (g Groups) First() *Group {
if g.Count() > 0 {
return g[0]
}
return nil
}
// 获取最后一个群组
func (g Groups) Last() *Group {
if g.Count() > 0 {
return g[g.Count()-1]
}
return nil
}
// 向群组依次发送消息, 支持发送延迟
func (g Groups) SendMsg(msg *SendMessage, delay ...time.Duration) error {
total := getTotalDuration(delay...)
var (
sentMessage *SentMessage
err error
self *Self
)
for _, group := range g {
self = group.Self
time.Sleep(total)
if sentMessage != nil {
err = self.ForwardMessageToGroups(sentMessage, g...)
return err
}
if sentMessage, err = group.SendMsg(msg); err != nil {
return err
}
}
return nil
}
// 向群组依次发送文本消息, 支持发送延迟
func (g Groups) SendText(text string, delay ...time.Duration) error {
total := getTotalDuration(delay...)
var (
sentMessage *SentMessage
err error
self *Self
)
for _, group := range g {
self = group.Self
time.Sleep(total)
if sentMessage != nil {
err = self.ForwardMessageToGroups(sentMessage, g...)
return err
}
if sentMessage, err = group.SendText(text); err != nil {
return err
}
}
return nil
}
// 向群组依次发送图片消息, 支持发送延迟
func (g Groups) SendImage(file *os.File, delay ...time.Duration) error {
total := getTotalDuration(delay...)
var (
sentMessage *SentMessage
err error
self *Self
)
for _, group := range g {
self = group.Self
time.Sleep(total)
if sentMessage != nil {
err = self.ForwardMessageToGroups(sentMessage, g...)
return err
}
if sentMessage, err = group.SendImage(file); err != nil {
return err
}
}
return nil
}
// 根据用户名查找群组
func (g Groups) SearchByUserName(limit int, username string) (results Groups) {
return g.Search(limit, func(group *Group) bool { return group.UserName == username })
}
// 根据昵称查找群组
func (g Groups) SearchByNickName(limit int, nickName string) (results Groups) {
return g.Search(limit, func(group *Group) bool { return group.NickName == nickName })
}
// 根据备注查找群组
func (g Groups) SearchByRemarkName(limit int, remarkName string) (results Groups) {
return g.Search(limit, func(group *Group) bool { return group.RemarkName == remarkName })
}
// 根据自定义条件查找群组
func (g Groups) Search(limit int, condFuncList ...func(group *Group) bool) (results Groups) {
if condFuncList == nil {
return g
}
if limit <= 0 {
limit = g.Count()
}
for _, member := range g {
if results.Count() == limit {
break
}
var passCount int
for _, condFunc := range condFuncList {
if condFunc(member) {
passCount++
}
}
if passCount == len(condFuncList) {
results = append(results, member)
}
}
return
}
// 公众号对象
type Mp struct{ *User }
func (m Mp) String() string {
return fmt.Sprintf("<Mp:%s>", m.NickName)
}
// 公众号组对象
type Mps []*Mp
// 数量统计
func (m Mps) Count() int {
return len(m)
}
// 获取第一个
func (m Mps) First() *Mp {
if m.Count() > 0 {
return m[0]
}
return nil
}
// 获取最后一个
func (m Mps) Last() *Mp {
if m.Count() > 0 {
return m[m.Count()-1]
}
return nil
}
// 根据自定义条件查找
func (m Mps) Search(limit int, condFuncList ...func(group *Mp) bool) (results Mps) {
if condFuncList == nil {
return m
}
if limit <= 0 {
limit = m.Count()
}
for _, member := range m {
if results.Count() == limit {
break
}
var passCount int
for _, condFunc := range condFuncList {
if condFunc(member) {
passCount++
}
}
if passCount == len(condFuncList) {
results = append(results, member)
}
}
return
}