Skip to main content

telers/methods/
copy_messages.rs

1use crate::client::Bot;
2use serde::Serialize;
3/// Use this method to copy messages of any kind. If some of the specified messages can't be found or copied, they are skipped. Service messages, paid media messages, giveaway messages, giveaway winners messages, and invoice messages can't be copied. A quiz poll can be copied only if the value of the field `correct_option_id` is known to the bot. The method is analogous to the method forwardMessages, but the copied messages don't have a link to the original message. Album grouping is kept for copied messages. On success, an array of [`crate::types::MessageId`] of the sent messages is returned.
4/// # Documentation
5/// <https://core.telegram.org/bots/api#copymessages>
6/// # Returns
7/// - `Box<[crate::types::MessageId]>`
8#[derive(Clone, Debug, Serialize)]
9pub struct CopyMessages {
10    /// Unique identifier for the target chat or username of the target channel (in the format @channelusername)
11    pub chat_id: crate::types::ChatIdKind,
12    /// Unique identifier for the target message thread (topic) of a forum; for forum supergroups and private chats of bots with forum topic mode enabled only
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub message_thread_id: Option<i64>,
15    /// Identifier of the direct messages topic to which the messages will be sent; required if the messages are sent to a direct messages chat
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub direct_messages_topic_id: Option<i64>,
18    /// Unique identifier for the chat where the original messages were sent (or channel username in the format @channelusername)
19    pub from_chat_id: crate::types::ChatIdKind,
20    /// A JSON-serialized list of 1-100 identifiers of messages in the chat `from_chat_id` to copy. The identifiers must be specified in a strictly increasing order.
21    pub message_ids: Box<[u8]>,
22    /// Sends the messages silently. Users will receive a notification with no sound.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub disable_notification: Option<bool>,
25    /// Protects the contents of the sent messages from forwarding and saving
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub protect_content: Option<bool>,
28    /// Pass `true` to copy the messages without their captions
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub remove_caption: Option<bool>,
31}
32impl CopyMessages {
33    /// Creates a new `CopyMessages`.
34    ///
35    /// # Arguments
36    /// * `chat_id` - Unique identifier for the target chat or username of the target channel (in the format @channelusername)
37    /// * `from_chat_id` - Unique identifier for the chat where the original messages were sent (or channel username in the format @channelusername)
38    /// * `message_ids` - A JSON-serialized list of 1-100 identifiers of messages in the chat `from_chat_id` to copy. The identifiers must be specified in a strictly increasing order.
39    ///
40    /// # Notes
41    /// Use builder methods to set optional fields.
42    #[must_use]
43    pub fn new<
44        T0: Into<crate::types::ChatIdKind>,
45        T1: Into<crate::types::ChatIdKind>,
46        T2Item: Into<u8>,
47        T2: IntoIterator<Item = T2Item>,
48    >(
49        chat_id: T0,
50        from_chat_id: T1,
51        message_ids: T2,
52    ) -> Self {
53        Self {
54            chat_id: chat_id.into(),
55            message_thread_id: None,
56            direct_messages_topic_id: None,
57            from_chat_id: from_chat_id.into(),
58            message_ids: message_ids.into_iter().map(Into::into).collect(),
59            disable_notification: None,
60            protect_content: None,
61            remove_caption: None,
62        }
63    }
64
65    /// Unique identifier for the target chat or username of the target channel (in the format @channelusername)
66    #[must_use]
67    pub fn chat_id<T: Into<crate::types::ChatIdKind>>(self, val: T) -> Self {
68        let mut this = self;
69        this.chat_id = val.into();
70        this
71    }
72
73    /// Unique identifier for the target message thread (topic) of a forum; for forum supergroups and private chats of bots with forum topic mode enabled only
74    #[must_use]
75    pub fn message_thread_id<T: Into<i64>>(self, val: T) -> Self {
76        let mut this = self;
77        this.message_thread_id = Some(val.into());
78        this
79    }
80
81    /// Unique identifier for the target message thread (topic) of a forum; for forum supergroups and private chats of bots with forum topic mode enabled only
82    #[must_use]
83    pub fn message_thread_id_option<T: Into<i64>>(self, val: Option<T>) -> Self {
84        let mut this = self;
85        this.message_thread_id = val.map(Into::into);
86        this
87    }
88
89    /// Identifier of the direct messages topic to which the messages will be sent; required if the messages are sent to a direct messages chat
90    #[must_use]
91    pub fn direct_messages_topic_id<T: Into<i64>>(self, val: T) -> Self {
92        let mut this = self;
93        this.direct_messages_topic_id = Some(val.into());
94        this
95    }
96
97    /// Identifier of the direct messages topic to which the messages will be sent; required if the messages are sent to a direct messages chat
98    #[must_use]
99    pub fn direct_messages_topic_id_option<T: Into<i64>>(self, val: Option<T>) -> Self {
100        let mut this = self;
101        this.direct_messages_topic_id = val.map(Into::into);
102        this
103    }
104
105    /// Unique identifier for the chat where the original messages were sent (or channel username in the format @channelusername)
106    #[must_use]
107    pub fn from_chat_id<T: Into<crate::types::ChatIdKind>>(self, val: T) -> Self {
108        let mut this = self;
109        this.from_chat_id = val.into();
110        this
111    }
112
113    /// A JSON-serialized list of 1-100 identifiers of messages in the chat `from_chat_id` to copy. The identifiers must be specified in a strictly increasing order.
114    ///
115    /// # Notes
116    /// Adds multiple elements.
117    #[must_use]
118    pub fn message_ids<TItem: Into<u8>, T: IntoIterator<Item = TItem>>(self, val: T) -> Self {
119        let mut this = self;
120        this.message_ids = this
121            .message_ids
122            .into_vec()
123            .into_iter()
124            .chain(val.into_iter().map(Into::into))
125            .collect();
126        this
127    }
128
129    /// A JSON-serialized list of 1-100 identifiers of messages in the chat `from_chat_id` to copy. The identifiers must be specified in a strictly increasing order.
130    ///
131    /// # Notes
132    /// Adds a single element.
133    #[must_use]
134    pub fn message_id<T: Into<u8>>(self, val: T) -> Self {
135        let mut this = self;
136        this.message_ids = this
137            .message_ids
138            .into_vec()
139            .into_iter()
140            .chain(Some(val.into()))
141            .collect();
142        this
143    }
144
145    /// Sends the messages silently. Users will receive a notification with no sound.
146    #[must_use]
147    pub fn disable_notification<T: Into<bool>>(self, val: T) -> Self {
148        let mut this = self;
149        this.disable_notification = Some(val.into());
150        this
151    }
152
153    /// Sends the messages silently. Users will receive a notification with no sound.
154    #[must_use]
155    pub fn disable_notification_option<T: Into<bool>>(self, val: Option<T>) -> Self {
156        let mut this = self;
157        this.disable_notification = val.map(Into::into);
158        this
159    }
160
161    /// Protects the contents of the sent messages from forwarding and saving
162    #[must_use]
163    pub fn protect_content<T: Into<bool>>(self, val: T) -> Self {
164        let mut this = self;
165        this.protect_content = Some(val.into());
166        this
167    }
168
169    /// Protects the contents of the sent messages from forwarding and saving
170    #[must_use]
171    pub fn protect_content_option<T: Into<bool>>(self, val: Option<T>) -> Self {
172        let mut this = self;
173        this.protect_content = val.map(Into::into);
174        this
175    }
176
177    /// Pass `true` to copy the messages without their captions
178    #[must_use]
179    pub fn remove_caption<T: Into<bool>>(self, val: T) -> Self {
180        let mut this = self;
181        this.remove_caption = Some(val.into());
182        this
183    }
184
185    /// Pass `true` to copy the messages without their captions
186    #[must_use]
187    pub fn remove_caption_option<T: Into<bool>>(self, val: Option<T>) -> Self {
188        let mut this = self;
189        this.remove_caption = val.map(Into::into);
190        this
191    }
192}
193impl super::TelegramMethod for CopyMessages {
194    type Method = Self;
195    type Return = Box<[crate::types::MessageId]>;
196
197    fn build_request<Client>(self, _bot: &Bot<Client>) -> super::Request<Self::Method> {
198        super::Request::new("copyMessages", self, None)
199    }
200}