-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.go
231 lines (218 loc) · 7.5 KB
/
api.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
package qqbot
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
)
type Friend struct {
Nick string
MarkName string
Uin int64
Status string
}
func (this *User) GetFriends() []Friend {
req, _ := http.NewRequest("POST", "http://s.web2.qq.com/api/get_user_friends2", bytes.NewReader([]byte("r=%7B%22vfwebqq%22%3A%22"+this.Vfwebqq+"%22%2C%22hash%22%3A%22"+TxHash(this.Uin, this.Ptwebqq)+"%22%7D")))
req.Header.Add("Referer", "http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, _ := this.Client.Do(req)
data, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
var result struct {
RetCode int
Result struct {
Info []Friend
Marknames []Friend
}
}
if result.RetCode != 0 {
fmt.Println(string(data))
}
json.Unmarshal(data, &result)
friendsMap := make(map[int64]Friend)
for _, it := range result.Result.Info {
friendsMap[it.Uin] = it
}
for _, it := range result.Result.Marknames {
tmp := friendsMap[it.Uin]
tmp.MarkName = it.MarkName
friendsMap[it.Uin] = tmp
}
friends := []Friend{}
for _, it := range friendsMap {
friends = append(friends, it)
}
return friends
}
type Group struct {
Code int64
Name string
Gid int64
}
func (this *User) GetGroups() []Group {
req, _ := http.NewRequest("POST", "http://s.web2.qq.com/api/get_group_name_list_mask2", bytes.NewReader([]byte("r=%7B%22vfwebqq%22%3A%22"+this.Vfwebqq+"%22%2C%22hash%22%3A%22"+TxHash(this.Uin, this.Ptwebqq)+"%22%7D")))
req.Header.Add("Referer", "http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, _ := this.Client.Do(req)
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
var result struct {
RetCode int
Result struct {
Gnamelist []Group
}
}
if result.RetCode != 0 {
fmt.Println(string(data))
}
json.Unmarshal(data, &result)
return result.Result.Gnamelist
}
type SelfInfo struct {
Account int64
Nick string
Gender string
Country string
Province string
City string
}
func (this *User) GetSelfInfo() SelfInfo {
req, _ := http.NewRequest("GET", "http://s.web2.qq.com/api/get_self_info2", nil)
req.Header.Add("Referer", "http://s.web2.qq.com/proxy.html?v=20130916001&callback=1&id=1")
res, _ := this.Client.Do(req)
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
var result struct {
RetCode int
Result SelfInfo
}
if result.RetCode != 0 {
fmt.Println(string(data))
}
json.Unmarshal(data, &result)
return result.Result
}
func (this *User) GetOnline() []Friend {
req, _ := http.NewRequest("GET", "http://d1.web2.qq.com/channel/get_online_buddies2?vfwebqq="+this.Vfwebqq+"&clientid=53999199&psessionid="+this.Pssesionid+"&t=1473673565772", nil)
req.Header.Add("Referer", "http://d1.web2.qq.com/proxy.html?v=20151105001&callback=1&id=2")
res, _ := this.Client.Do(req)
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
var result struct {
Retcode int
Result []Friend
}
json.Unmarshal(data, &result)
if result.Retcode != 0 {
fmt.Println(string(data))
}
return result.Result
}
func (this *User) SendMessage(uin int64, content string) error {
uinStr := strconv.FormatInt(uin, 10)
msgId := strconv.Itoa(rand.Intn(8))
req, _ := http.NewRequest("POST", "https://d1.web2.qq.com/channel/send_buddy_msg2", bytes.NewReader([]byte("r=%7B%22to%22%3A"+uinStr+"%2C%22content%22%3A%22%5B%5C%22"+content+"%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A10%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22face%22%3A525%2C%22clientid%22%3A53999199%2C%22msg_id%22%3A"+msgId+"%2C%22psessionid%22%3A%22"+this.Pssesionid+"%22%7D")))
req.Header.Add("Referer", "https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, _ := this.Client.Do(req)
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
var result struct {
Retcode int
ErrCode int
Msg string
}
json.Unmarshal(data, &result)
if result.ErrCode != 0 || result.Retcode != 0 {
return errors.New(result.Msg)
}
return nil
}
func (this *User) SendGroupMessage(uin int64, content string) error {
uinStr := strconv.FormatInt(uin, 10)
msgId := strconv.Itoa(rand.Intn(8))
req, _ := http.NewRequest("POST", "https://d1.web2.qq.com/channel/send_qun_msg2", bytes.NewReader([]byte("r=%7B%22group_uin%22%3A"+uinStr+"%2C%22content%22%3A%22%5B%5C%22"+content+"%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A10%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22face%22%3A525%2C%22clientid%22%3A53999199%2C%22msg_id%22%3A"+msgId+"%2C%22psessionid%22%3A%22"+this.Pssesionid+"%22%7D")))
req.Header.Add("Referer", "https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, _ := this.Client.Do(req)
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
var result struct {
Retcode int
ErrCode int
Msg string
}
json.Unmarshal(data, &result)
if result.ErrCode != 0 || result.Retcode != 0 {
return errors.New(result.Msg)
}
return nil
}
func (this *User) SendDiscuMessage(uin int64, content string) error {
uinStr := strconv.FormatInt(uin, 10)
msgId := strconv.Itoa(rand.Intn(8))
req, _ := http.NewRequest("POST", "https://d1.web2.qq.com/channel/send_discu_msg2", bytes.NewReader([]byte("r=%7B%22did%22%3A"+uinStr+"%2C%22content%22%3A%22%5B%5C%22"+content+"%5C%22%2C%5B%5C%22font%5C%22%2C%7B%5C%22name%5C%22%3A%5C%22%E5%AE%8B%E4%BD%93%5C%22%2C%5C%22size%5C%22%3A10%2C%5C%22style%5C%22%3A%5B0%2C0%2C0%5D%2C%5C%22color%5C%22%3A%5C%22000000%5C%22%7D%5D%5D%22%2C%22face%22%3A525%2C%22clientid%22%3A53999199%2C%22msg_id%22%3A"+msgId+"%2C%22psessionid%22%3A%22"+this.Pssesionid+"%22%7D")))
req.Header.Add("Referer", "https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, _ := this.Client.Do(req)
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
var result struct {
Retcode int
ErrCode int
Msg string
}
json.Unmarshal(data, &result)
if result.ErrCode != 0 || result.Retcode != 0 {
return errors.New(result.Msg)
}
return nil
}
type Message struct {
Type string //one of message|group_message
From int64
To int64
Content string
}
func (this *User) Poll() chan Message {
c := make(chan Message)
go func() {
for {
req, _ := http.NewRequest("POST", "https://d1.web2.qq.com/channel/poll2", bytes.NewReader([]byte("r=%7B%22ptwebqq%22%3A%22"+this.Ptwebqq+"%22%2C%22clientid%22%3A53999199%2C%22psessionid%22%3A%22"+this.Pssesionid+"%22%2C%22key%22%3A%22%22%7D")))
req.Header.Add("Referer", "https://d1.web2.qq.com/cfproxy.html?v=20151105001&callback=1")
res, err := this.Client.Do(req)
if err != nil {
continue
}
defer res.Body.Close()
data, _ := ioutil.ReadAll(res.Body)
var result struct {
Retcode int
Result []struct {
Poll_type string
Value struct {
From_uin int64
To_uin int64
Content [2]string
}
}
}
fmt.Println(string(data))
json.Unmarshal(data, &result)
for _, it := range result.Result {
msg := Message{
Type: it.Poll_type,
From: it.Value.From_uin,
To: it.Value.To_uin,
Content: it.Value.Content[1],
}
c <- msg
}
}
}()
return c
}