-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
media.go
171 lines (144 loc) · 4.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
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
package message
import (
"context"
"golang.org/x/xerrors"
"github.com/gotd/td/telegram/message/entity"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/tg"
)
func performTextOptions(media *tg.InputSingleMedia, opts []StyledTextOption) error {
if len(opts) > 0 {
tb := entity.Builder{}
if err := styling.Perform(&tb, opts...); err != nil {
return err
}
media.Message, media.Entities = tb.Complete()
}
return nil
}
// Media adds given media attachment to message.
func Media(media tg.InputMediaClass, caption ...StyledTextOption) MediaOption {
return mediaOptionFunc(func(ctx context.Context, b *multiMediaBuilder) error {
singleMedia := tg.InputSingleMedia{
Media: media,
}
if err := performTextOptions(&singleMedia, caption); err != nil {
return err
}
b.media = append(b.media, singleMedia)
return nil
})
}
// Album sends message with multiple media attachments.
func (b *Builder) Album(ctx context.Context, media MultiMediaOption, album ...MultiMediaOption) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
if len(album) < 1 {
return b.Media(ctx, media)
}
mb, err := b.applyMedia(ctx, p, media, album...)
if err != nil {
return nil, err
}
upd, err := b.sender.sendMultiMedia(ctx, &tg.MessagesSendMultiMediaRequest{
Silent: b.silent,
Background: b.background,
ClearDraft: b.clearDraft,
Peer: p,
ReplyToMsgID: b.replyToMsgID,
MultiMedia: mb,
ScheduleDate: b.scheduleDate,
})
if err != nil {
return nil, xerrors.Errorf("send album: %w", err)
}
return upd, nil
}
func (b *Builder) applyMedia(
ctx context.Context,
p tg.InputPeerClass,
media MultiMediaOption, album ...MultiMediaOption,
) ([]tg.InputSingleMedia, error) {
mb := multiMediaBuilder{
sender: b.sender,
peer: p,
media: make([]tg.InputSingleMedia, 0, len(album)+1),
}
if err := media.applyMulti(ctx, &mb); err != nil {
return nil, xerrors.Errorf("media first option: %w", err)
}
if len(album) > 0 {
for i, opt := range album {
if err := opt.applyMulti(ctx, &mb); err != nil {
return nil, xerrors.Errorf("media option %d: %w", i+2, err)
}
}
}
return mb.media, nil
}
// Media sends message with media attachment.
func (b *Builder) Media(ctx context.Context, media MediaOption) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
attachment, err := b.applySingleMedia(ctx, p, media)
if err != nil {
return nil, err
}
upd, err := b.sender.sendMedia(ctx, &tg.MessagesSendMediaRequest{
Silent: b.silent,
Background: b.background,
ClearDraft: b.clearDraft,
Peer: p,
ReplyToMsgID: b.replyToMsgID,
Media: attachment.Media,
Message: attachment.Message,
ReplyMarkup: b.replyMarkup,
Entities: attachment.Entities,
ScheduleDate: b.scheduleDate,
})
if err != nil {
return nil, xerrors.Errorf("send media: %w", err)
}
return upd, nil
}
func (b *Builder) applySingleMedia(
ctx context.Context,
p tg.InputPeerClass,
media MediaOption,
) (tg.InputSingleMedia, error) {
mb := multiMediaBuilder{
sender: b.sender,
peer: p,
media: make([]tg.InputSingleMedia, 0, 1),
}
if err := media.apply(ctx, &mb); err != nil {
return tg.InputSingleMedia{}, xerrors.Errorf("media first option: %w", err)
}
return mb.media[0], nil
}
// UploadMedia uses messages.uploadMedia to upload a file and associate it to
// a chat (without actually sending it to the chat).
//
// See https://core.telegram.org/method/messages.uploadMedia.
func (b *Builder) UploadMedia(ctx context.Context, media MediaOption) (tg.MessageMediaClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
attachment, err := b.applySingleMedia(ctx, p, media)
if err != nil {
return nil, err
}
r, err := b.sender.uploadMedia(ctx, &tg.MessagesUploadMediaRequest{
Peer: p,
Media: attachment.Media,
})
if err != nil {
return nil, xerrors.Errorf("upload media: %w", err)
}
return r, nil
}