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