-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.go
207 lines (199 loc) · 6.83 KB
/
send.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
package wwdk
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/ikuiki/wwdk/datastruct"
"github.com/ikuiki/wwdk/tool"
)
// StatusNotify 消息已读通知
func (wxwb *WechatWeb) StatusNotify(fromUserName, toUserName string, code int64) (err error) {
msgID, _ := strconv.ParseInt(tool.GetWxTimeStamp(), 10, 64)
reqBody := datastruct.StatusNotifyRequest{
BaseRequest: wxwb.baseRequest(),
ClientMsgID: msgID,
Code: code,
FromUserName: fromUserName,
ToUserName: toUserName,
}
data, err := json.Marshal(reqBody)
if err != nil {
return errors.New("Marshal request body to json fail: " + err.Error())
}
params := url.Values{}
params.Set("pass_ticket", wxwb.loginInfo.PassTicket)
req, err := http.NewRequest("POST", "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify?"+params.Encode(), bytes.NewReader(data))
if err != nil {
return errors.New("create request error: " + err.Error())
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
resp, err := wxwb.request(req)
if err != nil {
return errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var snResp datastruct.StatusNotifyRespond
err = json.Unmarshal(body, &snResp)
if err != nil {
return errors.New("Unmarshal respond json fail: " + err.Error())
}
if snResp.BaseResponse.Ret != 0 {
return errors.New("respond error ret: " + strconv.FormatInt(snResp.BaseResponse.Ret, 10))
}
return nil
}
// SendTextMessage 发送消息
func (wxwb *WechatWeb) SendTextMessage(toUserName, content string) (sendMessageRespond *datastruct.SendMessageRespond, err error) {
msgReq := datastruct.SendMessageRequest{
BaseRequest: wxwb.baseRequest(),
Msg: &datastruct.SendMessage{
ClientMsgID: tool.GetWxTimeStamp(),
Content: content,
FromUserName: wxwb.user.UserName,
LocalID: tool.GetWxTimeStamp(),
ToUserName: toUserName,
Type: datastruct.TextMsg,
},
}
body, err := json.Marshal(msgReq)
if err != nil {
return nil, errors.New("Marshal body to json fail: " + err.Error())
}
params := url.Values{}
params.Set("pass_ticket", wxwb.loginInfo.PassTicket)
req, err := http.NewRequest("POST", "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?"+params.Encode(), bytes.NewReader(body))
if err != nil {
return nil, errors.New("create request error: " + err.Error())
}
resp, err := wxwb.request(req)
if err != nil {
return nil, errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
var smResp datastruct.SendMessageRespond
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("read response body error: " + err.Error())
}
err = json.Unmarshal(respBody, &smResp)
if err != nil {
return nil, errors.New("UnMarshal respond json fail: " + err.Error())
}
if smResp.BaseResponse.Ret != 0 {
return nil, errors.New("Respond error ret: " + strconv.FormatInt(smResp.BaseResponse.Ret, 10))
}
wxwb.runInfo.MessageCount++
wxwb.runInfo.MessageSentCount++
return &smResp, nil
}
// SendRevokeMessage 撤回消息
func (wxwb *WechatWeb) SendRevokeMessage(svrMsgID, clientMsgID, toUserName string) (revokeMessageRespond *datastruct.RevokeMessageRespond, err error) {
srmReq := datastruct.RevokeMessageRequest{
BaseRequest: wxwb.baseRequest(),
ClientMsgID: clientMsgID,
SvrMsgID: svrMsgID,
ToUserName: toUserName,
}
body, err := json.Marshal(srmReq)
if err != nil {
return nil, errors.New("Marshal body to json fail: " + err.Error())
}
req, err := http.NewRequest("POST", "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxrevokemsg", bytes.NewReader(body))
if err != nil {
return nil, errors.New("create request error: " + err.Error())
}
resp, err := wxwb.request(req)
if err != nil {
return nil, errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
var rmResp datastruct.RevokeMessageRespond
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("read response body error: " + err.Error())
}
err = json.Unmarshal(respBody, &rmResp)
if err != nil {
return nil, errors.New("UnMarshal respond json fail: " + err.Error())
}
if rmResp.BaseResponse.Ret != 0 {
return nil, errors.New("Respond error ret: " + strconv.FormatInt(rmResp.BaseResponse.Ret, 10))
}
wxwb.runInfo.MessageRevokeCount++
wxwb.runInfo.MessageRevokeSentCount++
return &rmResp, nil
}
// ModifyUserRemakName 修改用户备注
func (wxwb *WechatWeb) ModifyUserRemakName(userName, remarkName string) (revokeMessageRespond *datastruct.ModifyRemarkRespond, err error) {
murReq := datastruct.ModifyRemarkRequest{
BaseRequest: wxwb.baseRequest(),
CmdID: 2,
RemarkName: remarkName,
UserName: userName,
}
body, err := json.Marshal(murReq)
if err != nil {
return nil, errors.New("Marshal body to json fail: " + err.Error())
}
req, err := http.NewRequest("POST", "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxoplog", bytes.NewReader(body))
if err != nil {
return nil, errors.New("create request error: " + err.Error())
}
resp, err := wxwb.request(req)
if err != nil {
return nil, errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
var murResp datastruct.ModifyRemarkRespond
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("read response body error: " + err.Error())
}
err = json.Unmarshal(respBody, &murResp)
if err != nil {
return nil, errors.New("UnMarshal respond json fail: " + err.Error())
}
if murResp.BaseResponse.Ret != 0 {
return nil, errors.New("Respond error ret: " + strconv.FormatInt(murResp.BaseResponse.Ret, 10))
}
return &murResp, nil
}
// ModifyChatRoomTopic 修改群名
func (wxwb *WechatWeb) ModifyChatRoomTopic(userName, newTopic string) (revokeMessageRespond *datastruct.ModifyChatRoomTopicRespond, err error) {
mctReq := datastruct.ModifyChatRoomTopicRequest{
BaseRequest: wxwb.baseRequest(),
NewTopic: newTopic,
ChatRoomName: userName,
}
body, err := json.Marshal(mctReq)
if err != nil {
return nil, errors.New("Marshal body to json fail: " + err.Error())
}
req, err := http.NewRequest("POST", "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxupdatechatroom?fun=modtopic", bytes.NewReader(body))
if err != nil {
return nil, errors.New("create request error: " + err.Error())
}
resp, err := wxwb.request(req)
if err != nil {
return nil, errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
var mctResp datastruct.ModifyChatRoomTopicRespond
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New("read response body error: " + err.Error())
}
err = json.Unmarshal(respBody, &mctResp)
if err != nil {
return nil, errors.New("UnMarshal respond json fail: " + err.Error())
}
if mctResp.BaseResponse.Ret != 0 {
return nil, errors.New("Respond error ret: " + strconv.FormatInt(mctResp.BaseResponse.Ret, 10))
}
return &mctResp, nil
}