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
// Copyright 2020 - developers of the `grammers` project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use grammers_tl_types as tl;
use super::{Attribute, Media, Uploaded};
/// Media item to be sent as part of an album.
#[derive(Default)]
pub struct InputMedia {
pub(crate) entities: Vec<tl::enums::MessageEntity>,
pub(crate) reply_to: Option<i32>,
pub(crate) caption: String,
pub(crate) media: Option<tl::enums::InputMedia>,
media_ttl: Option<i32>,
mime_type: Option<String>,
}
impl InputMedia {
/// Creates a new empty media message for input.
pub fn new() -> Self {
Self::default()
}
/// Replaces the plaintext in the message.
///
/// The caller must ensure that formatting entities remain valid for the given text.
/// If you need to update formatting entities, call method [`InputMedia::fmt_entities`].
///
/// <div class="warning">
/// Note that this method does not modify formatting entities, which may break
/// formatting or cause out-of-bounds errors if entities do not match the given text.
/// </div>
pub fn caption<T>(mut self, s: T) -> Self
where
T: Into<String>,
{
self.caption = s.into();
self
}
/// The formatting entities within the caption (such as bold, italics, etc.).
pub fn fmt_entities<I>(mut self, entities: I) -> Self
where
I: IntoIterator<Item = tl::enums::MessageEntity>,
{
self.entities = entities.into_iter().collect();
self
}
/// Builds a new media from the given markdown-formatted string as the
/// caption contents and entities.
///
/// Note that Telegram only supports a very limited subset of entities:
/// bold, italic, underline, strikethrough, code blocks, pre blocks and inline links (inline
/// links with this format `tg://user?id=12345678` will be replaced with inline mentions when
/// possible).
#[cfg(feature = "markdown")]
pub fn markdown<T>(mut self, s: T) -> Self
where
T: AsRef<str>,
{
let (caption, entities) = crate::parsers::parse_markdown_message(s.as_ref());
self.caption = caption;
self.entities = entities;
self
}
/// Builds a new media from the given HTML-formatted string as the
/// caption contents and entities.
///
/// Note that Telegram only supports a very limited subset of entities:
/// bold, italic, underline, strikethrough, code blocks, pre blocks and inline links (inline
/// links with this format `tg://user?id=12345678` will be replaced with inline mentions when
/// possible).
#[cfg(feature = "html")]
pub fn html<T>(mut self, s: T) -> Self
where
T: AsRef<str>,
{
let (caption, entities) = crate::parsers::parse_html_message(s.as_ref());
self.caption = caption;
self.entities = entities;
self
}
/// The album identifier to which this album should reply to, if any.
///
/// Otherwise, this album will not be a reply to any other.
///
/// Only the reply_to from the first media is used.
pub fn reply_to(mut self, reply_to: Option<i32>) -> Self {
self.reply_to = reply_to;
self
}
/// Include the uploaded file as a photo in the album.
///
/// The Telegram server will compress the image and convert it to JPEG format if necessary.
///
/// The text will be the caption of the photo, which may be empty for no caption.
pub fn photo(mut self, file: Uploaded) -> Self {
self.media = Some(
(tl::types::InputMediaUploadedPhoto {
spoiler: false,
file: file.raw,
stickers: None,
ttl_seconds: self.media_ttl,
})
.into(),
);
self
}
/// Include an external photo in the album.
///
/// The Telegram server will download and compress the image and convert it to JPEG format if
/// necessary.
///
/// The text will be the caption of the photo, which may be empty for no caption.
pub fn photo_url(mut self, url: impl Into<String>) -> Self {
self.media = Some(
(tl::types::InputMediaPhotoExternal {
spoiler: false,
url: url.into(),
ttl_seconds: self.media_ttl,
})
.into(),
);
self
}
/// Include the uploaded file as a document in the album.
///
/// You can use this to send videos, stickers, audios, or uncompressed photos.
///
/// The text will be the caption of the document, which may be empty for no caption.
pub fn document(mut self, file: Uploaded) -> Self {
let mime_type = self.get_file_mime(&file);
let file_name = file.name().to_string();
self.media = Some(
(tl::types::InputMediaUploadedDocument {
nosound_video: false,
force_file: false,
spoiler: false,
file: file.raw,
thumb: None,
mime_type,
attributes: vec![(tl::types::DocumentAttributeFilename { file_name }).into()],
stickers: None,
ttl_seconds: self.media_ttl,
video_cover: None,
video_timestamp: None,
})
.into(),
);
self
}
/// Include the video file with thumb in the album.
///
/// The text will be the caption of the document, which may be empty for no caption.
///
/// # Examples
///
/// ```
/// async fn f(client: &mut grammers_client::Client) -> Result<(), Box<dyn std::error::Error>> {
/// use grammers_client::media::InputMedia;
///
/// let video = client.upload_file("video.mp4").await?;
/// let thumb = client.upload_file("thumb.png").await?;
/// let media = InputMedia::new().caption("").document(video).thumbnail(thumb);
/// Ok(())
/// }
/// ```
pub fn thumbnail(mut self, thumb: Uploaded) -> Self {
if let Some(tl::enums::InputMedia::UploadedDocument(document)) = &mut self.media {
document.thumb = Some(thumb.raw);
}
self
}
/// Include an external file as a document in the album.
///
/// You can use this to send videos, stickers, audios, or uncompressed photos.
///
/// The Telegram server will be the one that downloads and includes the document as media.
///
/// The text will be the caption of the document, which may be empty for no caption.
pub fn document_url(mut self, url: impl Into<String>) -> Self {
self.media = Some(
(tl::types::InputMediaDocumentExternal {
spoiler: false,
url: url.into(),
ttl_seconds: self.media_ttl,
video_cover: None,
video_timestamp: None,
})
.into(),
);
self
}
/// Add additional attributes to the media.
///
/// This must be called *after* setting a file.
///
/// # Examples
///
/// ```
/// # async fn f(client: &mut grammers_client::Client) -> Result<(), Box<dyn std::error::Error>> {
/// # let audio = client.upload_file("audio.flac").await?;
/// #
/// use std::time::Duration;
/// use grammers_client::media::{Attribute, InputMedia};
///
/// let media = InputMedia::new().caption("").document(audio).attribute(
/// Attribute::Audio {
/// duration: Duration::new(123, 0),
/// title: Some("Hello".to_string()),
/// performer: Some("World".to_string()),
/// }
/// );
/// # Ok(())
/// # }
/// ```
pub fn attribute(mut self, attr: Attribute) -> Self {
if let Some(tl::enums::InputMedia::UploadedDocument(document)) = &mut self.media {
document.attributes.push(attr.into());
}
self
}
/// Copy media from an existing message.
///
/// You can use this to send media from another message without re-uploading it.
pub fn copy_media(mut self, media: &Media) -> Self {
self.media = media.to_raw_input_media();
self
}
/// Include the uploaded file as a document file in the album.
///
/// You can use this to send any type of media as a simple document file.
///
/// The text will be the caption of the file, which may be empty for no caption.
pub fn file(mut self, file: Uploaded) -> Self {
let mime_type = self.get_file_mime(&file);
let file_name = file.name().to_string();
self.media = Some(
(tl::types::InputMediaUploadedDocument {
nosound_video: false,
force_file: true,
spoiler: false,
file: file.raw,
thumb: None,
mime_type,
attributes: vec![(tl::types::DocumentAttributeFilename { file_name }).into()],
stickers: None,
ttl_seconds: self.media_ttl,
video_cover: None,
video_timestamp: None,
})
.into(),
);
self
}
/// Change the media's Time To Live (TTL).
///
/// For example, this enables you to send a `photo` that can only be viewed for a certain
/// amount of seconds before it expires.
///
/// Not all media supports this feature.
///
/// This method should be called before setting any media, else it won't have any effect.
pub fn media_ttl(mut self, seconds: i32) -> Self {
self.media_ttl = if seconds < 0 { None } else { Some(seconds) };
self
}
/// Change the media's mime type.
///
/// This method will override the mime type that would otherwise be automatically inferred
/// from the extension of the used file
///
/// If no mime type is set and it cannot be inferred, the mime type will be
/// "application/octet-stream".
///
/// This method should be called before setting any media, else it won't have any effect.
pub fn mime_type(mut self, mime_type: &str) -> Self {
self.mime_type = Some(mime_type.to_string());
self
}
/// Return the mime type string for the given file.
fn get_file_mime(&self, file: &Uploaded) -> String {
if let Some(mime) = self.mime_type.as_ref() {
mime.clone()
} else if let Some(mime) = mime_guess::from_path(file.name()).first() {
mime.essence_str().to_string()
} else {
"application/octet-stream".to_string()
}
}
}