-
Notifications
You must be signed in to change notification settings - Fork 7
/
attachments.go
233 lines (200 loc) · 5.2 KB
/
attachments.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
package vkapi
import "fmt"
type Attachment struct {
Type string `json:"type"`
AccessKey string `json:"access_key"`
Photo *Photo `json:"photo"`
Audio *Audio `json:"audio"`
Video *Video `json:"video"`
Document *Document `json:"doc"`
Wall *Wall `json:"wall"`
}
type Photo struct {
ID int64 `json:"id"`
AlbumID int64 `json:"album_id"`
OwnerID int64 `json:"owner_id"`
UserID int64 `json:"user_id"`
Text string `json:"text"`
Date int64 `json:"date"`
Sizes *[]Sizes `json:"sizes"`
Photo75 string `json:"photo_75"`
Photo130 string `json:"photo_130"`
Photo604 string `json:"photo_604"`
Photo807 string `json:"photo_807"`
Photo1280 string `json:"photo_1280"`
Photo2560 string `json:"photo_2560"`
Width int `json:"width"`
Height int `json:"height"`
}
type Sizes struct {
Src string `json:"src"`
Width int `json:"width"`
Height int `json:"height"`
Type string `json:"type"`
}
func (p *Photo) GetMaxSizePhoto() string {
if p.Photo2560 != "" {
return p.Photo2560
}
if p.Photo1280 != "" {
return p.Photo1280
}
if p.Photo807 != "" {
return p.Photo807
}
if p.Photo604 != "" {
return p.Photo604
}
if p.Photo130 != "" {
return p.Photo130
}
if p.Photo75 != "" {
return p.Photo75
}
return ""
}
type Audio struct {
ID int64 `json:"id"`
OwnerID int64 `json:"owner_id"`
Artist string `json:"artist"`
Title string `json:"title"`
Duration int64 `json:"duration"`
Url string `json:"url"`
LyricsID int64 `json:"lyrics_id"`
AlbumID int64 `json:"album_id"`
GenreID int64 `json:"genre_id"`
Date int64 `json:"date"`
NoSearch int `json:"no_search"`
}
type Document struct {
ID int64 `json:"id"`
OwnerID int64 `json:"owner_id"`
Title string `json:"title"`
Size int64 `json:"size"`
Ext string `json:"ext"`
Url string `json:"url"`
Date int64 `json:"date"`
Type int `json:"type"`
AccessKey string `json:"access_key"`
Preview struct {
Photo *struct {
Sizes *[]Sizes `json:"sizes"`
} `json:"photo"`
Graffiti *Graffiti `json:"graffiti"`
AudioMsg *AudioMsg `json:"audio_msg"`
} `json:"preview"`
}
func (doc *Document) IsTxt() bool {
return doc.Type == 1
}
func (doc *Document) IsArch() bool {
return doc.Type == 2
}
func (doc *Document) IsGif() bool {
return doc.Type == 3
}
func (doc *Document) IsImages() bool {
return doc.Type == 4
}
func (doc *Document) IsAudio() bool {
return doc.Type == 5
}
func (doc *Document) IsVideo() bool {
return doc.Type == 6
}
func (doc *Document) IsEBooks() bool {
return doc.Type == 7
}
func (doc *Document) IsUnknown() bool {
return doc.Type == 8
}
type Graffiti struct {
Src string `json:"src"`
Width int64 `json:"width"`
Height int64 `json:"height"`
}
type AudioMsg struct {
Duration int64 `json:"duration"`
Waveform []int64 `json:"waveform"`
LinkOgg string `json:"link_ogg"`
LinkMp3 string `json:"link_mp3"`
}
type Video struct {
ID int64 `json:"id"`
OwnerID int64 `json:"owner_id"`
Title string `json:"title"`
Description string `json:"description"`
Duration int64 `json:"duration"`
Photo130 string `json:"photo_130"`
Photo320 string `json:"photo_320"`
Photo640 string `json:"photo_640"`
Photo800 string `json:"photo_800"`
Date int64 `json:"date"`
AddingDate int64 `json:"adding_date"`
Views int64 `json:"views"`
Comments int64 `json:"comments"`
Player string `json:"player"`
AccessKey string `json:"access_key"`
Processing int `json:"processing"`
Live int `json:"live"`
Upcoming int `json:"upcoming"`
}
func (v *Video) GetMaxPreview() string {
if v.Photo800 != "" {
return v.Photo800
}
if v.Photo640 != "" {
return v.Photo640
}
if v.Photo320 != "" {
return v.Photo320
}
if v.Photo130 != "" {
return v.Photo130
}
return ""
}
func (client *Client) AddAttachmentPhoto(file interface{}) string {
server, err := client.GetMessagesUploadServerForPhoto()
if err != nil {
return ""
}
res, err := client.UploadFile(server.UploadURL, "photo", file)
if err != nil {
return ""
}
photo, err := client.SaveMessagesPhoto(res)
if err != nil {
return ""
}
return fmt.Sprintf("photo%d_%d", photo.OwnerID, photo.ID)
}
func (client *Client) AddAttachmentDoc(fieldName string, peerID int64, title string, file interface{}) string {
server, err := client.GetMessagesUploadServerForDoc(fieldName, peerID)
if err != nil {
return ""
}
res, err := client.UploadFile(server.UploadURL, "file", file)
if err != nil {
return ""
}
doc, err := client.SaveMessagesDoc(res.File, title)
if err != nil {
return ""
}
return fmt.Sprintf("doc%d_%d", doc.OwnerID, doc.ID)
}
func (client *Client) SendPhoto(dst Destination, file interface{}) (int64, *Error) {
config := MessageConfig{
Destination: dst,
Attachment: client.AddAttachmentPhoto(file),
}
return client.SendMessage(config)
}
func (client *Client) SendDoc(dst Destination, title string, file interface{}) (int64, *Error) {
config := MessageConfig{
Destination: dst,
Attachment: client.AddAttachmentDoc("doc", dst.GetPeerID(), title, file),
}
return client.SendMessage(config)
}