-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponses.go
368 lines (307 loc) · 11.7 KB
/
responses.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
GVK
Copyright (C) 2023-2024 The GVK Devs
GVK is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
GVK is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package gvk
import (
"encoding/json"
"fmt"
)
type Response interface {
Base() APIError
}
//---------------------------------------------------------------
type APIResponseMessagesSend struct {
Error APIError `json:"error,omitempty"`
Response int `json:"response,omitempty"`
}
func (a APIResponseMessagesSend) Base() APIError {
return a.Error
}
//---------------------------------------------------------------
type APIResponseMessagesEdit struct {
Error APIError `json:"error,omitempty"`
Response int `json:"response,omitempty"`
}
func (a APIResponseMessagesEdit) Base() APIError {
return a.Error
}
// ------------------------------------------------------------------
type APIResponseMessageEventAnswer struct {
Error APIError `json:"error,omitempty"`
Response int `json:"response,omitempty"`
}
func (a APIResponseMessageEventAnswer) Base() APIError {
return a.Error
}
// ------------------------------------------------------------
type APIResponseUsersGet struct {
Error APIError `json:"error,omitempty"`
Response []User `json:"response,omitempty"`
}
type User struct {
ID int64 `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
ScreenName string `json:"screen_name"` // aka username
Deactivated Deactivated `json:"deactivated"`
IsClosed bool `json:"is_closed"`
CanAccessClosed bool `json:"can_access_closed"`
}
type Deactivated string
const DeactivatedBanned Deactivated = "banned"
const DeactivatedDeleted Deactivated = "deleted"
func (a APIResponseUsersGet) Base() APIError {
return a.Error
}
//---------------------------------------------------------------
type APIResponseGetLongPollServer struct {
Response ResponseGetLongPollServer `json:"response,omitempty"`
Error APIError `json:"error,omitempty"`
}
type ResponseGetLongPollServer struct {
Key string `json:"key,omitempty"`
Server string `json:"server,omitempty"`
Ts string `json:"ts,omitempty"`
}
func (a APIResponseGetLongPollServer) Base() APIError {
return a.Error
}
// ---------------------------------------------------------------
type APIResponseSetLongPollSettings struct {
Response ResponseSetLongPollSettings `json:"response,omitempty"`
Error APIError `json:"error,omitempty"`
}
type ResponseSetLongPollSettings struct {
}
func (a APIResponseSetLongPollSettings) Base() APIError {
return a.Error
}
//---------------------------------------------------------------
type APIResponseGroupsIsMember struct {
Response int64 `json:"response,omitempty"`
Error APIError `json:"error,omitempty"`
}
func (a APIResponseGroupsIsMember) Base() APIError {
return a.Error
}
// ------ https://dev.vk.com/ru/method/wall.post#Результат -------------
type APIResponseWallPost struct {
Error APIError `json:"error,omitempty"`
Response ResponseWallPost `json:"response,omitempty"`
}
type ResponseWallPost struct {
PostID int64 `json:"post_id"`
}
func (a APIResponseWallPost) Base() APIError {
return a.Error
}
// ------ https://dev.vk.com/ru/method/wall.edit -------------
type APIResponseWallEdit struct {
Error APIError `json:"error,omitempty"`
Response ResponseWallEdit `json:"response,omitempty"`
}
type ResponseWallEdit struct {
PostID int64 `json:"post_id"`
}
func (a APIResponseWallEdit) Base() APIError {
return a.Error
}
// -------------------------
type APIResponseUpdate struct {
Ts string `json:"ts,omitempty"`
//Updates []*Update `json:"updates,omitempty"`
Updates RawUpdates `json:"updates,omitempty"`
Failed int64 `json:"failed,omitempty"` // ошибка
Error APIError `json:"error,omitempty"`
}
func (a APIResponseUpdate) Base() APIError {
return a.Error
}
type TypeSwitch struct {
Type EventType `json:"type"`
GroupID int `json:"group_id"`
EventID string `json:"event_id"`
V string `json:"v"`
}
type RawUpdates []json.RawMessage
type Updates []*Update
type Update struct {
TypeSwitch
Object Object `json:"object"`
}
type Object struct {
*MessageNew
*MessageReply
*MessageEvent
*MessageEdit
*MessageAllow
*MessageDeny
*MessageTypingState
*WallPostNew
// TODO: добавить остальные объекты Events
}
//---------------------------------------------------------------
// ChatID returns the ID of the chat the update is coming from.
func (u *Update) ChatID() int64 {
switch {
case u.Object.MessageNew != nil:
return u.Object.MessageNew.Message.FromID
case u.Object.MessageEdit != nil:
return u.Object.MessageEdit.FromID
case u.Object.MessageAllow != nil:
return u.Object.MessageAllow.UserID
case u.Object.MessageDeny != nil:
return u.Object.MessageDeny.UserID
case u.Object.MessageTypingState != nil:
return u.Object.MessageTypingState.FromID
case u.Object.MessageEvent != nil:
return u.Object.MessageEvent.UserID
case u.Object.WallPostNew != nil:
return u.Object.WallPostNew.FromID
//case u.ChannelPost != nil:
// return u.ChannelPost.Chat.ID
//case u.EditedChannelPost != nil:
// return u.EditedChannelPost.Chat.ID
//case u.InlineQuery != nil:
// return u.InlineQuery.From.ID
//case u.ChosenInlineResult != nil:
// return u.ChosenInlineResult.From.ID
//case u.CallbackQuery != nil:
// return u.CallbackQuery.Message.Chat.ID
//case u.ShippingQuery != nil:
// return u.ShippingQuery.From.ID
//case u.PreCheckoutQuery != nil:
// return u.PreCheckoutQuery.From.ID
//case u.MyChatMember != nil:
// return u.MyChatMember.Chat.ID
//case u.ChatMember != nil:
// return u.ChatMember.Chat.ID
//case u.ChatJoinRequest != nil:
// return u.ChatJoinRequest.Chat.ID
default:
return 0
}
}
func (a RawUpdates) UnmarshalCustom() (Updates, error) {
var updates Updates
for _, row := range a {
update := Update{}
if err := json.Unmarshal(row, &update); err != nil {
return updates, err
}
updates = append(updates, &update)
}
return updates, nil
}
func (u *Update) UnmarshalJSON(data []byte) error {
var temp struct {
TypeSwitch
Object json.RawMessage `json:"object"`
}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
u.TypeSwitch = temp.TypeSwitch
switch u.Type {
case EventMessageNew:
u.Object.MessageNew = &MessageNew{}
return json.Unmarshal(temp.Object, u.Object.MessageNew)
case EventMessageReply:
u.Object.MessageReply = &MessageReply{}
return json.Unmarshal(temp.Object, u.Object.MessageReply)
case EventMessageEdit:
u.Object.MessageEdit = &MessageEdit{}
return json.Unmarshal(temp.Object, u.Object.MessageEdit)
case EventMessageAllow:
u.Object.MessageAllow = &MessageAllow{}
return json.Unmarshal(temp.Object, u.Object.MessageAllow)
case EventMessageEvent:
u.Object.MessageEvent = &MessageEvent{}
return json.Unmarshal(temp.Object, u.Object.MessageEvent)
case EventMessageDeny:
u.Object.MessageDeny = &MessageDeny{}
return json.Unmarshal(temp.Object, u.Object.MessageDeny)
case EventMessageTypingState:
u.Object.MessageTypingState = &MessageTypingState{}
return json.Unmarshal(temp.Object, u.Object.MessageTypingState)
//TODO: add another events
case EventWallPostNew:
u.Object.WallPostNew = &WallPostNew{}
return json.Unmarshal(temp.Object, u.Object.WallPostNew)
//TODO: add another events
default:
return fmt.Errorf("unrecognized type value %q", u.Type)
}
}
// EventType type.
type EventType string
// EventType list.
const (
EventConfirmation = "confirmation"
EventMessageNew EventType = "message_new"
EventMessageReply EventType = "message_reply"
EventMessageEdit = "message_edit"
EventMessageAllow = "message_allow"
EventMessageDeny = "message_deny"
EventMessageTypingState = "message_typing_state"
EventMessageEvent EventType = "message_event"
EventPhotoNew = "photo_new"
EventPhotoCommentNew = "photo_comment_new"
EventPhotoCommentEdit = "photo_comment_edit"
EventPhotoCommentRestore = "photo_comment_restore"
EventPhotoCommentDelete = "photo_comment_delete"
EventAudioNew = "audio_new"
EventVideoNew = "video_new"
EventVideoCommentNew = "video_comment_new"
EventVideoCommentEdit = "video_comment_edit"
EventVideoCommentRestore = "video_comment_restore"
EventVideoCommentDelete = "video_comment_delete"
EventWallPostNew EventType = "wall_post_new"
EventWallRepost = "wall_repost"
EventWallReplyNew = "wall_reply_new"
EventWallReplyEdit = "wall_reply_edit"
EventWallReplyRestore = "wall_reply_restore"
EventWallReplyDelete = "wall_reply_delete"
EventBoardPostNew = "board_post_new"
EventBoardPostEdit = "board_post_edit"
EventBoardPostRestore = "board_post_restore"
EventBoardPostDelete = "board_post_delete"
EventMarketCommentNew = "market_comment_new"
EventMarketCommentEdit = "market_comment_edit"
EventMarketCommentRestore = "market_comment_restore"
EventMarketCommentDelete = "market_comment_delete"
EventMarketOrderNew = "market_order_new"
EventMarketOrderEdit = "market_order_edit"
EventGroupLeave = "group_leave"
EventGroupJoin = "group_join"
EventUserBlock = "user_block"
EventUserUnblock = "user_unblock"
EventPollVoteNew = "poll_vote_new"
EventGroupOfficersEdit = "group_officers_edit"
EventGroupChangeSettings = "group_change_settings"
EventGroupChangePhoto = "group_change_photo"
EventVkpayTransaction = "vkpay_transaction"
EventLeadFormsNew = "lead_forms_new"
EventAppPayload = "app_payload"
EventMessageRead = "message_read"
EventLikeAdd = "like_add"
EventLikeRemove = "like_remove"
EventDonutSubscriptionCreate = "donut_subscription_create"
EventDonutSubscriptionProlonged = "donut_subscription_prolonged"
EventDonutSubscriptionExpired = "donut_subscription_expired"
EventDonutSubscriptionCancelled = "donut_subscription_cancelled"
EventDonutSubscriptionPriceChanged = "donut_subscription_price_changed"
EventDonutMoneyWithdraw = "donut_money_withdraw"
EventDonutMoneyWithdrawError = "donut_money_withdraw_error"
)