rust_tdlib/types/
message.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Describes a message
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Message {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// Message identifier; unique for the chat to which the message belongs
14
15    #[serde(default)]
16    id: i64,
17    /// Identifier of the sender of the message
18
19    #[serde(skip_serializing_if = "MessageSender::_is_default")]
20    sender_id: MessageSender,
21    /// Chat identifier
22
23    #[serde(default)]
24    chat_id: i64,
25    /// The sending state of the message; may be null
26    sending_state: Option<MessageSendingState>,
27    /// The scheduling state of the message; may be null
28    scheduling_state: Option<MessageSchedulingState>,
29    /// True, if the message is outgoing
30
31    #[serde(default)]
32    is_outgoing: bool,
33    /// True, if the message is pinned
34
35    #[serde(default)]
36    is_pinned: bool,
37    /// True, if the message can be edited. For live location and poll messages this fields shows whether editMessageLiveLocation or stopPoll can be used with this message by the application
38
39    #[serde(default)]
40    can_be_edited: bool,
41    /// True, if the message can be forwarded
42
43    #[serde(default)]
44    can_be_forwarded: bool,
45    /// True, if content of the message can be saved locally or copied
46
47    #[serde(default)]
48    can_be_saved: bool,
49    /// True, if the message can be deleted only for the current user while other users will continue to see it
50
51    #[serde(default)]
52    can_be_deleted_only_for_self: bool,
53    /// True, if the message can be deleted for all users
54
55    #[serde(default)]
56    can_be_deleted_for_all_users: bool,
57    /// True, if the message statistics are available
58
59    #[serde(default)]
60    can_get_statistics: bool,
61    /// True, if the message thread info is available
62
63    #[serde(default)]
64    can_get_message_thread: bool,
65    /// True, if chat members already viewed the message can be received through getMessageViewers
66
67    #[serde(default)]
68    can_get_viewers: bool,
69    /// True, if media timestamp links can be generated for media timestamp entities in the message text, caption or web page description
70
71    #[serde(default)]
72    can_get_media_timestamp_links: bool,
73    /// True, if media timestamp entities refers to a media in this message as opposed to a media in the replied message
74
75    #[serde(default)]
76    has_timestamped_media: bool,
77    /// True, if the message is a channel post. All messages to channels are channel posts, all other messages are not channel posts
78
79    #[serde(default)]
80    is_channel_post: bool,
81    /// True, if the message contains an unread mention for the current user
82
83    #[serde(default)]
84    contains_unread_mention: bool,
85    /// Point in time (Unix timestamp) when the message was sent
86
87    #[serde(default)]
88    date: i32,
89    /// Point in time (Unix timestamp) when the message was last edited
90
91    #[serde(default)]
92    edit_date: i32,
93    /// Information about the initial message sender; may be null
94    forward_info: Option<MessageForwardInfo>,
95    /// Information about interactions with the message; may be null
96    interaction_info: Option<MessageInteractionInfo>,
97    /// If non-zero, the identifier of the chat to which the replied message belongs; Currently, only messages in the Replies chat can have different reply_in_chat_id and chat_id
98
99    #[serde(default)]
100    reply_in_chat_id: i64,
101    /// If non-zero, the identifier of the message this message is replying to; can be the identifier of a deleted message
102
103    #[serde(default)]
104    reply_to_message_id: i64,
105    /// If non-zero, the identifier of the message thread the message belongs to; unique within the chat to which the message belongs
106
107    #[serde(default)]
108    message_thread_id: i64,
109    /// For self-destructing messages, the message's TTL (Time To Live), in seconds; 0 if none. TDLib will send updateDeleteMessages or updateMessageContent once the TTL expires
110
111    #[serde(default)]
112    ttl: i32,
113    /// Time left before the message expires, in seconds. If the TTL timer isn't started yet, equals to the value of the ttl field
114
115    #[serde(default)]
116    ttl_expires_in: f32,
117    /// If non-zero, the user identifier of the bot through which this message was sent
118
119    #[serde(default)]
120    via_bot_user_id: i64,
121    /// For channel posts and anonymous group messages, optional author signature
122
123    #[serde(default)]
124    author_signature: String,
125    /// Unique identifier of an album this message belongs to. Only audios, documents, photos and videos can be grouped together in albums
126
127    #[serde(
128        deserialize_with = "super::_common::number_from_string",
129        serialize_with = "super::_common::string_to_number"
130    )]
131    #[serde(default)]
132    media_album_id: i64,
133    /// If non-empty, contains a human-readable description of the reason why access to this message must be restricted
134
135    #[serde(default)]
136    restriction_reason: String,
137    /// Content of the message
138
139    #[serde(skip_serializing_if = "MessageContent::_is_default")]
140    content: MessageContent,
141    /// Reply markup for the message; may be null
142    reply_markup: Option<ReplyMarkup>,
143}
144
145impl RObject for Message {
146    #[doc(hidden)]
147    fn extra(&self) -> Option<&str> {
148        self.extra.as_deref()
149    }
150    #[doc(hidden)]
151    fn client_id(&self) -> Option<i32> {
152        self.client_id
153    }
154}
155
156impl Message {
157    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
158        Ok(serde_json::from_str(json.as_ref())?)
159    }
160    pub fn builder() -> MessageBuilder {
161        let mut inner = Message::default();
162        inner.extra = Some(Uuid::new_v4().to_string());
163
164        MessageBuilder { inner }
165    }
166
167    pub fn id(&self) -> i64 {
168        self.id
169    }
170
171    pub fn sender_id(&self) -> &MessageSender {
172        &self.sender_id
173    }
174
175    pub fn chat_id(&self) -> i64 {
176        self.chat_id
177    }
178
179    pub fn sending_state(&self) -> &Option<MessageSendingState> {
180        &self.sending_state
181    }
182
183    pub fn scheduling_state(&self) -> &Option<MessageSchedulingState> {
184        &self.scheduling_state
185    }
186
187    pub fn is_outgoing(&self) -> bool {
188        self.is_outgoing
189    }
190
191    pub fn is_pinned(&self) -> bool {
192        self.is_pinned
193    }
194
195    pub fn can_be_edited(&self) -> bool {
196        self.can_be_edited
197    }
198
199    pub fn can_be_forwarded(&self) -> bool {
200        self.can_be_forwarded
201    }
202
203    pub fn can_be_saved(&self) -> bool {
204        self.can_be_saved
205    }
206
207    pub fn can_be_deleted_only_for_self(&self) -> bool {
208        self.can_be_deleted_only_for_self
209    }
210
211    pub fn can_be_deleted_for_all_users(&self) -> bool {
212        self.can_be_deleted_for_all_users
213    }
214
215    pub fn can_get_statistics(&self) -> bool {
216        self.can_get_statistics
217    }
218
219    pub fn can_get_message_thread(&self) -> bool {
220        self.can_get_message_thread
221    }
222
223    pub fn can_get_viewers(&self) -> bool {
224        self.can_get_viewers
225    }
226
227    pub fn can_get_media_timestamp_links(&self) -> bool {
228        self.can_get_media_timestamp_links
229    }
230
231    pub fn has_timestamped_media(&self) -> bool {
232        self.has_timestamped_media
233    }
234
235    pub fn is_channel_post(&self) -> bool {
236        self.is_channel_post
237    }
238
239    pub fn contains_unread_mention(&self) -> bool {
240        self.contains_unread_mention
241    }
242
243    pub fn date(&self) -> i32 {
244        self.date
245    }
246
247    pub fn edit_date(&self) -> i32 {
248        self.edit_date
249    }
250
251    pub fn forward_info(&self) -> &Option<MessageForwardInfo> {
252        &self.forward_info
253    }
254
255    pub fn interaction_info(&self) -> &Option<MessageInteractionInfo> {
256        &self.interaction_info
257    }
258
259    pub fn reply_in_chat_id(&self) -> i64 {
260        self.reply_in_chat_id
261    }
262
263    pub fn reply_to_message_id(&self) -> i64 {
264        self.reply_to_message_id
265    }
266
267    pub fn message_thread_id(&self) -> i64 {
268        self.message_thread_id
269    }
270
271    pub fn ttl(&self) -> i32 {
272        self.ttl
273    }
274
275    pub fn ttl_expires_in(&self) -> f32 {
276        self.ttl_expires_in
277    }
278
279    pub fn via_bot_user_id(&self) -> i64 {
280        self.via_bot_user_id
281    }
282
283    pub fn author_signature(&self) -> &String {
284        &self.author_signature
285    }
286
287    pub fn media_album_id(&self) -> i64 {
288        self.media_album_id
289    }
290
291    pub fn restriction_reason(&self) -> &String {
292        &self.restriction_reason
293    }
294
295    pub fn content(&self) -> &MessageContent {
296        &self.content
297    }
298
299    pub fn reply_markup(&self) -> &Option<ReplyMarkup> {
300        &self.reply_markup
301    }
302}
303
304#[doc(hidden)]
305pub struct MessageBuilder {
306    inner: Message,
307}
308
309#[deprecated]
310pub type RTDMessageBuilder = MessageBuilder;
311
312impl MessageBuilder {
313    pub fn build(&self) -> Message {
314        self.inner.clone()
315    }
316
317    pub fn id(&mut self, id: i64) -> &mut Self {
318        self.inner.id = id;
319        self
320    }
321
322    pub fn sender_id<T: AsRef<MessageSender>>(&mut self, sender_id: T) -> &mut Self {
323        self.inner.sender_id = sender_id.as_ref().clone();
324        self
325    }
326
327    pub fn chat_id(&mut self, chat_id: i64) -> &mut Self {
328        self.inner.chat_id = chat_id;
329        self
330    }
331
332    pub fn sending_state<T: AsRef<MessageSendingState>>(&mut self, sending_state: T) -> &mut Self {
333        self.inner.sending_state = Some(sending_state.as_ref().clone());
334        self
335    }
336
337    pub fn scheduling_state<T: AsRef<MessageSchedulingState>>(
338        &mut self,
339        scheduling_state: T,
340    ) -> &mut Self {
341        self.inner.scheduling_state = Some(scheduling_state.as_ref().clone());
342        self
343    }
344
345    pub fn is_outgoing(&mut self, is_outgoing: bool) -> &mut Self {
346        self.inner.is_outgoing = is_outgoing;
347        self
348    }
349
350    pub fn is_pinned(&mut self, is_pinned: bool) -> &mut Self {
351        self.inner.is_pinned = is_pinned;
352        self
353    }
354
355    pub fn can_be_edited(&mut self, can_be_edited: bool) -> &mut Self {
356        self.inner.can_be_edited = can_be_edited;
357        self
358    }
359
360    pub fn can_be_forwarded(&mut self, can_be_forwarded: bool) -> &mut Self {
361        self.inner.can_be_forwarded = can_be_forwarded;
362        self
363    }
364
365    pub fn can_be_saved(&mut self, can_be_saved: bool) -> &mut Self {
366        self.inner.can_be_saved = can_be_saved;
367        self
368    }
369
370    pub fn can_be_deleted_only_for_self(
371        &mut self,
372        can_be_deleted_only_for_self: bool,
373    ) -> &mut Self {
374        self.inner.can_be_deleted_only_for_self = can_be_deleted_only_for_self;
375        self
376    }
377
378    pub fn can_be_deleted_for_all_users(
379        &mut self,
380        can_be_deleted_for_all_users: bool,
381    ) -> &mut Self {
382        self.inner.can_be_deleted_for_all_users = can_be_deleted_for_all_users;
383        self
384    }
385
386    pub fn can_get_statistics(&mut self, can_get_statistics: bool) -> &mut Self {
387        self.inner.can_get_statistics = can_get_statistics;
388        self
389    }
390
391    pub fn can_get_message_thread(&mut self, can_get_message_thread: bool) -> &mut Self {
392        self.inner.can_get_message_thread = can_get_message_thread;
393        self
394    }
395
396    pub fn can_get_viewers(&mut self, can_get_viewers: bool) -> &mut Self {
397        self.inner.can_get_viewers = can_get_viewers;
398        self
399    }
400
401    pub fn can_get_media_timestamp_links(
402        &mut self,
403        can_get_media_timestamp_links: bool,
404    ) -> &mut Self {
405        self.inner.can_get_media_timestamp_links = can_get_media_timestamp_links;
406        self
407    }
408
409    pub fn has_timestamped_media(&mut self, has_timestamped_media: bool) -> &mut Self {
410        self.inner.has_timestamped_media = has_timestamped_media;
411        self
412    }
413
414    pub fn is_channel_post(&mut self, is_channel_post: bool) -> &mut Self {
415        self.inner.is_channel_post = is_channel_post;
416        self
417    }
418
419    pub fn contains_unread_mention(&mut self, contains_unread_mention: bool) -> &mut Self {
420        self.inner.contains_unread_mention = contains_unread_mention;
421        self
422    }
423
424    pub fn date(&mut self, date: i32) -> &mut Self {
425        self.inner.date = date;
426        self
427    }
428
429    pub fn edit_date(&mut self, edit_date: i32) -> &mut Self {
430        self.inner.edit_date = edit_date;
431        self
432    }
433
434    pub fn forward_info<T: AsRef<MessageForwardInfo>>(&mut self, forward_info: T) -> &mut Self {
435        self.inner.forward_info = Some(forward_info.as_ref().clone());
436        self
437    }
438
439    pub fn interaction_info<T: AsRef<MessageInteractionInfo>>(
440        &mut self,
441        interaction_info: T,
442    ) -> &mut Self {
443        self.inner.interaction_info = Some(interaction_info.as_ref().clone());
444        self
445    }
446
447    pub fn reply_in_chat_id(&mut self, reply_in_chat_id: i64) -> &mut Self {
448        self.inner.reply_in_chat_id = reply_in_chat_id;
449        self
450    }
451
452    pub fn reply_to_message_id(&mut self, reply_to_message_id: i64) -> &mut Self {
453        self.inner.reply_to_message_id = reply_to_message_id;
454        self
455    }
456
457    pub fn message_thread_id(&mut self, message_thread_id: i64) -> &mut Self {
458        self.inner.message_thread_id = message_thread_id;
459        self
460    }
461
462    pub fn ttl(&mut self, ttl: i32) -> &mut Self {
463        self.inner.ttl = ttl;
464        self
465    }
466
467    pub fn ttl_expires_in(&mut self, ttl_expires_in: f32) -> &mut Self {
468        self.inner.ttl_expires_in = ttl_expires_in;
469        self
470    }
471
472    pub fn via_bot_user_id(&mut self, via_bot_user_id: i64) -> &mut Self {
473        self.inner.via_bot_user_id = via_bot_user_id;
474        self
475    }
476
477    pub fn author_signature<T: AsRef<str>>(&mut self, author_signature: T) -> &mut Self {
478        self.inner.author_signature = author_signature.as_ref().to_string();
479        self
480    }
481
482    pub fn media_album_id(&mut self, media_album_id: i64) -> &mut Self {
483        self.inner.media_album_id = media_album_id;
484        self
485    }
486
487    pub fn restriction_reason<T: AsRef<str>>(&mut self, restriction_reason: T) -> &mut Self {
488        self.inner.restriction_reason = restriction_reason.as_ref().to_string();
489        self
490    }
491
492    pub fn content<T: AsRef<MessageContent>>(&mut self, content: T) -> &mut Self {
493        self.inner.content = content.as_ref().clone();
494        self
495    }
496
497    pub fn reply_markup<T: AsRef<ReplyMarkup>>(&mut self, reply_markup: T) -> &mut Self {
498        self.inner.reply_markup = Some(reply_markup.as_ref().clone());
499        self
500    }
501}
502
503impl AsRef<Message> for Message {
504    fn as_ref(&self) -> &Message {
505        self
506    }
507}
508
509impl AsRef<Message> for MessageBuilder {
510    fn as_ref(&self) -> &Message {
511        &self.inner
512    }
513}