tgbot 0.44.0

A Telegram Bot library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
use serde::{Deserialize, Serialize};

use crate::{
    api::{Form, Method, Payload},
    types::{
        AcceptedGiftTypes,
        Chat,
        InputProfilePhoto,
        InputProfilePhotoError,
        InputStoryContent,
        InputStoryContentError,
        Integer,
        Location,
        ParseMode,
        StarAmount,
        Sticker,
        Story,
        StoryAreas,
        StoryAreasError,
        TextEntities,
        TextEntity,
        TextEntityError,
        User,
    },
};

/// Represents the rights of a business bot
#[serde_with::skip_serializing_none]
#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct BusinessBotRights {
    /// Whether the bot can change the privacy settings pertaining to gifts for the business account.
    pub can_change_gift_settings: Option<bool>,
    /// Whether the bot can convert regular gifts owned by the business account to Telegram Stars.
    pub can_convert_gifts_to_stars: Option<bool>,
    /// Whether the bot can delete all private messages in managed chats.
    pub can_delete_all_messages: Option<bool>,
    /// Whether the bot can delete messages sent by the bot.
    pub can_delete_outgoing_messages: Option<bool>,
    /// Whether the bot can edit the bio of the business account.
    pub can_edit_bio: Option<bool>,
    /// Whether the bot can edit the first and last name of the business account.
    pub can_edit_name: Option<bool>,
    /// Whether the bot can edit the profile photo of the business account.
    pub can_edit_profile_photo: Option<bool>,
    /// Whether the bot can edit the username of the business account.
    pub can_edit_username: Option<bool>,
    /// Whether the bot can post, edit and delete stories on behalf of the business account.
    pub can_manage_stories: Option<bool>,
    /// Whether the bot can mark incoming private messages as read.
    pub can_read_messages: Option<bool>,
    /// Whether the bot can send and edit messages in the private chats
    /// that had incoming messages in the last 24 hours.
    pub can_reply: Option<bool>,
    /// Whether the bot can transfer and upgrade gifts owned by the business account.
    pub can_transfer_and_upgrade_gifts: Option<bool>,
    /// Whether the bot can transfer Telegram Stars received by the business account to its own account,
    /// or use them to upgrade and transfer gifts.
    pub can_transfer_stars: Option<bool>,
    /// Whether the bot can view gifts and the amount of Telegram Stars owned by the business account.
    pub can_view_gifts_and_stars: Option<bool>,
}

impl BusinessBotRights {
    /// Sets a new value for the `can_change_gift_settings` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can change the privacy settings pertaining to gifts for the business account.
    pub fn with_can_change_gift_settings(mut self, value: bool) -> Self {
        self.can_change_gift_settings = Some(value);
        self
    }

    /// Sets a new value for the `can_convert_gifts_to_stars` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can convert regular gifts owned by the business account to Telegram Stars.
    pub fn with_can_convert_gifts_to_stars(mut self, value: bool) -> Self {
        self.can_convert_gifts_to_stars = Some(value);
        self
    }

    /// Sets a new value for the `can_delete_all_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can delete all private messages in managed chats.
    pub fn with_can_delete_all_messages(mut self, value: bool) -> Self {
        self.can_delete_all_messages = Some(value);
        self
    }

    /// Sets a new value for the `can_delete_outgoing_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can delete messages sent by the bot.
    pub fn with_can_delete_outgoing_messages(mut self, value: bool) -> Self {
        self.can_delete_outgoing_messages = Some(value);
        self
    }

    /// Sets a new value for the `can_edit_bio` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can edit the bio of the business account.
    pub fn with_can_edit_bio(mut self, value: bool) -> Self {
        self.can_edit_bio = Some(value);
        self
    }

    /// Sets a new value for the `can_edit_name` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can edit the first and last name of the business account.
    pub fn with_can_edit_name(mut self, value: bool) -> Self {
        self.can_edit_name = Some(value);
        self
    }

