-
Notifications
You must be signed in to change notification settings - Fork 5
/
longpoll_group.go
280 lines (262 loc) · 7.34 KB
/
longpoll_group.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
package govkbot
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
// LongPollServer - longpoll server structure
type GroupLongPollServer struct {
Key string
Server string
Ts string
Wait int
Mode int
Version int
RequestInterval int
NeedPts bool
API *VkAPI
LpVersion int
ReadMessages map[int64]time.Time
}
type GroupLongPollServerResponse struct {
Response GroupLongPollServer
}
type GroupLongPollMessage struct {
Type string
Object struct {
Date int `json:"date"`
FromID int64 `json:"from_id"`
ID int64 `json:"id"`
Out int `json:"out"`
PeerID int `json:"peer_id"`
Text string `json:"text"`
ConversationMessageID int `json:"conversation_message_id"`
FwdMessages []GroupLongPollMessage `json:"fwd_messages"`
Important bool `json:"important"`
RandomID int64 `json:"random_id"`
//Attachments [] `json:"attachments"`
IsHidden bool `json:"is_hidden"`
Action struct {
Type string
MemberID int64 `json:"member_id"`
}
}
GroupID int64
}
type GroupLongPollEvent struct {
Type string
GroupID int64
}
type GroupFailResponse struct {
Failed int
Date int `json:"date,omitempty"`
Ts interface{} `json:"ts,omitempty"`
MinVersion int `json:"min_version"`
MaxVersion int `json:"max_version"`
}
func (g *GroupFailResponse) TS() (string, error) {
switch g.Ts.(type) {
case string:
return g.Ts.(string), nil
case float64:
return strconv.FormatFloat(g.Ts.(float64), 'f', 0, 64), nil
default:
return "", errors.New("unknown type")
}
}
// NewLongPollServer - get longpoll server
func NewGroupLongPollServer(requestInterval int) (resp *GroupLongPollServer) {
server := GroupLongPollServer{}
server.Wait = DefaultWait
server.Mode = DefaultMode
server.Version = DefaultVersion
server.RequestInterval = requestInterval
server.ReadMessages = make(map[int64]time.Time)
return &server
}
// Init - init longpoll server
func (server *GroupLongPollServer) Init() (err error) {
r := GroupLongPollServerResponse{}
err = API.CallMethod("groups.getLongPollServer", H{
"group_id": strconv.FormatInt(API.GroupID, 10),
}, &r)
server.Wait = DefaultWait
server.Mode = DefaultMode
server.Version = DefaultVersion
server.RequestInterval = API.RequestInterval
server.Server = r.Response.Server
server.Ts = r.Response.Ts
server.Key = r.Response.Key
return err
}
type GroupLongPollResponse struct {
Ts string
Messages []*Message
}
// Request - make request to longpoll server
func (server *GroupLongPollServer) Request() ([]byte, error) {
var err error
if server.Server == "" {
err = server.Init()
if err != nil {
log.Fatal(err)
}
}
parameters := url.Values{}
parameters.Add("act", "a_check")
parameters.Add("ts", server.Ts)
parameters.Add("wait", strconv.Itoa(server.Wait))
parameters.Add("key", server.Key)
query := server.Server + "?" + parameters.Encode()
if server.Server == "test" {
content, err := ioutil.ReadFile("./mocks/longpoll.json")
return content, err
}
resp, err := http.Get(query)
if err != nil {
debugPrint("%+v\n", err.Error())
time.Sleep(time.Duration(time.Millisecond * time.Duration(server.RequestInterval)))
return nil, err
}
buf, err := ioutil.ReadAll(resp.Body)
time.Sleep(time.Duration(time.Millisecond * time.Duration(server.RequestInterval)))
//debugPrint("longpoll vk resp: %+v\n", string(buf))
failResp := GroupFailResponse{}
err = json.Unmarshal(buf, &failResp)
if err != nil {
return nil, err
}
switch failResp.Failed {
case 1:
server.Ts, err = failResp.TS()
if err != nil {
log.Printf("error ts: %+v\n", err)
}
return server.Request()
case 2:
err = server.Init()
if err != nil {
log.Fatal(err)
}
return server.Request()
case 3:
err = server.Init()
if err != nil {
log.Fatal(err)
}
return server.Request()
case 4:
return nil, errors.New("vkapi: wrong longpoll version")
default:
server.Ts, err = failResp.TS()
if err != nil {
log.Printf("error ts: %+v\n", err)
}
return buf, nil
}
}
// GetLongPollMessages - get messages via longpoll
func (server *GroupLongPollServer) GetLongPollMessages() ([]*Message, error) {
resp, err := server.Request()
if err != nil {
return nil, err
}
messages, err := server.ParseLongPollMessages(string(resp))
return messages.Messages, nil
}
func (server *GroupLongPollServer) ParseMessage(obj map[string]interface{}) (Message, error) {
msg := Message{}
msg.ID = getJSONInt64(obj["id"])
if obj["text"] != nil {
msg.Body = obj["text"].(string)
} else {
msg.Body = ""
fmt.Printf("error parse message: %+v\n", obj)
return msg, errors.New("error parse message")
}
userID := getJSONInt64(obj["from_id"])
if userID != 0 {
msg.UserID = userID
}
msg.PeerID = getJSONInt64(obj["peer_id"])
if int64(msg.UserID) == msg.PeerID {
msg.ChatID = 0
} else {
msg.ChatID = msg.PeerID
}
msg.Date = getJSONInt(obj["date"])
fwd, ok := obj["fwd_messages"]
if ok {
for _, m := range fwd.([]interface{}) {
fwdMsg, err := server.ParseMessage(m.(map[string]interface{}))
if err != nil {
fmt.Printf("error parse fwd message: %+v\n", err)
} else {
msg.FwdMessages = append(msg.FwdMessages, fwdMsg)
}
}
}
return msg, nil
}
// ParseLongPollMessages - parse longpoll messages
func (server *GroupLongPollServer) ParseLongPollMessages(j string) (*GroupLongPollResponse, error) {
//fmt.Printf("\n>>>>>>>>>>>>>updates0: %+v\n\n", j)
d := json.NewDecoder(strings.NewReader(j))
d.UseNumber()
var lp interface{}
if err := d.Decode(&lp); err != nil {
return nil, err
}
lpMap := lp.(map[string]interface{})
result := GroupLongPollResponse{Messages: []*Message{}}
result.Ts = lpMap["ts"].(string)
updates := lpMap["updates"].([]interface{})
//fmt.Printf("\n>>>>>>>>>>>>>updates: %+v\n\n", updates)
for _, event := range updates {
//el := event.(interface{})
eventType := event.(map[string]interface{})["type"].(string)
if eventType == "message_new" {
obj := event.(map[string]interface{})["object"].(map[string]interface{})
out := getJSONInt(obj["out"])
if out == 0 {
debugPrint("new message: %+v\n", obj)
msg, err := server.ParseMessage(obj)
result.Messages = append(result.Messages, &msg)
if err != nil {
fmt.Printf("error parse message: %+v \n>>>%+v \n>>>>> %+v\n", err, obj, msg)
}
}
}
}
if len(result.Messages) == 0 {
debugPrint(j)
}
if len(result.Messages) > 0 {
debugPrint("new messages: ts: %+v = %+v\n", result.Ts, len(result.Messages))
}
// result.Messages = server.FilterReadMesages(result.Messages)
// fmt.Printf("\n>>>>>>>>>>>>>messages2: %+v\n\n", result)
return &result, nil
}
// FilterReadMesages - filter read messages
func (server *GroupLongPollServer) FilterReadMesages(messages []*Message) (result []*Message) {
for _, m := range messages {
t, ok := server.ReadMessages[m.ID]
if ok {
if time.Since(t).Minutes() > 1 {
delete(server.ReadMessages, m.ID)
}
} else {
result = append(result, m)
server.ReadMessages[m.ID] = time.Now()
}
}
return result
}