-
Notifications
You must be signed in to change notification settings - Fork 469
/
media.go
437 lines (362 loc) · 11 KB
/
media.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package telebot
import (
"encoding/json"
"math"
)
// Media is a generic type for all kinds of media that includes File.
type Media interface {
// MediaType returns string-represented media type.
MediaType() string
// MediaFile returns a pointer to the media file.
MediaFile() *File
}
// InputMedia represents a composite InputMedia struct that is
// used by Telebot in sending and editing media methods.
type InputMedia struct {
Type string `json:"type"`
Media string `json:"media"`
Caption string `json:"caption"`
Thumbnail string `json:"thumbnail,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
Entities Entities `json:"caption_entities,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Duration int `json:"duration,omitempty"`
Title string `json:"title,omitempty"`
Performer string `json:"performer,omitempty"`
Streaming bool `json:"supports_streaming,omitempty"`
DisableTypeDetection bool `json:"disable_content_type_detection,omitempty"`
CaptionAbove bool `json:"show_caption_above_media,omitempty"`
HasSpoiler bool `json:"has_spoiler,omitempty"`
}
// Inputtable is a generic type for all kinds of media you
// can put into an album.
type Inputtable interface {
Media
// InputMedia returns already marshalled InputMedia type
// ready to be used in sending and editing media methods.
InputMedia() InputMedia
}
// Album lets you group multiple media into a single message.
type Album []Inputtable
func (a Album) SetCaption(caption string) {
if len(a) < 1 {
return
}
switch a[0].MediaType() {
case "audio":
a[0].(*Audio).Caption = caption
case "video":
a[0].(*Video).Caption = caption
case "document":
a[0].(*Document).Caption = caption
case "photo":
a[0].(*Photo).Caption = caption
case "animation":
a[0].(*Animation).Caption = caption
}
}
// Photo object represents a single photo file.
type Photo struct {
File
// (Optional)
Width int `json:"width"`
Height int `json:"height"`
Caption string `json:"caption,omitempty"`
HasSpoiler bool `json:"has_spoiler,omitempty"`
CaptionAbove bool `json:"show_caption_above_media,omitempty"`
}
type photoSize struct {
File
Width int `json:"width"`
Height int `json:"height"`
Caption string `json:"caption,omitempty"`
}
func (p *Photo) MediaType() string {
return "photo"
}
func (p *Photo) MediaFile() *File {
return &p.File
}
func (p *Photo) InputMedia() InputMedia {
return InputMedia{
Type: p.MediaType(),
Caption: p.Caption,
HasSpoiler: p.HasSpoiler,
CaptionAbove: p.CaptionAbove,
}
}
func (p *Photo) Paid() bool {
return true
}
// UnmarshalJSON is custom unmarshaller required to abstract
// away the hassle of treating different thumbnail sizes.
// Instead, Telebot chooses the hi-res one and just sticks to it.
//
// I really do find it a beautiful solution.
func (p *Photo) UnmarshalJSON(data []byte) error {
var hq photoSize
if data[0] == '{' {
if err := json.Unmarshal(data, &hq); err != nil {
return err
}
} else {
var sizes []photoSize
if err := json.Unmarshal(data, &sizes); err != nil {
return err
}
hq = sizes[len(sizes)-1]
}
p.File = hq.File
p.Width = hq.Width
p.Height = hq.Height
p.Caption = hq.Caption
return nil
}
// Audio object represents an audio file.
type Audio struct {
File
Duration int `json:"duration,omitempty"`
// (Optional)
Caption string `json:"caption,omitempty"`
Thumbnail *Photo `json:"thumbnail,omitempty"`
Title string `json:"title,omitempty"`
Performer string `json:"performer,omitempty"`
MIME string `json:"mime_type,omitempty"`
FileName string `json:"file_name,omitempty"`
}
func (a *Audio) MediaType() string {
return "audio"
}
func (a *Audio) MediaFile() *File {
a.fileName = a.FileName
return &a.File
}
func (a *Audio) InputMedia() InputMedia {
return InputMedia{
Type: a.MediaType(),
Caption: a.Caption,
Duration: a.Duration,
Title: a.Title,
Performer: a.Performer,
}
}
// Document object represents a general file (as opposed to Photo or Audio).
// Telegram users can send files of any type of up to 1.5 GB in size.
type Document struct {
File
// (Optional)
Thumbnail *Photo `json:"thumbnail,omitempty"`
Caption string `json:"caption,omitempty"`
MIME string `json:"mime_type"`
FileName string `json:"file_name,omitempty"`
DisableTypeDetection bool `json:"disable_content_type_detection,omitempty"`
}
func (d *Document) MediaType() string {
return "document"
}
func (d *Document) MediaFile() *File {
d.fileName = d.FileName
return &d.File
}
func (d *Document) InputMedia() InputMedia {
return InputMedia{
Type: d.MediaType(),
Caption: d.Caption,
DisableTypeDetection: d.DisableTypeDetection,
}
}
// Video object represents a video file.
type Video struct {
File
Width int `json:"width"`
Height int `json:"height"`
Duration int `json:"duration,omitempty"`
// (Optional)
Caption string `json:"caption,omitempty"`
Thumbnail *Photo `json:"thumbnail,omitempty"`
Streaming bool `json:"supports_streaming,omitempty"`
MIME string `json:"mime_type,omitempty"`
FileName string `json:"file_name,omitempty"`
HasSpoiler bool `json:"has_spoiler,omitempty"`
CaptionAbove bool `json:"show_caption_above_media,omitempty"`
}
func (v *Video) MediaType() string {
return "video"
}
func (v *Video) MediaFile() *File {
v.fileName = v.FileName
return &v.File
}
func (v *Video) InputMedia() InputMedia {
return InputMedia{
Type: v.MediaType(),
Caption: v.Caption,
Width: v.Width,
Height: v.Height,
Duration: v.Duration,
Streaming: v.Streaming,
HasSpoiler: v.HasSpoiler,
CaptionAbove: v.CaptionAbove,
}
}
func (v *Video) Paid() bool {
return true
}
// Animation object represents a animation file.
type Animation struct {
File
Width int `json:"width"`
Height int `json:"height"`
Duration int `json:"duration,omitempty"`
// (Optional)
Caption string `json:"caption,omitempty"`
Thumbnail *Photo `json:"thumbnail,omitempty"`
MIME string `json:"mime_type,omitempty"`
FileName string `json:"file_name,omitempty"`
HasSpoiler bool `json:"has_spoiler,omitempty"`
CaptionAbove bool `json:"show_caption_above_media,omitempty"`
}
func (a *Animation) MediaType() string {
return "animation"
}
func (a *Animation) MediaFile() *File {
a.fileName = a.FileName
return &a.File
}
func (a *Animation) InputMedia() InputMedia {
return InputMedia{
Type: a.MediaType(),
Caption: a.Caption,
Width: a.Width,
Height: a.Height,
Duration: a.Duration,
HasSpoiler: a.HasSpoiler,
CaptionAbove: a.CaptionAbove,
}
}
// Voice object represents a voice note.
type Voice struct {
File
Duration int `json:"duration"`
// (Optional)
Caption string `json:"caption,omitempty"`
MIME string `json:"mime_type,omitempty"`
}
func (v *Voice) MediaType() string {
return "voice"
}
func (v *Voice) MediaFile() *File {
return &v.File
}
// VideoNote represents a video message.
type VideoNote struct {
File
Duration int `json:"duration"`
// (Optional)
Thumbnail *Photo `json:"thumbnail,omitempty"`
Length int `json:"length,omitempty"`
}
func (v *VideoNote) MediaType() string {
return "videoNote"
}
func (v *VideoNote) MediaFile() *File {
return &v.File
}
// Sticker object represents a WebP image, so-called sticker.
type Sticker struct {
File
Type StickerSetType `json:"type"`
Width int `json:"width"`
Height int `json:"height"`
Animated bool `json:"is_animated"`
Video bool `json:"is_video"`
Thumbnail *Photo `json:"thumbnail"`
Emoji string `json:"emoji"`
SetName string `json:"set_name"`
PremiumAnimation *File `json:"premium_animation"`
MaskPosition *MaskPosition `json:"mask_position"`
CustomEmojiID string `json:"custom_emoji_id"`
Repaint bool `json:"needs_repainting"`
}
func (s *Sticker) MediaType() string {
return "sticker"
}
func (s *Sticker) MediaFile() *File {
return &s.File
}
// Contact object represents a contact to Telegram user.
type Contact struct {
PhoneNumber string `json:"phone_number"`
FirstName string `json:"first_name"`
// (Optional)
LastName string `json:"last_name"`
UserID int64 `json:"user_id,omitempty"`
VCard string `json:"vcard,omitempty"`
}
// LiveForever is an alias for math.MaxInt32.
// Use it for LivePeriod of the Location.
const LiveForever = math.MaxInt32
// Location object represents geographic position.
type Location struct {
Lat float32 `json:"latitude"`
Lng float32 `json:"longitude"`
HorizontalAccuracy *float32 `json:"horizontal_accuracy,omitempty"`
Heading int `json:"heading,omitempty"`
AlertRadius int `json:"proximity_alert_radius,omitempty"`
// Period in seconds for which the location will be updated
// (see Live Locations, should be between 60 and 86400.)
LivePeriod int `json:"live_period,omitempty"`
// (Optional) Unique identifier of the business connection
// on behalf of which the message to be edited was sent
BusinessConnectionID string `json:"business_connection_id,omitempty"`
}
// Venue object represents a venue location with name, address and
// optional foursquare ID.
type Venue struct {
Location Location `json:"location"`
Title string `json:"title"`
Address string `json:"address"`
// (Optional)
FoursquareID string `json:"foursquare_id,omitempty"`
FoursquareType string `json:"foursquare_type,omitempty"`
GooglePlaceID string `json:"google_place_id,omitempty"`
GooglePlaceType string `json:"google_place_type,omitempty"`
}
// Dice object represents a dice with a random value
// from 1 to 6 for currently supported base emoji.
type Dice struct {
Type DiceType `json:"emoji"`
Value int `json:"value"`
}
// DiceType defines dice types.
type DiceType string
var (
Cube = &Dice{Type: "🎲"}
Dart = &Dice{Type: "🎯"}
Ball = &Dice{Type: "🏀"}
Goal = &Dice{Type: "⚽"}
Slot = &Dice{Type: "🎰"}
Bowl = &Dice{Type: "🎳"}
)
// PaidInputtable is a generic type for all kinds of media you
// can put into an album that are paid.
type PaidInputtable interface {
Inputtable
// Paid shows if the media is paid.
Paid() bool
}
// PaidAlbum lets you group multiple paid media into a single message.
type PaidAlbum []PaidInputtable
type PaidMedias struct {
Stars int `json:"star_count"`
PaidMedia []PaidMedia `json:"paid_media"`
}
type PaidMedia struct {
Type string `json:"type"`
Photo *Photo `json:"photo"` // photo
Video *Video `json:"video"` // video
Width int `json:"width"` // preview only
Height int `json:"height"` // preview only
Duration int `json:"duration"` // preview only
}