-
Notifications
You must be signed in to change notification settings - Fork 1
/
receive.go
123 lines (117 loc) · 3.89 KB
/
receive.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
package wwdk
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/ikuiki/wwdk/datastruct"
"github.com/ikuiki/wwdk/tool"
)
// getMessage 同步消息
// 如果同步状态接口返回有新消息需要同步,通过此接口从服务器中获取新消息
func (wxwb *WechatWeb) getMessage() (gmResp datastruct.GetMessageRespond, err error) {
gmResp = datastruct.GetMessageRespond{}
data, err := json.Marshal(datastruct.GetMessageRequest{
BaseRequest: wxwb.baseRequest(),
SyncKey: wxwb.loginInfo.syncKey,
Rr: ^time.Now().Unix() + 1,
})
if err != nil {
return gmResp, errors.New("Marshal request body to json fail: " + err.Error())
}
params := url.Values{}
params.Set("sid", wxwb.loginInfo.cookie.Wxsid)
params.Set("skey", wxwb.loginInfo.sKey)
// params.Set("pass_ticket", wxwb.PassTicket)
resp, err := wxwb.client.Post("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync?"+params.Encode(),
"application/json;charset=UTF-8",
bytes.NewReader(data))
if err != nil {
return gmResp, errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &gmResp)
if err != nil {
return gmResp, errors.New("Unmarshal respond json fail: " + err.Error())
}
if gmResp.BaseResponse.Ret != 0 {
return gmResp, errors.New("respond error ret: " + strconv.FormatInt(gmResp.BaseResponse.Ret, 10))
}
// if gmResp.AddMsgCount > 0 {
// fmt.Println(string(resp))
// panic(nil)
// }
return gmResp, nil
}
// SaveMessageImage 保存消息图片到指定位置
func (wxwb *WechatWeb) SaveMessageImage(msg datastruct.Message) (filename string, err error) {
params := url.Values{}
params.Set("MsgID", msg.MsgID)
params.Set("skey", wxwb.loginInfo.sKey)
// params.Set("type", "slave")
resp, err := wxwb.client.Get("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg?" + params.Encode())
if err != nil {
return "", errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
filename = msg.MsgID + ".jpg"
_, err = tool.WriteToFile(filename, resp.Body)
if err != nil {
return "", errors.New("WriteToFile error: " + err.Error())
}
return filename, nil
}
// SaveMessageVoice 保存消息声音到指定位置
func (wxwb *WechatWeb) SaveMessageVoice(msg datastruct.Message) (filename string, err error) {
if msg.MsgType != datastruct.VoiceMsg {
return "", errors.New("Message type wrong")
}
params := url.Values{}
params.Set("MsgID", msg.MsgID)
params.Set("skey", wxwb.loginInfo.sKey)
// params.Set("type", "slave")
resp, err := wxwb.client.Get("https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice?" + params.Encode())
if err != nil {
return "", errors.New("request error: " + err.Error())
}
defer resp.Body.Close()
filename = msg.MsgID + ".mp3"
_, err = tool.WriteToFile(filename, resp.Body)
if err != nil {
return "", errors.New("WriteToFile error: " + err.Error())
}
return filename, nil
}
// SaveMessageVideo 保存消息视频到指定位置
func (wxwb *WechatWeb) SaveMessageVideo(msg datastruct.Message) (filename string, err error) {
if msg.MsgType != datastruct.LittleVideoMsg {
return "", errors.New("Message type wrong")
}
params := url.Values{}
params.Set("msgid", msg.MsgID)
params.Set("skey", wxwb.loginInfo.sKey)
req, err := http.NewRequest("GET", "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo?"+params.Encode(), strings.NewReader(""))
if err != nil {
return "", errors.New("create request error: " + err.Error())
}
req.Header.Set("Range", "bytes=0-")
resp, err := wxwb.client.Do(req)
if err != nil {
return "", errors.New("request error: " + err.Error())
}
filename = msg.MsgID + ".mp4"
n, err := tool.WriteToFile(filename, resp.Body)
if err != nil {
return "", errors.New("WriteToFile error: " + err.Error())
}
if int64(n) != resp.ContentLength {
return filename, errors.New("File size wrong")
}
return filename, nil
}