    /// Sets a new value for the `can_edit_profile_photo` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can edit the profile photo of the business account.
    pub fn with_can_edit_profile_photo(mut self, value: bool) -> Self {
        self.can_edit_profile_photo = Some(value);
        self
    }

    /// Sets a new value for the `can_edit_username` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can edit the username of the business account.
    pub fn with_can_edit_username(mut self, value: bool) -> Self {
        self.can_edit_username = Some(value);
        self
    }

    /// Sets a new value for the `can_manage_stories` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can post, edit and delete stories on behalf of the business account.
    pub fn with_can_manage_stories(mut self, value: bool) -> Self {
        self.can_manage_stories = Some(value);
        self
    }

    /// Sets a new value for the `can_read_messages` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can mark incoming private messages as read.
    pub fn with_can_read_messages(mut self, value: bool) -> Self {
        self.can_read_messages = Some(value);
        self
    }

    /// Sets a new value for the `can_reply` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can send and edit messages in the private chats
    ///   that had incoming messages in the last 24 hours.
    pub fn with_can_reply(mut self, value: bool) -> Self {
        self.can_reply = Some(value);
        self
    }

    /// Sets a new value for the `can_transfer_and_upgrade_gifts` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can transfer and upgrade gifts owned by the business account.
    pub fn with_can_transfer_and_upgrade_gifts(mut self, value: bool) -> Self {
        self.can_transfer_and_upgrade_gifts = Some(value);
        self
    }

    /// Sets a new value for the `can_transfer_stars` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can transfer Telegram Stars received by the business account to its own account,
    ///   or use them to upgrade and transfer gifts.
    pub fn with_can_transfer_stars(mut self, value: bool) -> Self {
        self.can_transfer_stars = Some(value);
        self
    }

    /// Sets a new value for the `can_view_gifts_and_stars` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the bot can view gifts and the amount of Telegram Stars owned by the business account.
    pub fn with_can_view_gifts_and_stars(mut self, value: bool) -> Self {
        self.can_view_gifts_and_stars = Some(value);
        self
    }
}

/// Describes the connection of the bot with a business account.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct BusinessConnection {
    /// Date the connection was established in Unix time.
    pub date: Integer,
    /// Unique identifier of the business connection.
    pub id: String,
    /// Whether the connection is active.
    pub is_enabled: bool,
    /// Business account user that created the business connection.
    pub user: User,
    /// Identifier of a private chat with the user who created the business connection.
    pub user_chat_id: Integer,
    /// Rights of the business bot.
    pub rights: Option<BusinessBotRights>,
}

impl BusinessConnection {
    /// Creates a new `BusinessConnection`.
    ///
    /// # Arguments
    ///
    /// * `date` - Date the connection was established in Unix time.
    /// * `id` - Unique identifier of the business connection.
    /// * `user` - Business account user that created the business connection.
    /// * `user_chat_id` - Identifier of a private chat with the user who created the business connection.
    pub fn new<T>(date: Integer, id: T, user: User, user_chat_id: Integer) -> Self
    where
        T: Into<String>,
    {
        Self {
            date,
            id: id.into(),
            is_enabled: false,
            user,
            user_chat_id,
            rights: None,
        }
    }

    /// Sets a new value for the `is_enabled` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the connection is active.
    pub fn with_is_enabled(mut self, value: bool) -> Self {
        self.is_enabled = value;
        self
    }

    /// Sets a new rights.
    ///
    /// # Arguments
    ///
    /// * `value` - Rights of the business bot.
    pub fn with_rights(mut self, value: BusinessBotRights) -> Self {
        self.rights = Some(value);
        self
    }
}

/// Represents the intro of the business.
#[serde_with::skip_serializing_none]
#[derive(Clone, Default, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct BusinessIntro {
    /// Message text of the business intro.
    pub message: Option<String>,
    /// Sticker of the business intro.
    pub sticker: Option<Sticker>,
    /// Title text of the business intro.
    pub title: Option<String>,
}

