forked from go-vk-api/vk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
longpoll.go
159 lines (131 loc) · 3.08 KB
/
longpoll.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
package vk
import (
"encoding/json"
"github.com/go-resty/resty"
"strconv"
)
const (
longPollNewMessage = 4
)
// Message's flags
const (
FlagMessageUnread = 1 << iota
FlagMessageOutBox
FlagMessageReplied
FlagMessageImportant
FlagMessageChat
FlagMessageFriends
FlagMessageSpam
FlagMessageDeleted
FlagMessageFixed
FlagMessageMedia
)
// LPMessage struct
type LPMessage struct {
ID, Flags, FromID, Timestamp int64
Subject, Text string
Attachments map[string]string
}
// EventNewMessage delegate
type EventNewMessage func(*LPMessage)
type longPoll struct {
client *VK
chanNewMessage chan *LPMessage
eventNewMessage EventNewMessage
data struct {
server string
key string
ts int64
}
}
func (lp *longPoll) update() error {
resp, err := lp.client.CallMethod("messages.getLongPollServer", RequestParams{
"use_ssl": "0",
"need_pts": "0",
})
if err != nil {
return err
}
type JSONBody struct {
Response struct {
Server string `json:"server"`
Key string `json:"key"`
Ts int64 `json:"ts"`
} `json:"response"`
}
var body JSONBody
if err := json.Unmarshal(resp, &body); err != nil {
return err
}
lp.data.server = body.Response.Server
lp.data.key = body.Response.Key
lp.data.ts = body.Response.Ts
return nil
}
func (lp *longPoll) process() {
resp, err := resty.R().
SetQueryParams(RequestParams{
"act": "a_check",
"key": lp.data.key,
"ts": strconv.FormatInt(lp.data.ts, 10),
"wait": "25",
"mode": "2",
}).
Get("https://" + lp.data.server)
if err != nil {
lp.client.Log("[Error] longPoll::process:", err.Error(), "WebResponse:", string(resp.Body()))
return
}
type jsonBody struct {
Failed int64 `json:"failed"`
Ts int64 `json:"ts"`
Updates [][]interface{} `json:"updates"`
}
var body jsonBody
if err := json.Unmarshal(resp.Body(), &body); err != nil {
lp.client.Log("[Error] longPoll::process:", err.Error(), "WebResponse:", string(resp.Body()))
return
}
switch body.Failed {
case 0:
for _, update := range body.Updates {
updateID := update[0].(float64)
switch updateID {
case longPollNewMessage:
message := new(LPMessage)
message.ID = int64(update[1].(float64))
message.Flags = int64(update[2].(float64))
message.FromID = int64(update[3].(float64))
message.Timestamp = int64(update[4].(float64))
message.Subject = update[5].(string)
message.Text = update[6].(string)
message.Attachments = make(map[string]string)
for key, value := range update[7].(map[string]interface{}) {
message.Attachments[key] = value.(string)
}
lp.chanNewMessage <- message
}
}
lp.data.ts = body.Ts
case 1:
lp.data.ts = body.Ts
lp.client.Log("ts updated")
case 2, 3:
if err := lp.update(); err != nil {
lp.client.Log("Longpoll update error:", err.Error())
return
}
lp.client.Log("Longpoll data updated")
}
lp.process()
}
func (lp *longPoll) processEvents() {
for {
select {
case message := <-lp.chanNewMessage:
if lp.eventNewMessage != nil {
lp.eventNewMessage(message)
}
}
}
}