forked from eatmoreapple/openwechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caller.go
749 lines (687 loc) · 20.3 KB
/
caller.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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
package openwechat
import (
"bytes"
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"net/url"
"os"
)
// Caller 调用请求和解析请求
// 上层模块可以直接获取封装后的请求结果
type Caller struct {
Client *Client
}
// NewCaller Constructor for Caller
func NewCaller(client *Client) *Caller {
return &Caller{Client: client}
}
// DefaultCaller Default Constructor for Caller
func DefaultCaller() *Caller {
return NewCaller(DefaultClient())
}
// GetLoginUUID 获取登录的uuid
func (c *Caller) GetLoginUUID(ctx context.Context) (string, error) {
resp, err := c.Client.GetLoginUUID(ctx)
if err != nil {
return "", err
}
defer func() { _ = resp.Body.Close() }()
var buffer bytes.Buffer
if _, err = buffer.ReadFrom(resp.Body); err != nil {
return "", err
}
// 正则匹配uuid字符串
results := uuidRegexp.FindSubmatch(buffer.Bytes())
if len(results) != 2 {
// 如果没有匹配到,可能微信的接口做了修改,或者当前机器的ip被加入了黑名单
return "", errors.New("uuid does not match")
}
return string(results[1]), nil
}
// CheckLogin 检查是否登录成功
func (c *Caller) CheckLogin(ctx context.Context, uuid, tip string) (CheckLoginResponse, error) {
resp, err := c.Client.CheckLogin(ctx, uuid, tip)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var buffer bytes.Buffer
if _, err := buffer.ReadFrom(resp.Body); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// GetLoginInfo 获取登录信息
func (c *Caller) GetLoginInfo(ctx context.Context, path *url.URL) (*LoginInfo, error) {
// 从响应体里面获取需要跳转的url
query := path.Query()
query.Set("version", "v2")
path.RawQuery = query.Encode()
resp, err := c.Client.GetLoginInfo(ctx, path)
if err != nil {
return nil, err
}
// 微信 v2 版本修复了301 response missing Location header 的问题
defer func() { _ = resp.Body.Close() }()
// 这里部分账号可能会被误判, 但是我又没有号测试。如果你遇到了这个问题,可以帮忙解决一下。😊
if _, exists := CookieGroup(resp.Cookies()).GetByName("wxuin"); !exists {
err = ErrForbidden
if c.Client.mode != desktop {
err = fmt.Errorf("%w: try to login with desktop mode", err)
}
return nil, err
}
bs, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var loginInfo LoginInfo
// xml结构体序列化储存
// 为什么这里不直接使用resp.Body?
// 因为要确保传入的reader实现了 io.ByteReader 接口
// https://github.com/eatmoreapple/openwechat/pull/345
if err = xml.NewDecoder(bytes.NewBuffer(bs)).Decode(&loginInfo); err != nil {
return nil, err
}
if !loginInfo.Ok() {
return nil, loginInfo.Err()
}
// set domain
c.Client.Domain = WechatDomain(path.Host)
return &loginInfo, nil
}
// WebInit 获取初始化信息
func (c *Caller) WebInit(ctx context.Context, request *BaseRequest) (*WebInitResponse, error) {
resp, err := c.Client.WebInit(ctx, request)
if err != nil {
return nil, err
}
var webInitResponse WebInitResponse
defer func() { _ = resp.Body.Close() }()
if err = json.NewDecoder(resp.Body).Decode(&webInitResponse); err != nil {
return nil, err
}
if !webInitResponse.BaseResponse.Ok() {
return nil, webInitResponse.BaseResponse.Err()
}
return &webInitResponse, nil
}
type CallerCommonOptions struct {
BaseRequest *BaseRequest
WebInitResponse *WebInitResponse
LoginInfo *LoginInfo
}
type CallerWebWxStatusNotifyOptions CallerCommonOptions
// WebWxStatusNotify 通知手机已登录
func (c *Caller) WebWxStatusNotify(ctx context.Context, opt *CallerWebWxStatusNotifyOptions) error {
notifyOpt := &ClientWebWxStatusNotifyOptions{
BaseRequest: opt.BaseRequest,
WebInitResponse: opt.WebInitResponse,
LoginInfo: opt.LoginInfo,
}
resp, err := c.Client.WebWxStatusNotify(ctx, notifyOpt)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
type CallerSyncCheckOptions CallerCommonOptions
// SyncCheck 异步获取是否有新的消息
func (c *Caller) SyncCheck(ctx context.Context, opt *CallerSyncCheckOptions) (*SyncCheckResponse, error) {
syncCheckOption := &ClientSyncCheckOptions{
BaseRequest: opt.BaseRequest,
WebInitResponse: opt.WebInitResponse,
LoginInfo: opt.LoginInfo,
}
resp, err := c.Client.SyncCheck(ctx, syncCheckOption)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var buffer bytes.Buffer
if _, err = buffer.ReadFrom(resp.Body); err != nil {
return nil, err
}
return NewSyncCheckResponse(buffer.Bytes())
}
// WebWxGetContact 获取所有的联系人
func (c *Caller) WebWxGetContact(ctx context.Context, info *LoginInfo) (Members, error) {
var members Members
var reqs int64
for {
resp, err := c.Client.WebWxGetContact(ctx, info.SKey, reqs)
if err != nil {
return nil, err
}
var item WebWxContactResponse
if err = json.NewDecoder(resp.Body).Decode(&item); err != nil {
_ = resp.Body.Close()
return nil, err
}
if err = resp.Body.Close(); err != nil {
return nil, err
}
if !item.BaseResponse.Ok() {
return nil, item.BaseResponse.Err()
}
members = append(members, item.MemberList...)
if item.Seq == 0 || item.Seq == reqs {
break
}
reqs = item.Seq
}
return members, nil
}
// WebWxBatchGetContact 获取联系人的详情
// 注: Members参数的长度不要大于50
func (c *Caller) WebWxBatchGetContact(ctx context.Context, members Members, request *BaseRequest) (Members, error) {
resp, err := c.Client.WebWxBatchGetContact(ctx, members, request)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var item WebWxBatchContactResponse
if err = json.NewDecoder(resp.Body).Decode(&item); err != nil {
return nil, err
}
if !item.BaseResponse.Ok() {
return nil, item.BaseResponse.Err()
}
return item.ContactList, nil
}
type CallerWebWxSyncOptions CallerCommonOptions
// WebWxSync 获取新的消息接口
func (c *Caller) WebWxSync(ctx context.Context, opt *CallerWebWxSyncOptions) (*WebWxSyncResponse, error) {
wxSyncOption := &ClientWebWxSyncOptions{
BaseRequest: opt.BaseRequest,
WebInitResponse: opt.WebInitResponse,
LoginInfo: opt.LoginInfo,
}
resp, err := c.Client.WebWxSync(ctx, wxSyncOption)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var webWxSyncResponse WebWxSyncResponse
if err = json.NewDecoder(resp.Body).Decode(&webWxSyncResponse); err != nil {
return nil, err
}
return &webWxSyncResponse, nil
}
type CallerWebWxSendMsgOptions struct {
LoginInfo *LoginInfo
BaseRequest *BaseRequest
Message *SendMessage
}
// WebWxSendMsg 发送消息接口
func (c *Caller) WebWxSendMsg(ctx context.Context, opt *CallerWebWxSendMsgOptions) (*SentMessage, error) {
wxSendMsgOption := &ClientWebWxSendMsgOptions{
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
Message: opt.Message,
}
resp, err := c.Client.WebWxSendMsg(ctx, wxSendMsgOption)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.SentMessage(opt.Message)
}
type CallerWebWxOplogOptions struct {
RemarkName string
ToUserName string
BaseRequest *BaseRequest
}
// WebWxOplog 修改用户备注接口
func (c *Caller) WebWxOplog(ctx context.Context, opt *CallerWebWxOplogOptions) error {
wxOpLogOption := &ClientWebWxOplogOption{
RemarkName: opt.RemarkName,
UserName: opt.ToUserName,
BaseRequest: opt.BaseRequest,
}
resp, err := c.Client.WebWxOplog(ctx, wxOpLogOption)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
type CallerUploadMediaOptions struct {
FromUserName string
ToUserName string
File *os.File
BaseRequest *BaseRequest
LoginInfo *LoginInfo
}
func (c *Caller) UploadMedia(ctx context.Context, opt *CallerUploadMediaOptions) (*UploadResponse, error) {
// 首先尝试上传图片
clientWebWxUploadMediaByChunkOpt := &ClientWebWxUploadMediaByChunkOptions{
FromUserName: opt.FromUserName,
ToUserName: opt.ToUserName,
File: opt.File,
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
}
resp, err := c.Client.WebWxUploadMediaByChunk(ctx, clientWebWxUploadMediaByChunkOpt)
// 无错误上传成功之后获取请求结果,判断结果是否正常
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var item UploadResponse
if err = json.NewDecoder(resp.Body).Decode(&item); err != nil {
return &item, err
}
if !item.BaseResponse.Ok() {
return &item, item.BaseResponse.Err()
}
if len(item.MediaId) == 0 {
return &item, errors.New("upload failed")
}
return &item, nil
}
type CallerUploadMediaCommonOptions struct {
FromUserName string
ToUserName string
Reader io.Reader
BaseRequest *BaseRequest
LoginInfo *LoginInfo
}
type CallerWebWxSendImageMsgOptions CallerUploadMediaCommonOptions
// WebWxSendImageMsg 发送图片消息接口
func (c *Caller) WebWxSendImageMsg(ctx context.Context, opt *CallerWebWxSendImageMsgOptions) (*SentMessage, error) {
file, cb, err := readerToFile(opt.Reader)
if err != nil {
return nil, err
}
defer cb()
// 首先尝试上传图片
var mediaId string
{
uploadMediaOption := &CallerUploadMediaOptions{
FromUserName: opt.FromUserName,
ToUserName: opt.ToUserName,
File: file,
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
}
resp, err := c.UploadMedia(ctx, uploadMediaOption)
if err != nil {
return nil, err
}
mediaId = resp.MediaId
}
// 构造新的图片类型的信息
msg := NewMediaSendMessage(MsgTypeImage, opt.FromUserName, opt.ToUserName, mediaId)
// 发送图片信息
sendImageOption := &ClientWebWxSendMsgOptions{
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
Message: msg,
}
resp, err := c.Client.WebWxSendMsgImg(ctx, sendImageOption)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.SentMessage(msg)
}
type CallerWebWxSendFileOptions CallerUploadMediaCommonOptions
func (c *Caller) WebWxSendFile(ctx context.Context, opt *CallerWebWxSendFileOptions) (*SentMessage, error) {
file, cb, err := readerToFile(opt.Reader)
if err != nil {
return nil, err
}
defer cb()
uploadMediaOption := &CallerUploadMediaOptions{
FromUserName: opt.FromUserName,
ToUserName: opt.ToUserName,
File: file,
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
}
resp, err := c.UploadMedia(ctx, uploadMediaOption)
if err != nil {
return nil, err
}
// 构造新的文件类型的信息
stat, _ := file.Stat()
appMsg := NewFileAppMessage(stat, resp.MediaId)
content, err := appMsg.XmlByte()
if err != nil {
return nil, err
}
msg := NewSendMessage(AppMessage, string(content), opt.FromUserName, opt.ToUserName, "")
return c.WebWxSendAppMsg(ctx, msg, opt.BaseRequest)
}
type CallerWebWxSendAppMsgOptions CallerUploadMediaCommonOptions
func (c *Caller) WebWxSendVideoMsg(ctx context.Context, opt *CallerWebWxSendAppMsgOptions) (*SentMessage, error) {
file, cb, err := readerToFile(opt.Reader)
if err != nil {
return nil, err
}
defer cb()
var mediaId string
{
uploadMediaOption := &CallerUploadMediaOptions{
FromUserName: opt.FromUserName,
ToUserName: opt.ToUserName,
File: file,
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
}
resp, err := c.UploadMedia(ctx, uploadMediaOption)
if err != nil {
return nil, err
}
mediaId = resp.MediaId
}
// 构造新的图片类型的信息
msg := NewMediaSendMessage(MsgTypeVideo, opt.FromUserName, opt.ToUserName, mediaId)
resp, err := c.Client.WebWxSendVideoMsg(ctx, opt.BaseRequest, msg)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.SentMessage(msg)
}
// WebWxSendAppMsg 发送媒体消息
func (c *Caller) WebWxSendAppMsg(ctx context.Context, msg *SendMessage, req *BaseRequest) (*SentMessage, error) {
resp, err := c.Client.WebWxSendAppMsg(ctx, msg, req)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.SentMessage(msg)
}
// Logout 用户退出
func (c *Caller) Logout(ctx context.Context, info *LoginInfo) error {
resp, err := c.Client.Logout(ctx, info)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
type CallerAddFriendIntoChatRoomOptions struct {
BaseRequest *BaseRequest
LoginInfo *LoginInfo
Group *Group
GroupLength int
Friends []*Friend
}
// AddFriendIntoChatRoom 拉好友入群
func (c *Caller) AddFriendIntoChatRoom(ctx context.Context, opt *CallerAddFriendIntoChatRoomOptions) error {
if len(opt.Friends) == 0 {
return errors.New("no friends found")
}
inviteMemberList := make([]string, len(opt.Friends))
for i, friend := range opt.Friends {
inviteMemberList[i] = friend.UserName
}
clientAddMemberIntoChatRoomOption := &ClientAddMemberIntoChatRoomOption{
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
Group: opt.Group.UserName,
GroupLength: opt.GroupLength,
InviteMemberList: inviteMemberList,
}
resp, err := c.Client.AddMemberIntoChatRoom(ctx, clientAddMemberIntoChatRoomOption)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
type CallerRemoveFriendFromChatRoomOptions struct {
BaseRequest *BaseRequest
LoginInfo *LoginInfo
Group *Group
Members []*User
}
// RemoveFriendFromChatRoom 从群聊中移除用户
func (c *Caller) RemoveFriendFromChatRoom(ctx context.Context, opt *CallerRemoveFriendFromChatRoomOptions) error {
if len(opt.Members) == 0 {
return errors.New("no users found")
}
users := make([]string, len(opt.Members))
for i, member := range opt.Members {
users[i] = member.UserName
}
req := &ClientRemoveMemberFromChatRoomOption{
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
Group: opt.Group.UserName,
DelMemberList: users,
}
resp, err := c.Client.RemoveMemberFromChatRoom(ctx, req)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
type CallerWebWxVerifyUserOptions struct {
VerifyContent string
RecommendInfo RecommendInfo
BaseRequest *BaseRequest
LoginInfo *LoginInfo
}
// WebWxVerifyUser 同意加好友请求
func (c *Caller) WebWxVerifyUser(ctx context.Context, opt *CallerWebWxVerifyUserOptions) error {
webWxVerifyUserOption := &ClientWebWxVerifyUserOption{
BaseRequest: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
VerifyContent: opt.VerifyContent,
RecommendInfo: opt.RecommendInfo,
}
resp, err := c.Client.WebWxVerifyUser(ctx, webWxVerifyUserOption)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
// WebWxRevokeMsg 撤回消息操作
func (c *Caller) WebWxRevokeMsg(ctx context.Context, msg *SentMessage, request *BaseRequest) error {
resp, err := c.Client.WebWxRevokeMsg(ctx, msg, request)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
type CallerWebWxStatusAsReadOptions struct {
BaseRequest *BaseRequest
LoginInfo *LoginInfo
Message *Message
}
// WebWxStatusAsRead 将消息设置为已读
func (c *Caller) WebWxStatusAsRead(ctx context.Context, opt *CallerWebWxStatusAsReadOptions) error {
statusAsReadOption := &ClientWebWxStatusAsReadOption{
Request: opt.BaseRequest,
LoginInfo: opt.LoginInfo,
Message: opt.Message,
}
resp, err := c.Client.WebWxStatusAsRead(ctx, statusAsReadOption)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
type CallerWebWxRelationPinOptions struct {
BaseRequest *BaseRequest
User *User
Op uint8
}
// WebWxRelationPin 将联系人是否置顶
func (c *Caller) WebWxRelationPin(ctx context.Context, opt *CallerWebWxRelationPinOptions) error {
webWxRelationPinOption := &ClientWebWxRelationPinOption{
Request: opt.BaseRequest,
Op: opt.Op,
RemarkName: opt.User.RemarkName,
UserName: opt.User.UserName,
}
resp, err := c.Client.WebWxRelationPin(ctx, webWxRelationPinOption)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
// WebWxPushLogin 免扫码登陆接口
func (c *Caller) WebWxPushLogin(ctx context.Context, uin int64) (*PushLoginResponse, error) {
resp, err := c.Client.WebWxPushLogin(ctx, uin)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var item PushLoginResponse
if err := json.NewDecoder(resp.Body).Decode(&item); err != nil {
return nil, err
}
return &item, nil
}
type CallerWebWxCreateChatRoomOptions struct {
BaseRequest *BaseRequest
LoginInfo *LoginInfo
Topic string
Friends Friends
}
// WebWxCreateChatRoom 创建群聊
func (c *Caller) WebWxCreateChatRoom(ctx context.Context, opt *CallerWebWxCreateChatRoomOptions) (*Group, error) {
if len(opt.Friends) == 0 {
return nil, errors.New("create group with no friends")
}
friends := make([]string, len(opt.Friends))
for i, friend := range opt.Friends {
friends[i] = friend.UserName
}
webWxCreateChatRoomOption := &ClientWebWxCreateChatRoomOption{
Request: opt.BaseRequest,
Topic: opt.Topic,
Friends: friends,
LoginInfo: opt.LoginInfo,
}
resp, err := c.Client.WebWxCreateChatRoom(ctx, webWxCreateChatRoomOption)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var item struct {
BaseResponse BaseResponse
ChatRoomName string
}
if err = json.NewDecoder(resp.Body).Decode(&item); err != nil {
return nil, err
}
if !item.BaseResponse.Ok() {
return nil, item.BaseResponse.Err()
}
group := Group{User: &User{UserName: item.ChatRoomName}}
return &group, nil
}
type CallerWebWxRenameChatRoomOptions struct {
NewTopic string
BaseRequest *BaseRequest
LoginInfo *LoginInfo
Group *Group
}
// WebWxRenameChatRoom 群组重命名
func (c *Caller) WebWxRenameChatRoom(ctx context.Context, opt *CallerWebWxRenameChatRoomOptions) error {
webWxRenameChatRoomOption := &ClientWebWxRenameChatRoomOption{
Request: opt.BaseRequest,
NewTopic: opt.NewTopic,
Group: opt.Group.UserName,
LoginInfo: opt.LoginInfo,
}
resp, err := c.Client.WebWxRenameChatRoom(ctx, webWxRenameChatRoomOption)
if err != nil {
return err
}
defer func() { _ = resp.Body.Close() }()
parser := MessageResponseParser{resp.Body}
return parser.Err()
}
// SetMode 设置Client的模式
func (c *Client) SetMode(mode Mode) {
c.mode = mode
}
// MessageResponseParser 消息响应解析器
type MessageResponseParser struct {
Reader io.Reader
}
// Err 解析错误
func (p *MessageResponseParser) Err() error {
var item struct{ BaseResponse BaseResponse }
if err := json.NewDecoder(p.Reader).Decode(&item); err != nil {
return err
}
if !item.BaseResponse.Ok() {
return item.BaseResponse.Err()
}
return nil
}
// MsgID 解析消息ID
func (p *MessageResponseParser) MsgID() (string, error) {
var messageResp MessageResponse
if err := json.NewDecoder(p.Reader).Decode(&messageResp); err != nil {
return "", err
}
if !messageResp.BaseResponse.Ok() {
return "", messageResp.BaseResponse.Err()
}
return messageResp.MsgID, nil
}
// SentMessage 返回 SentMessage
func (p *MessageResponseParser) SentMessage(msg *SendMessage) (*SentMessage, error) {
msgID, err := p.MsgID()
if err != nil {
return nil, err
}
return &SentMessage{MsgId: msgID, SendMessage: msg}, nil
}
func readerToFile(reader io.Reader) (file *os.File, cb func(), err error) {
var ok bool
if file, ok = reader.(*os.File); ok {
return file, func() {}, nil
}
file, err = os.CreateTemp("", "*")
if err != nil {
return nil, nil, err
}
cb = func() {
_ = file.Close()
_ = os.Remove(file.Name())
}
_, err = io.Copy(file, reader)
if err != nil {
cb()
return nil, nil, err
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
cb()
return nil, nil, err
}
return file, cb, nil
}