impl BusinessIntro {
    /// Sets a new message.
    ///
    /// # Arguments
    ///
    /// * `value` - .Message text of the business intro.
    pub fn with_message<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.message = Some(value.into());
        self
    }

    /// Sets a new sticker.
    ///
    /// # Arguments
    ///
    /// * `value` - .Sticker of the business intro.
    pub fn with_sticker(mut self, value: Sticker) -> Self {
        self.sticker = Some(value);
        self
    }

    /// Sets a new title.
    ///
    /// # Arguments
    ///
    /// * `value` - .Title text of the business intro.
    pub fn with_title<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.title = Some(value.into());
        self
    }
}

/// Provides information about address and location of the business.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct BusinessLocation {
    /// Address of the business.
    pub address: String,
    /// Location of the business.
    pub location: Option<Location>,
}

impl BusinessLocation {
    /// Creates a new `BusinessLocation`.
    ///
    /// # Arguments
    ///
    /// * `address` - Address of the business.
    pub fn new<T>(address: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            address: address.into(),
            location: None,
        }
    }

    /// Sets a new location.
    ///
    /// # Arguments
    ///
    /// * `value` - Location of the business.
    pub fn with_location(mut self, value: Location) -> Self {
        self.location = Some(value);
        self
    }
}

/// Provides information about messages deleted from a connected business account.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct BusinessMessagesDeleted {
    /// Unique identifier of the business connection.
    pub business_connection_id: String,
    /// Information about a chat in the business account.
    /// The bot may not have access to the chat or the corresponding user.
    pub chat: Chat,
    /// A list of identifiers of deleted messages in the chat of the business account.
    pub message_ids: Vec<Integer>,
}

impl BusinessMessagesDeleted {
    /// Creates a new `BusinessMessagesDeleted`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `chat` - Information about a chat in the business account.
    /// * `message_ids` - A list of identifiers of deleted messages in the chat of the business account.
    pub fn new<A, B, C>(business_connection_id: A, chat: B, message_ids: C) -> Self
    where
        A: Into<String>,
        B: Into<Chat>,
        C: IntoIterator<Item = Integer>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            chat: chat.into(),
            message_ids: message_ids.into_iter().collect(),
        }
    }
}

/// Provides information about the time interval describing business opening hours.
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct BusinessOpeningHoursInterval {
    /// The minute's sequence number in a week, starting on Monday,
    /// marking the start of the time interval during which the business is open; 0 - 7 24 60.
    pub opening_minute: Integer,
    /// The minute's sequence number in a week, starting on Monday,
    /// marking the end of the time interval during which the business is open; 0 - 8 24 60.
    pub closing_minute: Integer,
}

impl BusinessOpeningHoursInterval {
    /// Creates a new `BusinessOpeningHoursInterval`.
    ///
    /// # Arguments
    ///
    /// * `opening_minute` - Start of the time interval during which the business is open.
    /// * `closing_minute` - End of the time interval during which the business is open.
    pub fn new(opening_minute: Integer, closing_minute: Integer) -> Self {
        Self {
            opening_minute,
            closing_minute,
        }
    }
}

impl From<(Integer, Integer)> for BusinessOpeningHoursInterval {
    fn from((opening_minute, closing_minute): (Integer, Integer)) -> Self {
        Self::new(opening_minute, closing_minute)
    }
}

/// Provides information about the opening hours of the business.
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct BusinessOpeningHours {
    /// Unique name of the time zone for which the opening hours are defined.
    pub time_zone_name: String,
    /// List of time intervals describing business opening hours.
    pub opening_hours: Vec<BusinessOpeningHoursInterval>,
}

