forked from himidori/golang-vk-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
353 lines (298 loc) · 9.93 KB
/
message.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
package vkapi
import (
"encoding/json"
"net/url"
"strconv"
"strings"
)
const (
ActivityTypeTyping = "typing"
ActivityTypeAudioMsg = "audiomessage"
)
type Dialog struct {
Count int `json:"count"`
Messages []*Item `json:"items"`
}
type Message struct {
Count int `json:"count"`
Messages []*DialogMessage `json:"items"`
}
type PersonalMessages struct {
Count int `json:"count"`
Messages []*PersonalMessage `json:"items"`
Profiles []*User `json:"profiles"`
}
type Item struct {
Message *DialogMessage `json:"message"`
InRead int `json:"in_read"`
OutRead int `json:"out_read"`
}
type PersonalMessage struct {
Id int `json:"id"`
Date int `json:"date"`
PeerId int `json:"peer_id"`
FromId int `json:"from_id"`
Text string `json:"text"`
RandomId int `json:"random_id"`
Ref string `json:"ref"`
RefSource string `json:"RefSource"`
Attachments []*MessageAttachment `json:"attachment"`
Important bool `json:"important"`
geo Geo `json:"geo"`
Payload string `json:"payload"`
ForwardedMessages []*ForwardedMessage `json:"fwd_message"`
ReplyMessage *ForwardedMessage `json:"reply_message"`
ChatAction ChatAction `json:"action"`
AdminAuthorId int `json:"admin_author_id"`
ConvMsgId int `json:"conversation_message_id"`
IsCropped bool `json:"is_cropped"`
MembersCount int `json:"members_count"`
UpdateTime int `json:"update_time"`
WasListened int `json:"was_listened"`
PinnedAt int `json:"pinned_at"`
MessageTag string `json:"message_tag"`
}
type DialogMessage struct {
MID int `json:"id"`
Date int64 `json:"date"`
Out int `json:"out"`
UID int `json:"user_id"`
ReadState int `json:"read_state"`
Title string `json:"title"`
Body string `json:"body"`
RandomID int `json:"random_id"`
ChatID int64 `json:"chat_id"`
ChatActive string `json:"chat_active"`
PushSettings *Push `json:"push_settings"`
UsersCount int `json:"users_count"`
AdminID int `json:"admin_id"`
Photo50 string `json:"photo_50"`
Photo100 string `json:"photo_100"`
Photo200 string `json:"photo_200"`
ForwardedMessages []*ForwardedMessage `json:"fwd_messages"`
Attachments []*MessageAttachment `json:"attachments"`
}
type Push struct {
Sound int `json:"sound"`
DisabledUntil int64 `json:"disabled_until"`
}
type ForwardedMessage struct {
UID int `json:"user_id"`
Date int64 `json:"date"`
Body string `json:"body"`
Attachments []*MessageAttachment `json:"attachments"`
ForwardedMessages []*ForwardedMessage `json:"fwd_messages"`
}
type MessageAttachment struct {
Type string `json:"type"`
Audio *AudioAttachment `json:"audio"`
Video *VideoAttachment `json:"video"`
Photo *PhotoAttachment `json:"photo"`
Document *DocAttachment `json:"doc"`
Link *LinkAttachment `json:"link"`
Wall *WallPost `json:"wall"`
Sticker *StickerAttachment `json:"sticker"`
}
type StickerAttachment struct {
ID int `json:"id"`
ProductID int `json:"product_id"`
Photo64 string `json:"photo_64"`
Photo128 string `json:"photo_128"`
Photo256 string `json:"photo_256"`
Photo352 string `json:"photo_352"`
Photo512 string `json:"photo_512"`
Width int `json:"width"`
Height int `json:"height"`
}
type HistoryAttachment struct {
Attachments []HistoryAttachmentItem `json:"items"`
NextFrom string `json:"next_from"`
}
type HistoryAttachmentItem struct {
MID int `json:"message_id"`
Attachment *MessageAttachment `json:"attachment"`
}
type AudioAttachment struct {
ID int `json:"id"`
OwnerID int `json:"owner_id"`
Artist string `json:"artist"`
Title string `json:"title"`
Duration int `json:"duration"`
URL string `json:"url"`
Performer string `json:"performer"`
}
type LinkAttachment struct {
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"description"`
Target string `json:"target"`
}
type Keyboard struct {
OneTime bool `json:"one_time"`
Buttons [][]Button `json:"buttons"`
Inline bool `json:"inline"`
}
type Button struct {
Action map[string]string `json:"action"`
Color string `json:"color"`
}
type ChatAction struct {
Type string `json:"type"`
MemberId int64 `json:"member_id"`
Text string `json:"text"`
Email string `json:"email"`
Photo struct {
Photo50 string `json:"photo_50"`
Photo100 string `json:"photo_100"`
Photo200 string `json:"photo_200"`
}
}
type Geo struct {
Type string `json:"type"`
Coordinates *[]Coordinate `json:"coordinates"`
Place Place `json:"place"`
Showmap int `json:"showmap"`
}
type Coordinate struct {
Latitude float32 `json:"latitude"`
Longitude float32 `json:"longitude"`
}
type Place struct {
Id int `json:"id"`
Title string `json:"title"`
Latitude float32 `json:"latitude"`
Longitude float32 `json:"longitude"`
Created int32 `json:"created"`
Icon string `json:"icon"`
Country string `json:"country"`
City string `json:"city"`
}
func (client *VKClient) DialogsGet(count int, params url.Values) (*Dialog, error) {
if params == nil {
params = url.Values{}
}
params.Add("count", strconv.Itoa(count))
resp, err := client.MakeRequest("messages.getDialogs", params)
if err != nil {
return nil, err
}
var dialog *Dialog
json.Unmarshal(resp.Response, &dialog)
return dialog, nil
}
func (client *VKClient) GetHistoryAttachments(peerID int, mediaType string, count int, params url.Values) (*HistoryAttachment, error) {
if params == nil {
params = url.Values{}
}
params.Add("count", strconv.Itoa(count))
params.Add("media_type", mediaType)
params.Add("peer_id", strconv.Itoa(peerID))
resp, err := client.MakeRequest("messages.getHistoryAttachments", params)
if err != nil {
return nil, err
}
var att *HistoryAttachment
json.Unmarshal(resp.Response, &att)
return att, nil
}
func (client *VKClient) MessagesGet(count int, chatID int, isDialog bool, params url.Values) (int, []*DialogMessage, error) {
if params == nil {
params = url.Values{}
}
if isDialog {
chatID += 2000000000
}
params.Add("user_id", strconv.Itoa(chatID))
params.Add("count", strconv.Itoa(count))
resp, err := client.MakeRequest("messages.getHistory", params)
if err != nil {
return 0, nil, err
}
var message *Message
json.Unmarshal(resp.Response, &message)
return message.Count, message.Messages, nil
}
func (client *VKClient) MessagesGetByID(message_ids []int, params url.Values) (int, []*DialogMessage, error) {
if params == nil {
params = url.Values{}
}
s := ArrayToStr(message_ids)
params.Add("message_ids", s)
resp, err := client.MakeRequest("messages.getById", params)
if err != nil {
return 0, nil, err
}
var message *Message
json.Unmarshal(resp.Response, &message)
return message.Count, message.Messages, nil
}
func (client *VKClient) MessagesSend(peerOrDomain interface{}, message string, params url.Values) (APIResponse, error) {
if params == nil {
params = url.Values{}
}
params.Add("message", message)
switch peerOrDomain.(type) {
case int:
params.Add("peer_id", strconv.Itoa(peerOrDomain.(int)))
case string:
params.Add("domain", peerOrDomain.(string))
}
resp, err := client.MakeRequest("messages.send", params)
if err != nil {
return resp, err
}
return resp, nil
}
func (client *VKClient) MessagesDelete(ids []int, spam int, deleteForAll int) (int, error) {
params := url.Values{}
s := ArrayToStr(ids)
params.Add("message_ids", s)
params.Add("spam", strconv.Itoa(spam))
params.Add("delete_for_all", strconv.Itoa(deleteForAll))
resp, err := client.MakeRequest("messages.delete", params)
if err != nil {
return 0, err
}
delCount := 0
var idMap map[string]int
reader := strings.NewReader(string(resp.Response))
err = json.NewDecoder(reader).Decode(&idMap)
if err != nil {
return 0, err
}
for _, v := range idMap {
if v == 1 {
delCount++
}
}
return delCount, nil
}
func (client *VKClient) MessagesSetActivity(user int, params url.Values) error {
if params == nil {
params = url.Values{}
}
params.Add("user_id", strconv.Itoa(user))
_, err := client.MakeRequest("messages.setActivity", params)
if err != nil {
return err
}
return nil
}
func (client *VKClient) GetByConversationMessageId(peerId int, conversationMessageIds []int, extended bool, fields []string, groupId int) (int, []*PersonalMessage, []*User, error) {
params := url.Values{}
ids := ArrayToStr(conversationMessageIds)
params.Add("peer_id", strconv.Itoa(peerId))
params.Add("conversation_message_ids", ids)
params.Add("extended", strconv.Itoa(BoolToInt(extended)))
if len(fields) > 0 {
params.Add("fields", strings.Join(fields, ","))
}
params.Add("group_id", strconv.Itoa(groupId))
resp, err := client.MakeRequest("messages.getByConversationMessageId", params)
if err != nil {
return 0, nil, nil, err
}
var message *PersonalMessages
json.Unmarshal(resp.Response, &message)
return message.Count, message.Messages, message.Profiles, nil
}