Skip to main content

tgbot/types/definitions/
checklist.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::{Chat, InlineKeyboardMarkup, Integer, Message, ParseMode, ReplyParameters, TextEntities, TextEntity, User},
6};
7
8/// Describes a task in a checklist.
9#[serde_with::skip_serializing_none]
10#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
11pub struct ChecklistTask {
12    /// Unique identifier of the task.
13    pub id: Integer,
14    /// Text of the task.
15    pub text: String,
16    /// Chat that completed the task; omitted if the task wasn't completed by a chat.
17    pub completed_by_chat: Option<Chat>,
18    /// User that completed the task; omitted if the task wasn't completed.
19    pub completed_by_user: Option<User>,
20    /// Point in time (Unix timestamp) when the task was completed; 0 if the task wasn't completed.
21    pub completion_date: Option<Integer>,
22    /// Special entities that appear in the task text.
23    pub text_entities: Option<TextEntities>,
24}
25
26impl ChecklistTask {
27    /// Creates a new `ChecklistTask`.
28    ///
29    /// # Arguments
30    ///
31    /// * `id` - Unique identifier of the task.
32    /// * `text` - Text of the task.
33    pub fn new<T>(id: Integer, text: T) -> Self
34    where
35        T: Into<String>,
36    {
37        Self {
38            id,
39            text: text.into(),
40            completed_by_chat: None,
41            completed_by_user: None,
42            completion_date: None,
43            text_entities: None,
44        }
45    }
46
47    /// Sets a new chat that completed the task.
48    ///
49    /// # Arguments
50    ///
51    /// * `value` - Chat that completed the task; omitted if the task wasn't completed by a chat.
52    pub fn with_completed_by_chat<T>(mut self, value: T) -> Self
53    where
54        T: Into<Chat>,
55    {
56        self.completed_by_chat = Some(value.into());
57        self
58    }
59
60    /// Sets a new user that completed the task.
61    ///
62    /// # Arguments
63    ///
64    /// * `value` - User that completed the task; omitted if the task wasn't completed.
65    pub fn with_completed_by_user(mut self, value: User) -> Self {
66        self.completed_by_user = Some(value);
67        self
68    }
69
70    /// Sets a new completion date
71    ///
72    /// # Arguments
73    ///
74    /// * `value` - Point in time (Unix timestamp) when the task was completed; 0 if the task wasn't completed.
75    pub fn with_completion_date(mut self, value: Integer) -> Self {
76        self.completion_date = Some(value);
77        self
78    }
79
80    /// Sets a new list of text entities.
81    ///
82    /// # Arguments
83    ///
84    /// * `value` - Special entities that appear in the task text.
85    pub fn with_text_entities<T>(mut self, value: T) -> Self
86    where
87        T: IntoIterator<Item = TextEntity>,
88    {
89        self.text_entities = Some(TextEntities::from_iter(value));
90        self
91    }
92}
93
94/// Describes a checklist.
95#[serde_with::skip_serializing_none]
96#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
97pub struct Checklist {
98    /// List of tasks in the checklist.
99    pub tasks: Vec<ChecklistTask>,
100    /// Title of the checklist.
101    pub title: String,
102    /// Whether users other than the creator of the list can add tasks to the list.
103    pub others_can_add_tasks: Option<bool>,
104    /// Whether users other than the creator of the list can mark tasks as done or not done.
105    pub others_can_mark_tasks_as_done: Option<bool>,
106    /// Special entities that appear in the checklist title.
107    pub title_entities: Option<TextEntities>,
108}
109
110impl Checklist {
111    /// Creates a new `Checklist`.
112    ///
113    /// # Arguments
114    ///
115    /// * `tasks` - List of tasks in the checklist.
116    /// * `title` - Title of the checklist.
117    pub fn new<A, B>(tasks: A, title: B) -> Self
118    where
119        A: IntoIterator<Item = ChecklistTask>,
120        B: Into<String>,
121    {
122        Self {
123            tasks: tasks.into_iter().collect(),
124            title: title.into(),
125            others_can_add_tasks: None,
126            others_can_mark_tasks_as_done: None,
127            title_entities: None,
128        }
129    }
130
131    /// Sets a new value for the `others_can_add_tasks` flag.
132    ///
133    /// # Arguments
134    ///
135    /// * `value` - Whether users other than the creator of the list can add tasks to the list.
136    pub fn with_others_can_add_tasks(mut self, value: bool) -> Self {
137        self.others_can_add_tasks = Some(value);
138        self
139    }
140
141    /// Sets a new value for the `others_can_mark_tasks_as_done` flag.
142    ///
143    /// # Arguments
144    ///
145    /// * `value` - Whether users other than the creator of the list can mark tasks as done or not done.
146    pub fn with_others_can_mark_tasks_as_done(mut self, value: bool) -> Self {
147        self.others_can_mark_tasks_as_done = Some(value);
148        self
149    }
150
151    /// Sets a new list of title entities.
152    ///
153    /// # Arguments
154    ///
155    /// * `value` - Special entities that appear in the checklist title
156    pub fn with_title_entities<T>(mut self, value: T) -> Self
157    where
158        T: IntoIterator<Item = TextEntity>,
159    {
160        self.title_entities = Some(TextEntities::from_iter(value));
161        self
162    }
163}
164
165/// Describes a task to add to a checklist.
166#[serde_with::skip_serializing_none]
167#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
168pub struct InputChecklistTask {
169    /// Unique identifier of the task.
170    ///
171    /// Must be positive and unique among all task identifiers currently present in the checklist.
172    pub id: Integer,
173    /// Text of the task.
174    ///
175    /// 1-100 characters after entities parsing.
176    pub text: String,
177    /// Mode for parsing entities in the text.
178    pub parse_mode: Option<ParseMode>,
179    /// List of special entities that appear in the text.
180    ///
181    /// Can be specified instead of `parse_mode`.
182    /// Currently, only bold, italic, underline, strikethrough, spoiler, custom_emoji,
183    /// and date_time entities are allowed.
184    pub text_entities: Option<TextEntities>,
185}
186
187impl InputChecklistTask {
188    /// Creates a new `InputChecklistTask`.
189    ///
190    /// # Arguments
191    ///
192    /// * `id` - Unique identifier of the task.
193    /// * `text` - Text of the task
194    pub fn new<T>(id: Integer, text: T) -> Self
195    where
196        T: Into<String>,
197    {
198        Self {
199            id,
200            text: text.into(),
201            parse_mode: None,
202            text_entities: None,
203        }
204    }
205
206    /// Sets a new parse mode.
207    ///
208    /// # Arguments
209    ///
210    /// * `value` - Mode for parsing entities in the text.
211    ///
212    /// Text entities will be set to [`None`] when this method is called.
213    pub fn with_parse_mode(mut self, value: ParseMode) -> Self {
214        self.text_entities = None;
215        self.parse_mode = Some(value);
216        self
217    }
218
219    /// Sets a new list of text entities.
220    ///
221    /// # Arguments
222    ///
223    /// * `value` - List of special entities that appear in the text.
224    ///
225    /// Parse mode will be set to [`None`] when this method is called.
226    pub fn with_text_entities<T>(mut self, value: T) -> Self
227    where
228        T: IntoIterator<Item = TextEntity>,
229    {
230        self.parse_mode = None;
231        self.text_entities = Some(TextEntities::from_iter(value));
232        self
233    }
234}
235
236/// Describes a checklist to create.
237#[serde_with::skip_serializing_none]
238#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
239pub struct InputChecklist {
240    /// List of 1-30 tasks in the checklist.
241    pub tasks: Vec<InputChecklistTask>,
242    /// Title of the checklist.
243    ///
244    /// 1-255 characters after entities parsing.
245    pub title: String,
246    /// Whether other users can add tasks to the checklist.
247    pub others_can_add_tasks: Option<bool>,
248    /// Whether other users can mark tasks as done or not done in the checklist.
249    pub others_can_mark_tasks_as_done: Option<bool>,
250    /// Mode for parsing entities in the title.
251    pub parse_mode: Option<ParseMode>,
252    /// List of special entities that appear in the title.
253    ///
254    /// Can be specified instead of parse_mode.
255    /// Currently, only bold, italic, underline, strikethrough, spoiler, and custom_emoji entities are allowed.
256    pub title_entities: Option<TextEntities>,
257}
258
259impl InputChecklist {
260    /// Creates a new `InputChecklist`.
261    ///
262    /// # Arguments
263    ///
264    /// * `tasks` - List of 1-30 tasks in the checklist.
265    /// * `title` - Title of the checklist.
266    pub fn new<A, B>(tasks: A, title: B) -> Self
267    where
268        A: IntoIterator<Item = InputChecklistTask>,
269        B: Into<String>,
270    {
271        Self {
272            tasks: tasks.into_iter().collect(),
273            title: title.into(),
274            others_can_add_tasks: None,
275            others_can_mark_tasks_as_done: None,
276            parse_mode: None,
277            title_entities: None,
278        }
279    }
280
281    /// Sets a new value for the `others_can_add_tasks` flag.
282    ///
283    /// # Arguments
284    ///
285    /// * `value` - Whether other users can add tasks to the checklist.
286    pub fn with_others_can_add_tasks(mut self, value: bool) -> Self {
287        self.others_can_add_tasks = Some(value);
288        self
289    }
290
291    /// Sets a new value for the `others_can_mark_tasks_as_done` flag.
292    ///
293    /// # Arguments
294    ///
295    /// * `value` - Whether other users can mark tasks as done or not done in the checklist.
296    pub fn with_others_can_mark_tasks_as_done(mut self, value: bool) -> Self {
297        self.others_can_mark_tasks_as_done = Some(value);
298        self
299    }
300
301    /// Sets a new parse mode.
302    ///
303    /// # Arguments
304    ///
305    /// * `value` - Mode for parsing entities in the title.
306    ///
307    /// Title entities will be set to [`None`] when this method is called.
308    pub fn with_parse_mode(mut self, value: ParseMode) -> Self {
309        self.title_entities = None;
310        self.parse_mode = Some(value);
311        self
312    }
313
314    /// Sets a new list of text entities.
315    ///
316    /// # Arguments
317    ///
318    /// * `value` - List of special entities that appear in the title.
319    ///
320    /// Parse mode will be set to [`None`] when this method is called.
321    pub fn with_title_entities<T>(mut self, value: T) -> Self
322    where
323        T: IntoIterator<Item = TextEntity>,
324    {
325        self.parse_mode = None;
326        self.title_entities = Some(TextEntities::from_iter(value));
327        self
328    }
329}
330
331/// Describes a service message about checklist tasks marked as done or not done.
332#[serde_with::skip_serializing_none]
333#[derive(Clone, Debug, Default, Deserialize, Serialize)]
334pub struct ChecklistTasksDone {
335    /// Message containing the checklist whose tasks were marked as done or not done.
336    ///
337    /// Note that the Message object in this field will not contain
338    /// the reply_to_message field even if it itself is a reply.
339    pub checklist_message: Option<Box<Message>>,
340    /// Identifiers of the tasks that were marked as done.
341    pub marked_as_done_task_ids: Option<Vec<Integer>>,
342    /// Identifiers of the tasks that were marked as not done.
343    pub marked_as_not_done_task_ids: Option<Vec<Integer>>,
344}
345
346impl ChecklistTasksDone {
347    /// Sets a new checklist message.
348    ///
349    /// # Arguments
350    ///
351    /// * `value` - Message containing the checklist whose tasks were marked as done or not done.
352    pub fn with_checklist_message(mut self, value: Message) -> Self {
353        self.checklist_message = Some(Box::new(value));
354        self
355    }
356
357    /// Sets a new list of task identifiers.
358    ///
359    /// # Arguments
360    ///
361    /// * `value` - Identifiers of the tasks that were marked as done.
362    pub fn with_marked_as_done_task_ids<T>(mut self, value: T) -> Self
363    where
364        T: IntoIterator<Item = Integer>,
365    {
366        self.marked_as_done_task_ids = Some(value.into_iter().collect());
367        self
368    }
369
370    /// Sets a new list of task identifiers.
371    ///
372    /// # Arguments
373    ///
374    /// * `value` - Identifiers of the tasks that were marked as not done.
375    pub fn with_marked_as_not_done_task_ids<T>(mut self, value: T) -> Self
376    where
377        T: IntoIterator<Item = Integer>,
378    {
379        self.marked_as_not_done_task_ids = Some(value.into_iter().collect());
380        self
381    }
382}
383
384/// Describes a service message about tasks added to a checklist.
385#[serde_with::skip_serializing_none]
386#[derive(Clone, Debug, Deserialize, Serialize)]
387pub struct ChecklistTasksAdded {
388    /// List of tasks added to the checklist.
389    pub tasks: Vec<ChecklistTask>,
390    /// Message containing the checklist to which the tasks were added.
391    ///
392    /// Note that the Message object in this field will not contain
393    /// the reply_to_message field even if it itself is a reply.
394    pub checklist_message: Option<Box<Message>>,
395}
396
397impl ChecklistTasksAdded {
398    /// Creates a new `ChecklistTasksAdded`.
399    ///
400    /// # Arguments
401    ///
402    /// * `tasks` - List of tasks added to the checklist.
403    pub fn new<T>(tasks: T) -> Self
404    where
405        T: IntoIterator<Item = ChecklistTask>,
406    {
407        Self {
408            tasks: tasks.into_iter().collect(),
409            checklist_message: None,
410        }
411    }
412
413    /// Sets a new checklist message.
414    ///
415    /// # Arguments
416    ///
417    /// * `value` - Message containing the checklist to which the tasks were added.
418    pub fn with_checklist_message(mut self, value: Message) -> Self {
419        self.checklist_message = Some(Box::new(value));
420        self
421    }
422}
423
424/// Sends a checklist on behalf of a connected business account.
425#[serde_with::skip_serializing_none]
426#[derive(Clone, Debug, PartialEq, Serialize)]
427pub struct SendChecklist {
428    business_connection_id: String,
429    chat_id: Integer,
430    checklist: InputChecklist,
431    disable_notification: Option<bool>,
432    message_effect_id: Option<String>,
433    protect_content: Option<bool>,
434    reply_markup: Option<InlineKeyboardMarkup>,
435    reply_parameters: Option<ReplyParameters>,
436}
437
438impl SendChecklist {
439    /// Creates a new `SendChecklist`.
440    ///
441    /// # Arguments
442    ///
443    /// * `business_connection_id` - Unique identifier of the business connection.
444    /// * `chat_id` - Unique identifier for the target chat.
445    /// * `checklist` - The checklist to send.
446    pub fn new<T>(business_connection_id: T, chat_id: Integer, checklist: InputChecklist) -> Self
447    where
448        T: Into<String>,
449    {
450        Self {
451            business_connection_id: business_connection_id.into(),
452            chat_id,
453            checklist,
454            disable_notification: None,
455            message_effect_id: None,
456            protect_content: None,
457            reply_markup: None,
458            reply_parameters: None,
459        }
460    }
461
462    /// Sets a new value for the `disable_notification` flag.
463    ///
464    /// # Arguments
465    ///
466    /// * `value` - Whether to send the message silently.
467    ///
468    /// Users will receive a notification with no sound.
469    pub fn with_disable_notification(mut self, value: bool) -> Self {
470        self.disable_notification = Some(value);
471        self
472    }
473
474    /// Sets a new message effect ID.
475    ///
476    /// # Arguments
477    ///
478    /// * `value` - Unique identifier of the message effect to be added to the message.
479    pub fn with_message_effect_id<T>(mut self, value: T) -> Self
480    where
481        T: Into<String>,
482    {
483        self.message_effect_id = Some(value.into());
484        self
485    }
486
487    /// Sets a new value for the `protect_content` flag.
488    ///
489    /// # Arguments
490    ///
491    /// * `value` - Whether to protect the contents of the sent message from forwarding and saving.
492    pub fn with_protect_content(mut self, value: bool) -> Self {
493        self.protect_content = Some(value);
494        self
495    }
496
497    /// Sets a new reply markup.
498    ///
499    /// # Arguments
500    ///
501    // * `value` - An inline keyboard.
502    pub fn with_reply_markup<T>(mut self, value: T) -> Self
503    where
504        T: Into<InlineKeyboardMarkup>,
505    {
506        self.reply_markup = Some(value.into());
507        self
508    }
509
510    /// Sets a new list of reply parameters.
511    ///
512    /// # Arguments
513    ///
514    // * `value` - Description of the message to reply to.
515    pub fn with_reply_parameters(mut self, value: ReplyParameters) -> Self {
516        self.reply_parameters = Some(value);
517        self
518    }
519}
520
521impl Method for SendChecklist {
522    type Response = Message;
523
524    fn into_payload(self) -> Payload {
525        Payload::json("sendChecklist", self)
526    }
527}
528
529/// Edits a checklist on behalf of a connected business account. On success, the edited Message is returned.
530#[serde_with::skip_serializing_none]
531#[derive(Clone, Debug, PartialEq, Serialize)]
532pub struct EditMessageChecklist {
533    business_connection_id: String,
534    chat_id: Integer,
535    checklist: InputChecklist,
536    message_id: Integer,
537    reply_markup: Option<InlineKeyboardMarkup>,
538}
539
540impl EditMessageChecklist {
541    /// Creates a new `EditMessageChecklist`.
542    ///
543    /// # Arguments
544    ///
545    /// * `business_connection_id` - Unique identifier of the business connection.
546    /// * `chat_id` - Unique identifier for the target chat.
547    /// * `checklist` - The new checklist.
548    /// * `message_id` - Unique identifier for the target message.
549    pub fn new<T>(business_connection_id: T, chat_id: Integer, checklist: InputChecklist, message_id: Integer) -> Self
550    where
551        T: Into<String>,
552    {
553        Self {
554            business_connection_id: business_connection_id.into(),
555            chat_id,
556            checklist,
557            message_id,
558            reply_markup: None,
559        }
560    }
561
562    /// Sets a new reply markup.
563    ///
564    /// * `value` - The new inline keyboard for the message
565    pub fn with_reply_markup<T>(mut self, value: T) -> Self
566    where
567        T: Into<InlineKeyboardMarkup>,
568    {
569        self.reply_markup = Some(value.into());
570        self
571    }
572}
573
574impl Method for EditMessageChecklist {
575    type Response = Message;
576
577    fn into_payload(self) -> Payload {
578        Payload::json("editMessageChecklist", self)
579    }
580}