impl BusinessOpeningHours {
    /// Creates a new `BusinessOpeningHours`.
    ///
    /// # Arguments
    ///
    /// * `time_zone_name` - Unique name of the time zone for which the opening hours are defined.
    /// * `opening_hours` - List of time intervals describing business opening hours.
    pub fn new<A, B, C>(time_zone_name: A, opening_hours: B) -> Self
    where
        A: Into<String>,
        B: IntoIterator<Item = C>,
        C: Into<BusinessOpeningHoursInterval>,
    {
        Self {
            time_zone_name: time_zone_name.into(),
            opening_hours: opening_hours.into_iter().map(Into::into).collect(),
        }
    }
}

/// Converts a given regular gift to Telegram Stars.
///
/// Requires the can_convert_gifts_to_stars business bot right.
#[derive(Clone, Debug, Serialize)]
pub struct ConvertGiftToStars {
    business_connection_id: String,
    owned_gift_id: String,
}

impl ConvertGiftToStars {
    /// Creates a new `ConvertGiftToStars`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `owned_gift_id` - Unique identifier of the regular gift that should be converted to Telegram Stars.
    pub fn new<A, B>(business_connection_id: A, owned_gift_id: B) -> Self
    where
        A: Into<String>,
        B: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            owned_gift_id: owned_gift_id.into(),
        }
    }
}

impl Method for ConvertGiftToStars {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("convertGiftToStars", self)
    }
}

/// Deletes messages on behalf of a business account.
///
/// Requires the `can_delete_outgoing_messages` business bot right to delete messages sent by the bot itself,
/// or the `can_delete_all_messages` business bot right to delete any message.
#[derive(Clone, Debug, Serialize)]
pub struct DeleteBusinessMessages {
    business_connection_id: String,
    message_ids: Vec<Integer>,
}

impl DeleteBusinessMessages {
    /// Creates a new `DeleteBusinessMessages`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection
    ///   on behalf of which to delete the messages.
    /// * `message_ids` - A list of 1-100 identifiers of messages to delete;
    ///   all messages must be from the same chat;
    ///   see deleteMessage for limitations on which messages can be deleted.
    pub fn new<A, B>(business_connection_id: A, message_ids: B) -> Self
    where
        A: Into<String>,
        B: IntoIterator<Item = Integer>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            message_ids: message_ids.into_iter().collect(),
        }
    }
}

impl Method for DeleteBusinessMessages {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("deleteBusinessMessages", self)
    }
}

/// Deletes a story previously posted by the bot on behalf of a managed business account.
///
/// Requires the can_manage_stories business bot right.
#[derive(Clone, Debug, Serialize)]
pub struct DeleteStory {
    business_connection_id: String,
    story_id: Integer,
}

impl DeleteStory {
    /// Creates a new `DeleteStory`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `story_id` - Unique identifier of the story to delete.
    pub fn new<T>(business_connection_id: T, story_id: Integer) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            story_id,
        }
    }
}

impl Method for DeleteStory {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("deleteStory", self)
    }
}

/// Edits a story previously posted by the bot on behalf of a managed business account.
///
/// Requires the `can_manage_stories` business bot right.
pub struct EditStory {
    form: Form,
}

impl EditStory {
    /// Creates a new `EditStory`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `content` - Content of the story.
    /// * `story_id` - Unique identifier of the story to edit.
    pub fn new<A, B>(business_connection_id: A, content: B, story_id: Integer) -> Result<Self, InputStoryContentError>
    where
        A: Into<String>,
        B: Into<InputStoryContent>,
    {
        let mut form: Form = content.into().try_into()?;
        form.insert_field("business_connection_id", business_connection_id.into());
        form.insert_field("story_id", story_id);
        Ok(Self { form })
    }

    /// Sets a new list of areas.
    ///
    /// # Arguments
    ///
    /// * `value` - A list of clickable areas to be shown on the story.
    pub fn with_areas<T>(mut self, value: T) -> Result<Self, StoryAreasError>
    where
        T: Into<StoryAreas>,
    {
        let value = value.into().serialize()?;
        self.form.insert_field("areas", value);
        Ok(self)
    }

