/
media.go
137 lines (116 loc) · 3.02 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
package botkit
import (
"io"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
type Media interface {
toChattable(chatID int64, replyID int) tgbotapi.Chattable
toInputMedia() any
}
type MediaSource interface {
toRequestFileData() tgbotapi.RequestFileData
}
type wrapperMediaSource struct {
data tgbotapi.RequestFileData
}
type BaseMedia struct {
File MediaSource
Thumb MediaSource
Caption string
}
type Photo struct {
BaseMedia
}
type Video struct {
BaseMedia
Duration int
SupportsStreaming bool
}
type Audio struct {
BaseMedia
Duration int
Performer string
Title string
}
func FileSource(name string, r io.Reader) MediaSource {
return &wrapperMediaSource{data: tgbotapi.FileReader{Name: name, Reader: r}}
}
func URLSource(url string) MediaSource {
return &wrapperMediaSource{data: tgbotapi.FileURL(url)}
}
func NewPhoto(file MediaSource) *Photo {
return &Photo{
BaseMedia: BaseMedia{File: file},
}
}
func (photo *Photo) toChattable(chatID int64, replyID int) tgbotapi.Chattable {
cfg := tgbotapi.NewPhoto(chatID, photo.File.toRequestFileData())
if photo.Thumb != nil {
cfg.Thumb = photo.Thumb.toRequestFileData()
}
cfg.Caption = photo.Caption
cfg.ReplyToMessageID = replyID
return cfg
}
func (photo *Photo) toInputMedia() any {
cfg := tgbotapi.NewInputMediaPhoto(photo.File.toRequestFileData())
cfg.Caption = photo.Caption
return cfg
}
func NewVideo(file MediaSource) *Video {
return &Video{
BaseMedia: BaseMedia{File: file},
}
}
func (video *Video) toChattable(chatID int64, replyID int) tgbotapi.Chattable {
cfg := tgbotapi.NewVideo(chatID, video.File.toRequestFileData())
if video.Thumb != nil {
cfg.Thumb = video.Thumb.toRequestFileData()
}
cfg.Caption = video.Caption
cfg.ReplyToMessageID = replyID
cfg.Duration = video.Duration
cfg.SupportsStreaming = video.SupportsStreaming
return cfg
}
func (video *Video) toInputMedia() any {
cfg := tgbotapi.NewInputMediaVideo(video.File.toRequestFileData())
if video.Thumb != nil {
cfg.Thumb = video.Thumb.toRequestFileData()
}
cfg.Caption = video.Caption
cfg.Duration = video.Duration
cfg.SupportsStreaming = video.SupportsStreaming
return cfg
}
func NewAudio(file MediaSource) *Audio {
return &Audio{
BaseMedia: BaseMedia{File: file},
}
}
func (audio *Audio) toChattable(chatID int64, replyID int) tgbotapi.Chattable {
cfg := tgbotapi.NewAudio(chatID, audio.File.toRequestFileData())
if audio.Thumb != nil {
cfg.Thumb = audio.Thumb.toRequestFileData()
}
cfg.Caption = audio.Caption
cfg.ReplyToMessageID = replyID
cfg.Duration = audio.Duration
cfg.Performer = audio.Performer
cfg.Title = audio.Title
return cfg
}
func (audio *Audio) toInputMedia() any {
cfg := tgbotapi.NewInputMediaAudio(audio.File.toRequestFileData())
if audio.Thumb != nil {
cfg.Thumb = audio.Thumb.toRequestFileData()
}
cfg.Caption = audio.Caption
cfg.Duration = audio.Duration
cfg.Performer = audio.Performer
cfg.Title = audio.Title
return cfg
}
func (w wrapperMediaSource) toRequestFileData() tgbotapi.RequestFileData {
return w.data
}