-
Notifications
You must be signed in to change notification settings - Fork 0
/
BotLongPoll.go
222 lines (197 loc) · 4.67 KB
/
BotLongPoll.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
package VkApi
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
)
type BotLongPollServer struct {
Key string
Server string
Ts int
httpClient *http.Client
Logger *log.Logger
intSize int
listener CallbackEventListener
stop chan bool
api *Api
GroupId int
}
type BotLongPollResponse struct {
Ts json.Number `json:"ts,Number"`
Updates []CallbackEvent `json:"updates"`
Failed int `json:"failed"`
MinVersion int `json:"min_version"`
MaxVersion int `json:"nax_version"`
}
func GetBotLongPollServer(api *Api, logger *log.Logger, groupId int) *BotLongPollServer {
return &BotLongPollServer{
api: api,
stop: make(chan bool, 1),
Logger: logger,
GroupId: groupId,
Ts: 0,
}
}
func (s *BotLongPollServer) SetListener(listener CallbackEventListener) {
s.listener = listener
}
func (s *BotLongPollServer) Listen(listener CallbackEventListener) error {
s.SetListener(listener)
return s.Start()
}
func (s *BotLongPollServer) getUpdates() (*BotLongPollResponse, error) {
path := s.Server + "?act=a_check&key=" + s.Key + "&ts=" + strconv.Itoa(s.Ts) + "&wait=80"
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(req.Context(), 300*time.Second)
defer cancel()
req = req.WithContext(ctx)
if s.httpClient == nil {
s.httpClient = s.getHttpClient()
}
requestError := make(chan error, 1)
requestResponse := make(chan *http.Response, 1)
go func() {
res, err := s.httpClient.Do(req)
if err != nil {
requestError <- err
} else {
requestResponse <- res
}
}()
for {
select {
case <-s.stop:
cancel()
return nil, &LPCloseError{}
case err := <-requestError:
return nil, err
case res := <-requestResponse:
data, err := ioutil.ReadAll(res.Body)
_ = res.Body.Close()
if err != nil {
return nil, &TransportError{
string(s.Server),
P{
"ts": strconv.Itoa(s.Ts),
"key": s.Key,
},
data,
TransportExternalData(res.Header),
nil,
}
}
response := BotLongPollResponse{}
err = json.Unmarshal(data, &response)
if err != nil {
return nil, errors.New("BotLongPoll json decode error: " + err.Error() + " on string: " + string(data))
}
if response.Failed == 0 && (response.Ts == "" || response.Ts == "0") {
return nil, errors.New("BotLongPoll bad response: " + string(data))
}
return &response, nil
}
}
}
func (s *BotLongPollServer) up(updateTs bool) error {
data, err := s.api.run(
"groups.getLongPollServer",
P{
"group_id": strconv.Itoa(s.GroupId),
}, 0)
if err != nil {
return err
}
var e error
if updateTs {
s.Ts, e = data.GetInt("ts")
}
s.Key, e = data.GetString("key")
s.Server, e = data.GetString("server")
if e != nil {
return e
}
return nil
}
func (s *BotLongPollServer) Stop() {
s.stop <- true
}
func (s *BotLongPollServer) Start() error {
err := s.up(s.Ts == 0)
if err != nil {
return err
}
for {
updates, err := s.getUpdates()
if err != nil {
if err.Error() == LongPollClosed {
return nil
}
s.onError(err)
} else if updates.Failed == 0 {
newTs, err := strconv.Atoi(string(updates.Ts))
if err != nil {
return errors.New("Cant get new ts from updates, ints not INT: " + err.Error())
}
if newTs > s.Ts {
s.Ts = newTs
}
s.onUpdate(updates.Updates)
} else if updates.Failed == 1 {
newTs, err := strconv.Atoi(string(updates.Ts))
if err != nil {
return errors.New("Cant get new ts from updates, ints not INT: " + err.Error())
}
s.onErrorS(fmt.Sprintf("failed: 1, new ts %d old ts %d", newTs, s.Ts))
s.Ts = newTs
} else if updates.Failed == 2 {
s.onErrorS("failed: 2")
err := s.up(false)
if err != nil {
return err
}
} else if updates.Failed == 3 {
s.onErrorS("failed: 3")
err := s.up(true)
if err != nil {
return err
}
} else if updates.Failed == 4 {
return errors.New("Invalid version min:" + strconv.Itoa(updates.MinVersion) + " max:" + strconv.Itoa(updates.MaxVersion))
}
select {
case <-s.stop:
return nil
default:
}
}
}
func (s *BotLongPollServer) onError(err error) {
s.onErrorS(printError(err))
}
func (s *BotLongPollServer) onErrorS(msg string) {
if s.Logger != nil {
s.Logger.Println(s.GetName() + ": " + msg)
}
}
func (s *BotLongPollServer) onUpdate(updates []CallbackEvent) {
for _, upd := range updates {
s.listener(&upd)
}
}
func (s *BotLongPollServer) getHttpClient() *http.Client {
c := http.DefaultClient
c.Timeout = 300 * time.Second
return c
}
func (s *BotLongPollServer) GetName() string {
return "BotLongPoll"
}