    /// Sets a new caption.
    ///
    /// # Arguments
    ///
    /// * `value` -  Caption of the story, 0-2048 characters after entities parsing.
    pub fn with_caption<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.form.insert_field("caption", value.into());
        self
    }

    /// Sets a new list of caption entities.
    ///
    /// # Arguments
    ///
    /// * `value` - A list of special entities that appear in the caption.
    pub fn with_caption_entities<T>(mut self, value: T) -> Result<Self, TextEntityError>
    where
        T: IntoIterator<Item = TextEntity>,
    {
        let value = TextEntities::from_iter(value);
        let value = value.serialize()?;
        self.form.remove_field("parse_mode");
        self.form.insert_field("caption_entities", value);
        Ok(self)
    }

    /// Sets a new parse mode.
    ///
    /// # Arguments
    ///
    /// * `value` - Mode for parsing entities in the story caption.
    pub fn with_parse_mode(mut self, value: ParseMode) -> Self {
        self.form.remove_field("caption_entities");
        self.form.insert_field("parse_mode", value);
        self
    }
}

impl Method for EditStory {
    type Response = Story;

    fn into_payload(self) -> Payload {
        Payload::form("editStory", self.form)
    }
}

/// Returns the amount of Telegram Stars owned by a managed business account.
///
/// Requires the can_view_gifts_and_stars business bot right.
#[derive(Clone, Debug, Serialize)]
pub struct GetBusinessAccountStarBalance {
    business_connection_id: String,
}

impl GetBusinessAccountStarBalance {
    /// Creates a new `GetBusinessAccountStarBalance`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    pub fn new<T>(business_connection_id: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
        }
    }
}

impl Method for GetBusinessAccountStarBalance {
    type Response = StarAmount;

    fn into_payload(self) -> Payload {
        Payload::json("getBusinessAccountStarBalance", self)
    }
}

/// Returns information about the connection of the bot with a business account.
#[derive(Clone, Debug, Serialize)]
pub struct GetBusinessConnection {
    business_connection_id: String,
}

impl GetBusinessConnection {
    /// Creates a new `GetBusinessConnection`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    pub fn new<T>(business_connection_id: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
        }
    }
}

impl Method for GetBusinessConnection {
    type Response = BusinessConnection;

    fn into_payload(self) -> Payload {
        Payload::json("getBusinessConnection", self)
    }
}

/// Posts a story on behalf of a managed business account.
///
/// Requires the `can_manage_stories` business bot right.
pub struct PostStory {
    form: Form,
}

impl PostStory {
    /// Creates a new `PostStory`.
    ///
    /// # Arguments
    ///
    /// * `active_period` - Period after which the story is moved to the archive, in seconds;
    ///   must be one of 6 * 3600, 12 * 3600, 86400, or 2 * 86400.
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `content` - Content of the story.
    pub fn new<A, B>(
        active_period: Integer,
        business_connection_id: A,
        content: B,
    ) -> Result<Self, InputStoryContentError>
    where
        A: Into<String>,
        B: Into<InputStoryContent>,
    {
        let mut form: Form = content.into().try_into()?;
        form.insert_field("active_period", active_period);
        form.insert_field("business_connection_id", business_connection_id.into());
        Ok(Self { form })
    }

    /// Sets a new list of areas.
    ///
    /// # Arguments
    ///
    /// * `value` - A list of clickable areas to be shown on the story.
    pub fn with_areas<T>(mut self, value: T) -> Result<Self, StoryAreasError>
    where
        T: Into<StoryAreas>,
    {
        let value = value.into().serialize()?;
        self.form.insert_field("areas", value);
        Ok(self)
    }

