1use std::{error::Error, fmt};
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6 api::{Method, Payload},
7 types::{ChatAdministratorRights, ChatId, Integer, StarAmount},
8};
9
10#[serde_with::skip_serializing_none]
12#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
13pub struct Bot {
14 pub first_name: String,
16 pub id: Integer,
18 pub username: String,
20 pub can_connect_to_business: bool,
22 pub can_join_groups: bool,
24 pub can_read_all_group_messages: bool,
26 pub has_main_web_app: bool,
28 pub last_name: Option<String>,
30 pub supports_inline_queries: bool,
32}
33
34impl Bot {
35 pub fn new<A, B>(id: Integer, username: A, first_name: B) -> Self
43 where
44 A: Into<String>,
45 B: Into<String>,
46 {
47 Self {
48 first_name: first_name.into(),
49 id,
50 username: username.into(),
51 can_connect_to_business: false,
52 can_join_groups: false,
53 can_read_all_group_messages: false,
54 has_main_web_app: false,
55 last_name: None,
56 supports_inline_queries: false,
57 }
58 }
59
60 pub fn with_can_connect_to_business(mut self, value: bool) -> Self {
66 self.can_connect_to_business = value;
67 self
68 }
69
70 pub fn with_can_join_groups(mut self, value: bool) -> Self {
76 self.can_join_groups = value;
77 self
78 }
79
80 pub fn with_can_read_all_group_messages(mut self, value: bool) -> Self {
86 self.can_read_all_group_messages = value;
87 self
88 }
89
90 pub fn with_has_main_web_app(mut self, value: bool) -> Self {
96 self.has_main_web_app = value;
97 self
98 }
99
100 pub fn with_last_name<T>(mut self, value: T) -> Self
106 where
107 T: Into<String>,
108 {
109 self.last_name = Some(value.into());
110 self
111 }
112
113 pub fn with_supports_inline_queries(mut self, value: bool) -> Self {
119 self.supports_inline_queries = value;
120 self
121 }
122}
123
124#[derive(Clone, Debug, Deserialize, Serialize)]
126pub struct BotCommand {
127 #[serde(rename = "command")]
128 name: String,
129 description: String,
130}
131
132impl BotCommand {
133 const MIN_NAME_LEN: usize = 1;
134 const MAX_NAME_LEN: usize = 32;
135 const MIN_DESCRIPTION_LEN: usize = 3;
136 const MAX_DESCRIPTION_LEN: usize = 256;
137
138 pub fn new<C, D>(name: C, description: D) -> Result<Self, BotCommandError>
146 where
147 C: Into<String>,
148 D: Into<String>,
149 {
150 let name = name.into();
151 let description = description.into();
152 let name_len = name.len();
153 let description_len = description.len();
154 if !(Self::MIN_NAME_LEN..=Self::MAX_NAME_LEN).contains(&name_len) {
155 Err(BotCommandError::BadNameLen(name_len))
156 } else if !(Self::MIN_DESCRIPTION_LEN..=Self::MAX_DESCRIPTION_LEN).contains(&description_len) {
157 Err(BotCommandError::BadDescriptionLen(description_len))
158 } else {
159 Ok(Self { name, description })
160 }
161 }
162
163 pub fn name(&self) -> &str {
165 &self.name
166 }
167
168 pub fn with_name<T>(mut self, value: T) -> Self
174 where
175 T: Into<String>,
176 {
177 self.name = value.into();
178 self
179 }
180
181 pub fn description(&self) -> &str {
183 &self.description
184 }
185
186 pub fn with_description<T>(mut self, value: T) -> Self
192 where
193 T: Into<String>,
194 {
195 self.description = value.into();
196 self
197 }
198}
199
200#[derive(Debug)]
202pub enum BotCommandError {
203 BadNameLen(usize),
205 BadDescriptionLen(usize),
207}
208
209impl Error for BotCommandError {}
210
211impl fmt::Display for BotCommandError {
212 fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
213 use self::BotCommandError::*;
214 match self {
215 BadNameLen(len) => write!(
216 out,
217 "command name can have a length of {} up to {} characters, got {}",
218 BotCommand::MIN_NAME_LEN,
219 BotCommand::MAX_NAME_LEN,
220 len
221 ),
222 BadDescriptionLen(len) => write!(
223 out,
224 "command description can have a length of {} up to {} characters, got {}",
225 BotCommand::MIN_DESCRIPTION_LEN,
226 BotCommand::MAX_DESCRIPTION_LEN,
227 len
228 ),
229 }
230 }
231}
232
233#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
235#[serde(tag = "type")]
236#[serde(rename_all = "snake_case")]
237pub enum BotCommandScope {
238 AllChatAdministrators,
240 AllGroupChats,
242 AllPrivateChats,
244 Chat {
246 chat_id: ChatId,
248 },
249 ChatAdministrators {
251 chat_id: ChatId,
253 },
254 ChatMember {
256 chat_id: ChatId,
258 user_id: Integer,
260 },
261 Default,
265}
266
267impl BotCommandScope {
268 pub fn chat<T>(value: T) -> Self
274 where
275 T: Into<ChatId>,
276 {
277 Self::Chat { chat_id: value.into() }
278 }
279
280 pub fn chat_administrators<T>(value: T) -> Self
287 where
288 T: Into<ChatId>,
289 {
290 Self::ChatAdministrators { chat_id: value.into() }
291 }
292
293 pub fn chat_member<A>(chat_id: A, user_id: Integer) -> Self
300 where
301 A: Into<ChatId>,
302 {
303 Self::ChatMember {
304 chat_id: chat_id.into(),
305 user_id,
306 }
307 }
308}
309
310#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
312pub struct BotDescription {
313 pub description: String,
315}
316
317impl BotDescription {
318 pub fn new<T>(value: T) -> Self
324 where
325 T: Into<String>,
326 {
327 Self {
328 description: value.into(),
329 }
330 }
331}
332
333#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
335pub struct BotName {
336 pub name: String,
338}
339
340impl BotName {
341 pub fn new<T>(value: T) -> Self
347 where
348 T: Into<String>,
349 {
350 Self { name: value.into() }
351 }
352}
353
354#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
356pub struct BotShortDescription {
357 pub short_description: String,
359}
360
361impl BotShortDescription {
362 pub fn new<T>(value: T) -> Self
368 where
369 T: Into<String>,
370 {
371 Self {
372 short_description: value.into(),
373 }
374 }
375}
376
377#[derive(Clone, Copy, Debug)]
384pub struct Close;
385
386impl Method for Close {
387 type Response = bool;
388
389 fn into_payload(self) -> Payload {
390 Payload::empty("close")
391 }
392}
393
394#[serde_with::skip_serializing_none]
398#[derive(Clone, Debug, Default, Serialize)]
399pub struct DeleteBotCommands {
400 language_code: Option<String>,
401 scope: Option<BotCommandScope>,
402}
403
404impl DeleteBotCommands {
405 pub fn with_language_code<T>(mut self, value: T) -> Self
411 where
412 T: Into<String>,
413 {
414 self.language_code = Some(value.into());
415 self
416 }
417
418 pub fn with_scope(mut self, value: BotCommandScope) -> Self {
424 self.scope = Some(value);
425 self
426 }
427}
428
429impl Method for DeleteBotCommands {
430 type Response = bool;
431
432 fn into_payload(self) -> Payload {
433 Payload::json("deleteMyCommands", self)
434 }
435}
436
437#[derive(Clone, Copy, Debug)]
439pub struct GetBot;
440
441impl Method for GetBot {
442 type Response = Bot;
443
444 fn into_payload(self) -> Payload {
445 Payload::empty("getMe")
446 }
447}
448
449#[serde_with::skip_serializing_none]
451#[derive(Clone, Debug, Default, Serialize)]
452pub struct GetBotCommands {
453 language_code: Option<String>,
454 scope: Option<BotCommandScope>,
455}
456
457impl GetBotCommands {
458 pub fn with_language_code<T>(mut self, value: T) -> Self
464 where
465 T: Into<String>,
466 {
467 self.language_code = Some(value.into());
468 self
469 }
470
471 pub fn with_scope(mut self, value: BotCommandScope) -> Self {
477 self.scope = Some(value);
478 self
479 }
480}
481
482impl Method for GetBotCommands {
483 type Response = Vec<BotCommand>;
484
485 fn into_payload(self) -> Payload {
486 Payload::json("getMyCommands", self)
487 }
488}
489
490#[serde_with::skip_serializing_none]
492#[derive(Clone, Copy, Debug, Default, Serialize)]
493pub struct GetBotDefaultAdministratorRights {
494 for_channels: Option<bool>,
495}
496
497impl GetBotDefaultAdministratorRights {
498 pub fn with_for_channels(mut self, value: bool) -> Self {
504 self.for_channels = Some(value);
505 self
506 }
507}
508
509impl Method for GetBotDefaultAdministratorRights {
510 type Response = ChatAdministratorRights;
511
512 fn into_payload(self) -> Payload {
513 Payload::json("getMyDefaultAdministratorRights", self)
514 }
515}
516
517#[serde_with::skip_serializing_none]
519#[derive(Clone, Debug, Default, Serialize)]
520pub struct GetBotDescription {
521 language_code: Option<String>,
522}
523
524impl GetBotDescription {
525 pub fn with_language_code<T>(mut self, value: T) -> Self
531 where
532 T: Into<String>,
533 {
534 self.language_code = Some(value.into());
535 self
536 }
537}
538
539impl Method for GetBotDescription {
540 type Response = BotDescription;
541
542 fn into_payload(self) -> Payload {
543 Payload::json("getMyDescription", self)
544 }
545}
546
547#[serde_with::skip_serializing_none]
549#[derive(Clone, Debug, Default, Serialize)]
550pub struct GetBotName {
551 language_code: Option<String>,
552}
553
554impl GetBotName {
555 pub fn with_language_code<T>(mut self, value: T) -> Self
561 where
562 T: Into<String>,
563 {
564 self.language_code = Some(value.into());
565 self
566 }
567}
568
569impl Method for GetBotName {
570 type Response = BotName;
571
572 fn into_payload(self) -> Payload {
573 Payload::json("getMyName", self)
574 }
575}
576
577#[serde_with::skip_serializing_none]
579#[derive(Clone, Debug, Default, Serialize)]
580pub struct GetBotShortDescription {
581 language_code: Option<String>,
582}
583
584impl GetBotShortDescription {
585 pub fn with_language_code<T>(mut self, value: T) -> Self
591 where
592 T: Into<String>,
593 {
594 self.language_code = Some(value.into());
595 self
596 }
597}
598
599impl Method for GetBotShortDescription {
600 type Response = BotShortDescription;
601
602 fn into_payload(self) -> Payload {
603 Payload::json("getMyShortDescription", self)
604 }
605}
606
607#[derive(Clone, Copy, Debug)]
609pub struct GetBotStarBalance;
610
611impl Method for GetBotStarBalance {
612 type Response = StarAmount;
613
614 fn into_payload(self) -> Payload {
615 Payload::empty("getMyStarBalance")
616 }
617}
618
619#[derive(Clone, Copy, Debug)]
627pub struct LogOut;
628
629impl Method for LogOut {
630 type Response = bool;
631
632 fn into_payload(self) -> Payload {
633 Payload::empty("logOut")
634 }
635}
636
637#[serde_with::skip_serializing_none]
639#[derive(Clone, Debug, Serialize)]
640pub struct SetBotCommands {
641 commands: Vec<BotCommand>,
642 language_code: Option<String>,
643 scope: Option<BotCommandScope>,
644}
645
646impl SetBotCommands {
647 pub fn new(commands: impl IntoIterator<Item = BotCommand>) -> Self {
653 Self {
654 commands: commands.into_iter().collect(),
655 language_code: None,
656 scope: None,
657 }
658 }
659
660 pub fn with_language_code<T>(mut self, value: T) -> Self
668 where
669 T: Into<String>,
670 {
671 self.language_code = Some(value.into());
672 self
673 }
674
675 pub fn with_scope(mut self, value: BotCommandScope) -> Self {
682 self.scope = Some(value);
683 self
684 }
685}
686
687impl Method for SetBotCommands {
688 type Response = bool;
689
690 fn into_payload(self) -> Payload {
691 Payload::json("setMyCommands", self)
692 }
693}
694
695#[serde_with::skip_serializing_none]
701#[derive(Clone, Copy, Debug, Default, Serialize)]
702pub struct SetBotDefaultAdministratorRights {
703 for_channels: Option<bool>,
704 rights: Option<ChatAdministratorRights>,
705}
706
707impl SetBotDefaultAdministratorRights {
708 pub fn with_for_channels(mut self, value: bool) -> Self {
714 self.for_channels = Some(value);
715 self
716 }
717
718 pub fn with_rights(mut self, value: ChatAdministratorRights) -> Self {
725 self.rights = Some(value);
726 self
727 }
728}
729
730impl Method for SetBotDefaultAdministratorRights {
731 type Response = bool;
732
733 fn into_payload(self) -> Payload {
734 Payload::json("setMyDefaultAdministratorRights", self)
735 }
736}
737
738#[serde_with::skip_serializing_none]
740#[derive(Clone, Debug, Default, Serialize)]
741pub struct SetBotDescription {
742 description: Option<String>,
743 language_code: Option<String>,
744}
745
746impl SetBotDescription {
747 pub fn with_description<T>(mut self, value: T) -> Self
755 where
756 T: Into<String>,
757 {
758 self.description = Some(value.into());
759 self
760 }
761
762 pub fn with_language_code<T>(mut self, value: T) -> Self
770 where
771 T: Into<String>,
772 {
773 self.language_code = Some(value.into());
774 self
775 }
776}
777
778impl Method for SetBotDescription {
779 type Response = bool;
780
781 fn into_payload(self) -> Payload {
782 Payload::json("setMyDescription", self)
783 }
784}
785
786#[serde_with::skip_serializing_none]
788#[derive(Clone, Debug, Default, Serialize)]
789pub struct SetBotName {
790 language_code: Option<String>,
791 name: Option<String>,
792}
793
794impl SetBotName {
795 pub fn with_language_code<T>(mut self, value: T) -> Self
803 where
804 T: Into<String>,
805 {
806 self.language_code = Some(value.into());
807 self
808 }
809
810 pub fn with_name<T>(mut self, value: T) -> Self
818 where
819 T: Into<String>,
820 {
821 self.name = Some(value.into());
822 self
823 }
824}
825
826impl Method for SetBotName {
827 type Response = bool;
828
829 fn into_payload(self) -> Payload {
830 Payload::json("setMyName", self)
831 }
832}
833
834#[serde_with::skip_serializing_none]
837#[derive(Clone, Debug, Default, Serialize)]
838pub struct SetBotShortDescription {
839 language_code: Option<String>,
840 short_description: Option<String>,
841}
842
843impl SetBotShortDescription {
844 pub fn with_language_code<T>(mut self, value: T) -> Self
852 where
853 T: Into<String>,
854 {
855 self.language_code = Some(value.into());
856 self
857 }
858
859 pub fn with_short_description<T>(mut self, value: T) -> Self
867 where
868 T: Into<String>,
869 {
870 self.short_description = Some(value.into());
871 self
872 }
873}
874
875impl Method for SetBotShortDescription {
876 type Response = bool;
877
878 fn into_payload(self) -> Payload {
879 Payload::json("setMyShortDescription", self)
880 }
881}