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 has_topics_enabled: bool,
30 pub last_name: Option<String>,
32 pub supports_inline_queries: bool,
34}
35
36impl Bot {
37 pub fn new<A, B>(id: Integer, username: A, first_name: B) -> Self
45 where
46 A: Into<String>,
47 B: Into<String>,
48 {
49 Self {
50 first_name: first_name.into(),
51 id,
52 username: username.into(),
53 can_connect_to_business: false,
54 can_join_groups: false,
55 can_read_all_group_messages: false,
56 has_main_web_app: false,
57 has_topics_enabled: false,
58 last_name: None,
59 supports_inline_queries: false,
60 }
61 }
62
63 pub fn with_can_connect_to_business(mut self, value: bool) -> Self {
69 self.can_connect_to_business = value;
70 self
71 }
72
73 pub fn with_can_join_groups(mut self, value: bool) -> Self {
79 self.can_join_groups = value;
80 self
81 }
82
83 pub fn with_can_read_all_group_messages(mut self, value: bool) -> Self {
89 self.can_read_all_group_messages = value;
90 self
91 }
92
93 pub fn with_has_main_web_app(mut self, value: bool) -> Self {
99 self.has_main_web_app = value;
100 self
101 }
102
103 pub fn with_has_topics_enabled(mut self, value: bool) -> Self {
109 self.has_topics_enabled = value;
110 self
111 }
112
113 pub fn with_last_name<T>(mut self, value: T) -> Self
119 where
120 T: Into<String>,
121 {
122 self.last_name = Some(value.into());
123 self
124 }
125
126 pub fn with_supports_inline_queries(mut self, value: bool) -> Self {
132 self.supports_inline_queries = value;
133 self
134 }
135}
136
137#[derive(Clone, Debug, Deserialize, Serialize)]
139pub struct BotCommand {
140 #[serde(rename = "command")]
141 name: String,
142 description: String,
143}
144
145impl BotCommand {
146 const MIN_NAME_LEN: usize = 1;
147 const MAX_NAME_LEN: usize = 32;
148 const MIN_DESCRIPTION_LEN: usize = 3;
149 const MAX_DESCRIPTION_LEN: usize = 256;
150
151 pub fn new<C, D>(name: C, description: D) -> Result<Self, BotCommandError>
159 where
160 C: Into<String>,
161 D: Into<String>,
162 {
163 let name = name.into();
164 let description = description.into();
165 let name_len = name.len();
166 let description_len = description.len();
167 if !(Self::MIN_NAME_LEN..=Self::MAX_NAME_LEN).contains(&name_len) {
168 Err(BotCommandError::BadNameLen(name_len))
169 } else if !(Self::MIN_DESCRIPTION_LEN..=Self::MAX_DESCRIPTION_LEN).contains(&description_len) {
170 Err(BotCommandError::BadDescriptionLen(description_len))
171 } else {
172 Ok(Self { name, description })
173 }
174 }
175
176 pub fn name(&self) -> &str {
178 &self.name
179 }
180
181 pub fn with_name<T>(mut self, value: T) -> Self
187 where
188 T: Into<String>,
189 {
190 self.name = value.into();
191 self
192 }
193
194 pub fn description(&self) -> &str {
196 &self.description
197 }
198
199 pub fn with_description<T>(mut self, value: T) -> Self
205 where
206 T: Into<String>,
207 {
208 self.description = value.into();
209 self
210 }
211}
212
213#[derive(Debug)]
215pub enum BotCommandError {
216 BadNameLen(usize),
218 BadDescriptionLen(usize),
220}
221
222impl Error for BotCommandError {}
223
224impl fmt::Display for BotCommandError {
225 fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
226 use self::BotCommandError::*;
227 match self {
228 BadNameLen(len) => write!(
229 out,
230 "command name can have a length of {} up to {} characters, got {}",
231 BotCommand::MIN_NAME_LEN,
232 BotCommand::MAX_NAME_LEN,
233 len
234 ),
235 BadDescriptionLen(len) => write!(
236 out,
237 "command description can have a length of {} up to {} characters, got {}",
238 BotCommand::MIN_DESCRIPTION_LEN,
239 BotCommand::MAX_DESCRIPTION_LEN,
240 len
241 ),
242 }
243 }
244}
245
246#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
248#[serde(tag = "type")]
249#[serde(rename_all = "snake_case")]
250pub enum BotCommandScope {
251 AllChatAdministrators,
253 AllGroupChats,
255 AllPrivateChats,
257 Chat {
259 chat_id: ChatId,
261 },
262 ChatAdministrators {
264 chat_id: ChatId,
266 },
267 ChatMember {
269 chat_id: ChatId,
271 user_id: Integer,
273 },
274 Default,
278}
279
280impl BotCommandScope {
281 pub fn chat<T>(value: T) -> Self
287 where
288 T: Into<ChatId>,
289 {
290 Self::Chat { chat_id: value.into() }
291 }
292
293 pub fn chat_administrators<T>(value: T) -> Self
300 where
301 T: Into<ChatId>,
302 {
303 Self::ChatAdministrators { chat_id: value.into() }
304 }
305
306 pub fn chat_member<A>(chat_id: A, user_id: Integer) -> Self
313 where
314 A: Into<ChatId>,
315 {
316 Self::ChatMember {
317 chat_id: chat_id.into(),
318 user_id,
319 }
320 }
321}
322
323#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
325pub struct BotDescription {
326 pub description: String,
328}
329
330impl BotDescription {
331 pub fn new<T>(value: T) -> Self
337 where
338 T: Into<String>,
339 {
340 Self {
341 description: value.into(),
342 }
343 }
344}
345
346#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
348pub struct BotName {
349 pub name: String,
351}
352
353impl BotName {
354 pub fn new<T>(value: T) -> Self
360 where
361 T: Into<String>,
362 {
363 Self { name: value.into() }
364 }
365}
366
367#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
369pub struct BotShortDescription {
370 pub short_description: String,
372}
373
374impl BotShortDescription {
375 pub fn new<T>(value: T) -> Self
381 where
382 T: Into<String>,
383 {
384 Self {
385 short_description: value.into(),
386 }
387 }
388}
389
390#[derive(Clone, Copy, Debug)]
397pub struct Close;
398
399impl Method for Close {
400 type Response = bool;
401
402 fn into_payload(self) -> Payload {
403 Payload::empty("close")
404 }
405}
406
407#[serde_with::skip_serializing_none]
411#[derive(Clone, Debug, Default, Serialize)]
412pub struct DeleteBotCommands {
413 language_code: Option<String>,
414 scope: Option<BotCommandScope>,
415}
416
417impl DeleteBotCommands {
418 pub fn with_language_code<T>(mut self, value: T) -> Self
424 where
425 T: Into<String>,
426 {
427 self.language_code = Some(value.into());
428 self
429 }
430
431 pub fn with_scope(mut self, value: BotCommandScope) -> Self {
437 self.scope = Some(value);
438 self
439 }
440}
441
442impl Method for DeleteBotCommands {
443 type Response = bool;
444
445 fn into_payload(self) -> Payload {
446 Payload::json("deleteMyCommands", self)
447 }
448}
449
450#[derive(Clone, Copy, Debug)]
452pub struct GetBot;
453
454impl Method for GetBot {
455 type Response = Bot;
456
457 fn into_payload(self) -> Payload {
458 Payload::empty("getMe")
459 }
460}
461
462#[serde_with::skip_serializing_none]
464#[derive(Clone, Debug, Default, Serialize)]
465pub struct GetBotCommands {
466 language_code: Option<String>,
467 scope: Option<BotCommandScope>,
468}
469
470impl GetBotCommands {
471 pub fn with_language_code<T>(mut self, value: T) -> Self
477 where
478 T: Into<String>,
479 {
480 self.language_code = Some(value.into());
481 self
482 }
483
484 pub fn with_scope(mut self, value: BotCommandScope) -> Self {
490 self.scope = Some(value);
491 self
492 }
493}
494
495impl Method for GetBotCommands {
496 type Response = Vec<BotCommand>;
497
498 fn into_payload(self) -> Payload {
499 Payload::json("getMyCommands", self)
500 }
501}
502
503#[serde_with::skip_serializing_none]
505#[derive(Clone, Copy, Debug, Default, Serialize)]
506pub struct GetBotDefaultAdministratorRights {
507 for_channels: Option<bool>,
508}
509
510impl GetBotDefaultAdministratorRights {
511 pub fn with_for_channels(mut self, value: bool) -> Self {
517 self.for_channels = Some(value);
518 self
519 }
520}
521
522impl Method for GetBotDefaultAdministratorRights {
523 type Response = ChatAdministratorRights;
524
525 fn into_payload(self) -> Payload {
526 Payload::json("getMyDefaultAdministratorRights", self)
527 }
528}
529
530#[serde_with::skip_serializing_none]
532#[derive(Clone, Debug, Default, Serialize)]
533pub struct GetBotDescription {
534 language_code: Option<String>,
535}
536
537impl GetBotDescription {
538 pub fn with_language_code<T>(mut self, value: T) -> Self
544 where
545 T: Into<String>,
546 {
547 self.language_code = Some(value.into());
548 self
549 }
550}
551
552impl Method for GetBotDescription {
553 type Response = BotDescription;
554
555 fn into_payload(self) -> Payload {
556 Payload::json("getMyDescription", self)
557 }
558}
559
560#[serde_with::skip_serializing_none]
562#[derive(Clone, Debug, Default, Serialize)]
563pub struct GetBotName {
564 language_code: Option<String>,
565}
566
567impl GetBotName {
568 pub fn with_language_code<T>(mut self, value: T) -> Self
574 where
575 T: Into<String>,
576 {
577 self.language_code = Some(value.into());
578 self
579 }
580}
581
582impl Method for GetBotName {
583 type Response = BotName;
584
585 fn into_payload(self) -> Payload {
586 Payload::json("getMyName", self)
587 }
588}
589
590#[serde_with::skip_serializing_none]
592#[derive(Clone, Debug, Default, Serialize)]
593pub struct GetBotShortDescription {
594 language_code: Option<String>,
595}
596
597impl GetBotShortDescription {
598 pub fn with_language_code<T>(mut self, value: T) -> Self
604 where
605 T: Into<String>,
606 {
607 self.language_code = Some(value.into());
608 self
609 }
610}
611
612impl Method for GetBotShortDescription {
613 type Response = BotShortDescription;
614
615 fn into_payload(self) -> Payload {
616 Payload::json("getMyShortDescription", self)
617 }
618}
619
620#[derive(Clone, Copy, Debug)]
622pub struct GetBotStarBalance;
623
624impl Method for GetBotStarBalance {
625 type Response = StarAmount;
626
627 fn into_payload(self) -> Payload {
628 Payload::empty("getMyStarBalance")
629 }
630}
631
632#[derive(Clone, Copy, Debug)]
640pub struct LogOut;
641
642impl Method for LogOut {
643 type Response = bool;
644
645 fn into_payload(self) -> Payload {
646 Payload::empty("logOut")
647 }
648}
649
650#[serde_with::skip_serializing_none]
652#[derive(Clone, Debug, Serialize)]
653pub struct SetBotCommands {
654 commands: Vec<BotCommand>,
655 language_code: Option<String>,
656 scope: Option<BotCommandScope>,
657}
658
659impl SetBotCommands {
660 pub fn new(commands: impl IntoIterator<Item = BotCommand>) -> Self {
666 Self {
667 commands: commands.into_iter().collect(),
668 language_code: None,
669 scope: None,
670 }
671 }
672
673 pub fn with_language_code<T>(mut self, value: T) -> Self
681 where
682 T: Into<String>,
683 {
684 self.language_code = Some(value.into());
685 self
686 }
687
688 pub fn with_scope(mut self, value: BotCommandScope) -> Self {
695 self.scope = Some(value);
696 self
697 }
698}
699
700impl Method for SetBotCommands {
701 type Response = bool;
702
703 fn into_payload(self) -> Payload {
704 Payload::json("setMyCommands", self)
705 }
706}
707
708#[serde_with::skip_serializing_none]
714#[derive(Clone, Copy, Debug, Default, Serialize)]
715pub struct SetBotDefaultAdministratorRights {
716 for_channels: Option<bool>,
717 rights: Option<ChatAdministratorRights>,
718}
719
720impl SetBotDefaultAdministratorRights {
721 pub fn with_for_channels(mut self, value: bool) -> Self {
727 self.for_channels = Some(value);
728 self
729 }
730
731 pub fn with_rights(mut self, value: ChatAdministratorRights) -> Self {
738 self.rights = Some(value);
739 self
740 }
741}
742
743impl Method for SetBotDefaultAdministratorRights {
744 type Response = bool;
745
746 fn into_payload(self) -> Payload {
747 Payload::json("setMyDefaultAdministratorRights", self)
748 }
749}
750
751#[serde_with::skip_serializing_none]
753#[derive(Clone, Debug, Default, Serialize)]
754pub struct SetBotDescription {
755 description: Option<String>,
756 language_code: Option<String>,
757}
758
759impl SetBotDescription {
760 pub fn with_description<T>(mut self, value: T) -> Self
768 where
769 T: Into<String>,
770 {
771 self.description = Some(value.into());
772 self
773 }
774
775 pub fn with_language_code<T>(mut self, value: T) -> Self
783 where
784 T: Into<String>,
785 {
786 self.language_code = Some(value.into());
787 self
788 }
789}
790
791impl Method for SetBotDescription {
792 type Response = bool;
793
794 fn into_payload(self) -> Payload {
795 Payload::json("setMyDescription", self)
796 }
797}
798
799#[serde_with::skip_serializing_none]
801#[derive(Clone, Debug, Default, Serialize)]
802pub struct SetBotName {
803 language_code: Option<String>,
804 name: Option<String>,
805}
806
807impl SetBotName {
808 pub fn with_language_code<T>(mut self, value: T) -> Self
816 where
817 T: Into<String>,
818 {
819 self.language_code = Some(value.into());
820 self
821 }
822
823 pub fn with_name<T>(mut self, value: T) -> Self
831 where
832 T: Into<String>,
833 {
834 self.name = Some(value.into());
835 self
836 }
837}
838
839impl Method for SetBotName {
840 type Response = bool;
841
842 fn into_payload(self) -> Payload {
843 Payload::json("setMyName", self)
844 }
845}
846
847#[serde_with::skip_serializing_none]
850#[derive(Clone, Debug, Default, Serialize)]
851pub struct SetBotShortDescription {
852 language_code: Option<String>,
853 short_description: Option<String>,
854}
855
856impl SetBotShortDescription {
857 pub fn with_language_code<T>(mut self, value: T) -> Self
865 where
866 T: Into<String>,
867 {
868 self.language_code = Some(value.into());
869 self
870 }
871
872 pub fn with_short_description<T>(mut self, value: T) -> Self
880 where
881 T: Into<String>,
882 {
883 self.short_description = Some(value.into());
884 self
885 }
886}
887
888impl Method for SetBotShortDescription {
889 type Response = bool;
890
891 fn into_payload(self) -> Payload {
892 Payload::json("setMyShortDescription", self)
893 }
894}