    /// Sets a new caption.
    ///
    /// # Arguments
    ///
    /// * `value` -  Caption of the story, 0-2048 characters after entities parsing.
    pub fn with_caption<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.form.insert_field("caption", value.into());
        self
    }

    /// Sets a new list of caption entities.
    ///
    /// # Arguments
    ///
    /// * `value` - A list of special entities that appear in the caption.
    pub fn with_caption_entities<T>(mut self, value: T) -> Result<Self, TextEntityError>
    where
        T: IntoIterator<Item = TextEntity>,
    {
        let value = TextEntities::from_iter(value);
        let value = value.serialize()?;
        self.form.remove_field("parse_mode");
        self.form.insert_field("caption_entities", value);
        Ok(self)
    }

    /// Sets a new parse mode.
    ///
    /// # Arguments
    ///
    /// * `value` - Mode for parsing entities in the story caption.
    pub fn with_parse_mode(mut self, value: ParseMode) -> Self {
        self.form.remove_field("caption_entities");
        self.form.insert_field("parse_mode", value);
        self
    }

    /// Sets a new value for the `post_to_chat_page` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether to keep the story accessible after it expires.
    pub fn with_post_to_chat_page(mut self, value: bool) -> Self {
        self.form.insert_field("post_to_chat_page", value);
        self
    }

    /// Sets a new value for the `protect_content` flag.
    ///
    /// # Arguments
    ///
    /// * `value` Whether the content of the story must be protected from forwarding and screenshotting.
    pub fn with_protect_content(mut self, value: bool) -> Self {
        self.form.insert_field("protect_content", value);
        self
    }
}

impl Method for PostStory {
    type Response = Story;

    fn into_payload(self) -> Payload {
        Payload::form("postStory", self.form)
    }
}

/// Marks incoming message as read on behalf of a business account.
///
/// Requires the can_read_messages business bot right.
#[derive(Clone, Debug, Serialize)]
pub struct ReadBusinessMessage {
    business_connection_id: String,
    chat_id: Integer,
    message_id: Integer,
}

impl ReadBusinessMessage {
    /// Creates a new `ReadBusinessMessage`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection
    ///   on behalf of which to read the message.
    /// * `chat_id` - Unique identifier of the chat in which the message was received;
    ///   the chat must have been active in the last 24 hours.
    /// * `message_id` - Unique identifier of the message to mark as read.
    pub fn new<T>(business_connection_id: T, chat_id: Integer, message_id: Integer) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            chat_id,
            message_id,
        }
    }
}

impl Method for ReadBusinessMessage {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("readBusinessMessage", self)
    }
}

/// Removes the current profile photo of a managed business account.
///
/// Requires the can_edit_profile_photo business bot right.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct RemoveBusinessAccountProfilePhoto {
    business_connection_id: String,
    is_public: Option<bool>,
}

impl RemoveBusinessAccountProfilePhoto {
    /// Creates a new `RemoveBusinessAccountProfilePhoto`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection
    pub fn new<T>(business_connection_id: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            is_public: None,
        }
    }

    /// Sets a new value for the `is_public` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether to remove the public photo,
    ///   which is visible even if the main photo is hidden by the business account's privacy settings;
    ///   after the main photo is removed, the previous profile photo (if present) becomes the main photo.
    pub fn with_is_public(mut self, value: bool) -> Self {
        self.is_public = Some(value);
        self
    }
}

impl Method for RemoveBusinessAccountProfilePhoto {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("removeBusinessAccountProfilePhoto", self)
    }
}

/// Changes the bio of a managed business account.
///
/// Requires the can_change_bio business bot right.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct SetBusinessAccountBio {
    business_connection_id: String,
    bio: Option<String>,
}

impl SetBusinessAccountBio {
    /// Creates a new `SetBusinessAccountBio`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection
    pub fn new<T>(business_connection_id: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            bio: None,
        }
    }

    /// Sets a new bio
    ///
    /// # Arguments
    ///
    // * `value` - The new value of the bio for the business account; 0-140 characters.
    pub fn with_bio<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.bio = Some(value.into());
        self
    }
}

