-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.go
181 lines (141 loc) Β· 4.72 KB
/
start.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
package longpoll
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/botscommunity/vkgo/internal/pkg/consts"
"github.com/botscommunity/vkgo/internal/pkg/responses"
communityStates "github.com/botscommunity/vkgo/longpoll/community-states"
userStates "github.com/botscommunity/vkgo/longpoll/user-states"
"github.com/botscommunity/vkgo/update"
)
func Start(properties ...any) *responses.Error {
lp, err := New(properties...)
if err != nil {
return err
}
return lp.Start()
}
func (lp *LongPoll) Start() *responses.Error {
if lp.Bot == nil {
return responses.NewInternalError(ErrSessionClosed)
}
if lp.Session.Error != nil {
return lp.Session.Error
}
updates := update.Updates{}
switch lp.BotType {
case consts.CommunityBotType:
for {
if _, err := communityUpdates(lp, updates); err != nil {
return err
}
}
case consts.UserBotType:
for {
if _, err := userUpdates(lp, updates); err != nil {
return err
}
}
}
return nil
}
func communityUpdates(lp *LongPoll, updates update.Updates) (update.Updates, *responses.Error) {
if lp.Session.Server == "" {
return update.Updates{}, responses.NewInternalError(ErrSessionClosed)
}
link := fmt.Sprintf("%s?act=a_check&key=%s&ts=%v&wait=%d&mode=%d&version=%f", lp.Session.Server, lp.Session.Key, lp.Session.TS, lp.Timeout, lp.Mode, lp.Bot.Version)
request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, link, nil)
if err != nil {
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("GroupLongPoll NewRequest error is %s", err.Error()))
}
return updates, responses.NewInternalError(err)
}
response, err := lp.Bot.HTTPClient.Do(request)
if err != nil {
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("GroupLongPoll Do error is %s", err.Error()))
}
return updates, responses.NewInternalError(err)
}
body, err := io.ReadAll(response.Body)
if err != nil {
if err := response.Body.Close(); err != nil {
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("GroupLongPoll BodyClose error is %s", err.Error()))
}
}
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("GroupLongPoll ReadAll error is %s", err.Error()))
}
return updates, responses.NewInternalError(err)
}
if err := json.Unmarshal(body, &updates); err != nil {
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("GroupLongPoll JSON unmarshal error is %s", err.Error()))
}
return updates, responses.NewInternalError(err)
}
if lp.Bot.Logger != nil {
lp.Bot.Logger.Info(fmt.Sprintf("GroupLongPoll response is %s", string(body)))
}
communityStates.ExecuteState(lp.Bot, lp.Session, updates, *lp.Scenes)
return updates, responses.NewInternalError(err)
}
func userUpdates(lp *LongPoll, updates update.Updates) (update.Updates, *responses.Error) {
if lp.Session.Server == "" {
return update.Updates{}, responses.NewInternalError(ErrSessionClosed)
}
sessionTS := defineSessionTS(lp.Session.TS)
link := fmt.Sprintf("https://%s?act=a_check&key=%s&ts=%+v&wait=%d&mode=%d&version=%d", lp.Session.Server, lp.Session.Key, sessionTS, lp.Timeout, lp.Mode, lp.Session.Version)
request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, link, nil)
if err != nil {
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("UserLongPoll NewRequest error is %s", err.Error()))
}
return updates, responses.NewInternalError(err)
}
response, err := lp.Bot.HTTPClient.Do(request)
if err != nil {
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("UserLongPoll Do error is %s", err.Error()))
}
return updates, responses.NewInternalError(err)
}
body, err := io.ReadAll(response.Body)
if err != nil {
if err := response.Body.Close(); err != nil {
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("UserLongPoll BodyClose error is %s", err.Error()))
}
}
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("UserLongPoll ReadAll error is %s", err.Error()))
}
return updates, responses.NewInternalError(err)
}
var messages userStates.Messages
if err := json.Unmarshal(body, &messages); err != nil {
if lp.Bot.Logger != nil {
lp.Bot.Logger.Error(fmt.Sprintf("UserLongPoll JSON unmarshal error is %s", err.Error()))
}
return updates, responses.NewInternalError(err)
}
if lp.Bot.Logger != nil {
lp.Bot.Logger.Info(fmt.Sprintf("UserLongPoll response is %s", string(body)))
}
updates = userStates.ExecuteState(lp.Bot, lp.Session, messages, *lp.Scenes)
return updates, responses.NewInternalError(err)
}
func defineSessionTS(sessionTS any) string {
switch offset := sessionTS.(type) {
case uint32, uint64:
return fmt.Sprintf("%d", offset)
case float32, float64:
return fmt.Sprintf("%f", offset)
}
return ""
}