tgbot/types/inline_mode/result/mpeg4_gif/
mod.rs

1use serde::{Deserialize, Serialize};
2
3use super::raw::{
4    RawInlineQueryResult,
5    RawInlineQueryResultData,
6    RawInlineQueryResultDataError::{self, MissingField},
7    RawInlineQueryResultType,
8};
9use crate::types::{InlineKeyboardMarkup, InputMessageContent, Integer, ParseMode, TextEntities, TextEntity};
10
11#[cfg(test)]
12mod tests;
13
14/// Represents a link to a video animation (H.264/MPEG-4 AVC video without sound).
15///
16/// By default, this animated MPEG-4 file will be sent by the user with optional caption.
17/// Alternatively, you can use [`Self::with_input_message_content`]
18/// to send a message with the specified content instead of the animation.
19#[serde_with::skip_serializing_none]
20#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
21pub struct InlineQueryResultMpeg4Gif {
22    id: String,
23    mpeg4_url: String,
24    thumbnail_url: String,
25    caption: Option<String>,
26    caption_entities: Option<TextEntities>,
27    input_message_content: Option<InputMessageContent>,
28    mpeg4_duration: Option<Integer>,
29    mpeg4_height: Option<Integer>,
30    mpeg4_width: Option<Integer>,
31    parse_mode: Option<ParseMode>,
32    reply_markup: Option<InlineKeyboardMarkup>,
33    show_caption_above_media: Option<bool>,
34    thumbnail_mime_type: Option<String>,
35    title: Option<String>,
36}
37
38impl InlineQueryResultMpeg4Gif {
39    /// Creates a new `InlineQueryResultMpeg4Gif`.
40    ///
41    /// # Arguments
42    ///
43    /// * `id` - Unique identifier for the result; 1-64 bytes.
44    /// * `mpeg4_url` - A valid URL for the MP4 file; file size must not exceed 1MB.
45    /// * `thumbnail_url` - URL of the static thumbnail (jpeg or gif) for the result.
46    pub fn new<A, B, C>(id: A, mpeg4_url: B, thumbnail_url: C) -> Self
47    where
48        A: Into<String>,
49        B: Into<String>,
50        C: Into<String>,
51    {
52        Self {
53            id: id.into(),
54            mpeg4_url: mpeg4_url.into(),
55            thumbnail_url: thumbnail_url.into(),
56            caption: None,
57            caption_entities: None,
58            input_message_content: None,
59            mpeg4_duration: None,
60            mpeg4_height: None,
61            mpeg4_width: None,
62            parse_mode: None,
63            reply_markup: None,
64            show_caption_above_media: None,
65            thumbnail_mime_type: None,
66            title: None,
67        }
68    }
69
70    /// Sets a new caption.
71    ///
72    /// # Arguments
73    ///
74    /// * `value` - Caption; 0-1024 characters.
75    pub fn with_caption<T>(mut self, value: T) -> Self
76    where
77        T: Into<String>,
78    {
79        self.caption = Some(value.into());
80        self
81    }
82
83    /// Sets a new list of caption entities.
84    ///
85    /// # Arguments
86    ///
87    /// * `value` - The list of special entities that appear in the caption.
88    ///
89    /// Caption parse mode will be set to [`None`] when this method is called.
90    pub fn with_caption_entities<T>(mut self, value: T) -> Self
91    where
92        T: IntoIterator<Item = TextEntity>,
93    {
94        self.caption_entities = Some(value.into_iter().collect());
95        self.parse_mode = None;
96        self
97    }
98
99    /// Sets a new caption parse mode.
100    ///
101    /// # Arguments
102    ///
103    /// * `value` - Parse mode.
104    ///
105    /// Caption entities will be set to [`None`] when this method is called.
106    pub fn with_caption_parse_mode(mut self, value: ParseMode) -> Self {
107        self.parse_mode = Some(value);
108        self.caption_entities = None;
109        self
110    }
111
112    /// Sets a new input message content.
113    ///
114    /// # Arguments
115    ///
116    /// * `value` - Content of the message to be sent instead of the video animation.
117    pub fn with_input_message_content<T>(mut self, value: T) -> Self
118    where
119        T: Into<InputMessageContent>,
120    {
121        self.input_message_content = Some(value.into());
122        self
123    }
124
125    /// Sets a new MPEG4 duration.
126    ///
127    /// # Arguments
128    ///
129    /// * `value` - MPEG4 duration.
130    pub fn with_mpeg4_duration(mut self, value: Integer) -> Self {
131        self.mpeg4_duration = Some(value);
132        self
133    }
134
135    /// Sets a new MPEG4 height.
136    ///
137    /// # Arguments
138    ///
139    /// * `value` - MPEG4 height.
140    pub fn with_mpeg4_height(mut self, value: Integer) -> Self {
141        self.mpeg4_height = Some(value);
142        self
143    }
144
145    /// Sets a new MPEG4 width.
146    ///
147    /// # Arguments
148    ///
149    /// * `value` - MPEG4 width.
150    pub fn with_mpeg4_width(mut self, value: Integer) -> Self {
151        self.mpeg4_width = Some(value);
152        self
153    }
154
155    /// Sets a new reply markup.
156    ///
157    /// # Arguments
158    ///
159    /// * `value` - Reply markup.
160    pub fn with_reply_markup<T>(mut self, value: T) -> Self
161    where
162        T: Into<InlineKeyboardMarkup>,
163    {
164        self.reply_markup = Some(value.into());
165        self
166    }
167
168    /// Sets a new value for the `show_caption_above_media` flag.
169    ///
170    /// # Arguments
171    ///
172    /// * `value` - Whether the caption must be shown above the message media.
173    pub fn with_show_caption_above_media(mut self, value: bool) -> Self {
174        self.show_caption_above_media = Some(value);
175        self
176    }
177
178    /// Sets a new thumbnail MIME type.
179    ///
180    /// # Arguments
181    ///
182    /// * `value` - MIME type of the thumbnail; default - “image/jpeg”.
183    ///
184    /// Must be one of “image/jpeg”, “image/gif”, or “video/mp4”.
185    pub fn with_thumbnail_mime_type<T>(mut self, value: T) -> Self
186    where
187        T: Into<String>,
188    {
189        self.thumbnail_mime_type = Some(value.into());
190        self
191    }
192
193    /// Sets a new title.
194    ///
195    /// # Arguments
196    ///
197    /// * `value` - Title of the result.
198    pub fn with_title<T>(mut self, value: T) -> Self
199    where
200        T: Into<String>,
201    {
202        self.title = Some(value.into());
203        self
204    }
205}
206
207/// Represents a link to a video animation
208/// (H.264/MPEG-4 AVC video without sound) stored on the Telegram servers.
209///
210/// By default, this animated MPEG-4 file will be sent by the user with an optional caption.
211/// Alternatively, you can use [`Self::with_input_message_content`]
212/// to send a message with the specified content
213/// instead of the animation.
214#[serde_with::skip_serializing_none]
215#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
216pub struct InlineQueryResultCachedMpeg4Gif {
217    id: String,
218    mpeg4_file_id: String,
219    caption: Option<String>,
220    caption_entities: Option<TextEntities>,
221    input_message_content: Option<InputMessageContent>,
222    parse_mode: Option<ParseMode>,
223    reply_markup: Option<InlineKeyboardMarkup>,
224    show_caption_above_media: Option<bool>,
225    title: Option<String>,
226}
227
228impl InlineQueryResultCachedMpeg4Gif {
229    /// Creates a new `InlineQueryResultCachedMpeg4Gif`.
230    ///
231    /// # Arguments
232    ///
233    /// * `id` - Unique identifier of the result; 1-64 bytes.
234    /// * `mpeg4_file_id` - A valid file identifier for the MP4 file.
235    pub fn new<A, B>(id: A, mpeg4_file_id: B) -> Self
236    where
237        A: Into<String>,
238        B: Into<String>,
239    {
240        Self {
241            id: id.into(),
242            mpeg4_file_id: mpeg4_file_id.into(),
243            caption: None,
244            caption_entities: None,
245            input_message_content: None,
246            parse_mode: None,
247            reply_markup: None,
248            show_caption_above_media: None,
249            title: None,
250        }
251    }
252
253    /// Sets a new caption.
254    ///
255    /// # Arguments
256    ///
257    /// * `value` - Caption; 0-1024 characters.
258    pub fn with_caption<T>(mut self, caption: T) -> Self
259    where
260        T: Into<String>,
261    {
262        self.caption = Some(caption.into());
263        self
264    }
265
266    /// Sets a new list of caption entities.
267    ///
268    /// # Arguments
269    ///
270    /// * `value` - The list of special entities that appear in the caption.
271    ///
272    /// Caption parse mode will be set to [`None`] when this method is called.
273    pub fn with_caption_entities<T>(mut self, value: T) -> Self
274    where
275        T: IntoIterator<Item = TextEntity>,
276    {
277        self.caption_entities = Some(value.into_iter().collect());
278        self.parse_mode = None;
279        self
280    }
281
282    /// Sets a new caption parse mode.
283    ///
284    /// # Arguments
285    ///
286    /// * `value` - Parse mode.
287    ///
288    /// Caption entities will be set to [`None`] when this method is called.
289    pub fn with_caption_parse_mode(mut self, value: ParseMode) -> Self {
290        self.parse_mode = Some(value);
291        self.caption_entities = None;
292        self
293    }
294
295    /// Sets a new input message content.
296    ///
297    /// # Arguments
298    ///
299    /// * `value` - Content of the message to be sent instead of the video animation.
300    pub fn with_input_message_content<T>(mut self, input_message_content: T) -> Self
301    where
302        T: Into<InputMessageContent>,
303    {
304        self.input_message_content = Some(input_message_content.into());
305        self
306    }
307
308    /// Sets a new reply markup.
309    ///
310    /// # Arguments
311    ///
312    /// * `value` - Reply markup.
313    pub fn with_reply_markup<T>(mut self, value: T) -> Self
314    where
315        T: Into<InlineKeyboardMarkup>,
316    {
317        self.reply_markup = Some(value.into());
318        self
319    }
320
321    /// Sets a new value for the `show_caption_above_media` flag.
322    ///
323    /// # Arguments
324    ///
325    /// * `value` - Whether the caption must be shown above the message media.
326    pub fn with_show_caption_above_media(mut self, value: bool) -> Self {
327        self.show_caption_above_media = Some(value);
328        self
329    }
330
331    /// Sets a new title.
332    ///
333    /// # Arguments
334    ///
335    /// * `value` - Title for the result.
336    pub fn with_title<T>(mut self, title: T) -> Self
337    where
338        T: Into<String>,
339    {
340        self.title = Some(title.into());
341        self
342    }
343}
344
345impl TryFrom<RawInlineQueryResult> for InlineQueryResultMpeg4Gif {
346    type Error = RawInlineQueryResultDataError;
347
348    fn try_from(value: RawInlineQueryResult) -> Result<Self, Self::Error> {
349        Ok(Self {
350            id: value.id,
351            input_message_content: value.data.input_message_content,
352            caption: value.data.caption,
353            caption_entities: value.data.caption_entities,
354            mpeg4_url: value.data.mpeg4_url.ok_or(MissingField("mpeg4_url"))?,
355            mpeg4_width: value.data.mpeg4_width,
356            mpeg4_height: value.data.mpeg4_height,
357            mpeg4_duration: value.data.mpeg4_duration,
358            parse_mode: value.data.parse_mode,
359            reply_markup: value.data.reply_markup,
360            show_caption_above_media: value.data.show_caption_above_media,
361            thumbnail_mime_type: value.data.thumbnail_mime_type,
362            thumbnail_url: value.data.thumbnail_url.ok_or(MissingField("thumbnail_url"))?,
363            title: value.data.title,
364        })
365    }
366}
367
368impl From<InlineQueryResultMpeg4Gif> for RawInlineQueryResult {
369    fn from(value: InlineQueryResultMpeg4Gif) -> Self {
370        Self {
371            data: RawInlineQueryResultData {
372                caption: value.caption,
373                caption_entities: value.caption_entities,
374                input_message_content: value.input_message_content,
375                mpeg4_duration: value.mpeg4_duration,
376                mpeg4_height: value.mpeg4_height,
377                mpeg4_width: value.mpeg4_width,
378                mpeg4_url: Some(value.mpeg4_url),
379                parse_mode: value.parse_mode,
380                reply_markup: value.reply_markup,
381                show_caption_above_media: value.show_caption_above_media,
382                thumbnail_url: Some(value.thumbnail_url),
383                thumbnail_mime_type: value.thumbnail_mime_type,
384                title: value.title,
385                ..Default::default()
386            },
387            id: value.id,
388            result_type: RawInlineQueryResultType::Mpeg4Gif,
389        }
390    }
391}
392
393impl TryFrom<RawInlineQueryResult> for InlineQueryResultCachedMpeg4Gif {
394    type Error = RawInlineQueryResultDataError;
395
396    fn try_from(value: RawInlineQueryResult) -> Result<Self, Self::Error> {
397        Ok(Self {
398            id: value.id,
399            caption: value.data.caption,
400            caption_entities: value.data.caption_entities,
401            input_message_content: value.data.input_message_content,
402            mpeg4_file_id: value.data.mpeg4_file_id.ok_or(MissingField("mpeg4_file_id"))?,
403            parse_mode: value.data.parse_mode,
404            show_caption_above_media: value.data.show_caption_above_media,
405            reply_markup: value.data.reply_markup,
406            title: value.data.title,
407        })
408    }
409}
410
411impl From<InlineQueryResultCachedMpeg4Gif> for RawInlineQueryResult {
412    fn from(value: InlineQueryResultCachedMpeg4Gif) -> Self {
413        Self {
414            data: RawInlineQueryResultData {
415                caption: value.caption,
416                caption_entities: value.caption_entities,
417                input_message_content: value.input_message_content,
418                mpeg4_file_id: Some(value.mpeg4_file_id),
419                parse_mode: value.parse_mode,
420                reply_markup: value.reply_markup,
421                show_caption_above_media: value.show_caption_above_media,
422                title: value.title,
423                ..Default::default()
424            },
425            id: value.id,
426            result_type: RawInlineQueryResultType::CachedMpeg4Gif,
427        }
428    }
429}