impl Method for SetBusinessAccountBio {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("setBusinessAccountBio", self)
    }
}

/// Changes the privacy settings pertaining to incoming gifts in a managed business account.
///
/// Requires the can_change_gift_settings business bot right.
#[derive(Clone, Debug, Serialize)]
pub struct SetBusinessAccountGiftSettings {
    accepted_gift_types: AcceptedGiftTypes,
    business_connection_id: String,
    show_gift_button: bool,
}

impl SetBusinessAccountGiftSettings {
    /// Creates a new `SetBusinessAccountGiftSettings`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `show_gift_button` - Whether a button for sending a gift to the user
    ///   or by the business account must always be shown in the input field
    /// * `accepted_gift_types` - Types of gifts accepted by the business account.
    pub fn new<T>(business_connection_id: T, show_gift_button: bool, accepted_gift_types: AcceptedGiftTypes) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            show_gift_button,
            accepted_gift_types,
        }
    }
}

impl Method for SetBusinessAccountGiftSettings {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("setBusinessAccountGiftSettings", self)
    }
}

/// Changes the first and last name of a managed business account.
///
/// Requires the can_change_name business bot right.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct SetBusinessAccountName {
    business_connection_id: String,
    first_name: String,
    last_name: Option<String>,
}

impl SetBusinessAccountName {
    /// Creates a new `SetBusinessAccountName`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `first_name` - The new value of the first name for the business account; 1-64 characters.
    pub fn new<A, B>(business_connection_id: A, first_name: B) -> Self
    where
        A: Into<String>,
        B: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            first_name: first_name.into(),
            last_name: None,
        }
    }

    /// Sets a new last name.
    ///
    /// # Arguments
    ///
    /// * `value` - The new value of the last name for the business account; 0-64 characters.
    pub fn with_last_name<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.last_name = Some(value.into());
        self
    }
}

impl Method for SetBusinessAccountName {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("setBusinessAccountName", self)
    }
}

/// Changes the profile photo of a managed business account.
///
/// Requires the can_edit_profile_photo business bot right.
#[derive(Debug)]
pub struct SetBusinessAccountProfilePhoto {
    form: Form,
}

impl SetBusinessAccountProfilePhoto {
    /// Creates a new `SetBusinessAccountProfilePhoto`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `photo` - The new profile photo to set.
    pub fn new<A, B>(business_connection_id: A, photo: B) -> Result<Self, InputProfilePhotoError>
    where
        A: Into<String>,
        B: Into<InputProfilePhoto>,
    {
        let mut form = Form::try_from(photo.into())?;
        form.insert_field("business_connection_id", business_connection_id.into());
        Ok(Self { form })
    }

    /// Sets a new value for the `is_public` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether to set the public photo,
    ///   which will be visible even if the main photo is hidden by the business account's privacy settings;
    ///   an account can have only one public photo.
    pub fn with_is_public(mut self, value: bool) -> Self {
        self.form.insert_field("is_public", value);
        self
    }
}

impl Method for SetBusinessAccountProfilePhoto {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::form("setBusinessAccountProfilePhoto", self.form)
    }
}

/// Changes the username of a managed business account.
///
/// Requires the can_change_username business bot right.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct SetBusinessAccountUsername {
    business_connection_id: String,
    username: Option<String>,
}

impl SetBusinessAccountUsername {
    /// Creates a new `SetBusinessAccountUsername`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    pub fn new<T>(business_connection_id: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            username: None,
        }
    }

    /// Sets a new username
    ///
    /// # Arguments
    ///
    /// * `value` - The new value of the username for the business account; 0-32 characters.
    pub fn with_username<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.username = Some(value.into());
        self
    }
}

impl Method for SetBusinessAccountUsername {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("setBusinessAccountUsername", self)
    }
}

/// Transfers Telegram Stars from the business account balance to the bot's balance.
///
/// Requires the can_transfer_stars business bot right.
#[derive(Clone, Debug, Serialize)]
pub struct TransferBusinessAccountStars {
    business_connection_id: String,
    star_count: Integer,
}

impl TransferBusinessAccountStars {
    /// Creates a new `TransferBusinessAccountStars`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `star_count` - Number of Telegram Stars to transfer; 1-10000.
    pub fn new<T>(business_connection_id: T, star_count: Integer) -> Self
    where
        T: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            star_count,
        }
    }
}

impl Method for TransferBusinessAccountStars {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("transferBusinessAccountStars", self)
    }
}

/// Transfers an owned unique gift to another user.
///
/// Requires the `can_transfer_and_upgrade_gifts` business bot right.
/// Requires `can_transfer_stars` business bot right if the transfer is paid.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct TransferGift {
    business_connection_id: String,
    owned_gift_id: String,
    new_owner_chat_id: Integer,
    star_count: Option<Integer>,
}

impl TransferGift {
    /// Creates a new `TransferGift`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `owned_gift_id` - Unique identifier of the regular gift that should be transferred.
    /// * `new_owner_chat_id` - Unique identifier of the chat which will own the gift;
    ///   the chat must be active in the last 24 hours.
    pub fn new<A, B>(business_connection_id: A, owned_gift_id: B, new_owner_chat_id: Integer) -> Self
    where
        A: Into<String>,
        B: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            owned_gift_id: owned_gift_id.into(),
            new_owner_chat_id,
            star_count: None,
        }
    }

    /// Sets a new star count
    ///
    /// # Arguments
    ///
    /// * `value` - The amount of Telegram Stars that will be paid for the transfer from the business account balance;
    ///   if positive, then the can_transfer_stars business bot right is required.
    pub fn with_star_count(mut self, value: Integer) -> Self {
        self.star_count = Some(value);
        self
    }
}

impl Method for TransferGift {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("transferGift", self)
    }
}

/// Upgrades a given regular gift to a unique gift.
///
/// Requires the can_transfer_and_upgrade_gifts business bot right.
/// Additionally requires the can_transfer_stars business bot right if the upgrade is paid.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct UpgradeGift {
    business_connection_id: String,
    owned_gift_id: String,
    keep_original_details: Option<bool>,
    star_count: Option<Integer>,
}

impl UpgradeGift {
    /// Creates a new `UpgradeGift`.
    ///
    /// # Arguments
    ///
    /// * `business_connection_id` - Unique identifier of the business connection.
    /// * `owned_gift_id` - Unique identifier of the regular gift that should be upgraded to a unique one.
    pub fn new<A, B>(business_connection_id: A, owned_gift_id: B) -> Self
    where
        A: Into<String>,
        B: Into<String>,
    {
        Self {
            business_connection_id: business_connection_id.into(),
            owned_gift_id: owned_gift_id.into(),
            keep_original_details: None,
            star_count: None,
        }
    }

    /// Sets a new value for the `keep_original_details` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether to keep the original gift text, sender and receiver in the upgraded gift.
    pub fn with_keep_original_details(mut self, value: bool) -> Self {
        self.keep_original_details = Some(value);
        self
    }

    /// Sets a new star count.
    ///
    /// If `gift.prepaid_upgrade_star_count > 0`, then pass 0,
    /// otherwise, the `can_transfer_stars` business bot right is required and `gift.upgrade_star_count` must be passed.
    ///
    /// * `value` - The amount of Telegram Stars that will be paid for the upgrade from the business account balance.
    pub fn with_star_count(mut self, value: Integer) -> Self {
        self.star_count = Some(value);
        self
    }
}

impl Method for UpgradeGift {
    type Response = bool;

    fn into_payload(self) -> Payload {
        Payload::json("upgradeGift", self)
    }
}