tele 0.1.19

Ergonomic Telegram Bot API SDK for Rust, built on reqx
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
use std::collections::BTreeMap;
use std::fmt::{Display, Write as _};
use std::str::FromStr;

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::types::common::{ChatId, MessageId, ParseMode};
use crate::types::message::MessageEntity;
use crate::{Error, Result};

pub const MAX_CALLBACK_DATA_BYTES: usize = 64;

fn invalid_request(reason: impl Into<String>) -> Error {
    Error::InvalidRequest {
        reason: reason.into(),
    }
}

fn validate_callback_data(data: impl Into<String>) -> Result<String> {
    let data = data.into();
    if data.trim().is_empty() {
        return Err(invalid_request("callback_data cannot be empty"));
    }
    if data.len() > MAX_CALLBACK_DATA_BYTES {
        return Err(invalid_request(format!(
            "callback_data exceeds Telegram's 64-byte limit ({})",
            data.len()
        )));
    }
    Ok(data)
}

fn is_compact_callback_safe(byte: u8) -> bool {
    matches!(
        byte,
        b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~'
    )
}

fn encode_compact_callback_segment(segment: &str) -> String {
    let mut encoded = String::with_capacity(segment.len());
    for byte in segment.as_bytes() {
        if is_compact_callback_safe(*byte) {
            encoded.push(*byte as char);
        } else {
            let _ = write!(&mut encoded, "%{byte:02X}");
        }
    }
    encoded
}

fn decode_hex_digit(byte: u8) -> Option<u8> {
    match byte {
        b'0'..=b'9' => Some(byte - b'0'),
        b'a'..=b'f' => Some(byte - b'a' + 10),
        b'A'..=b'F' => Some(byte - b'A' + 10),
        _ => None,
    }
}

fn decode_compact_callback_segment(segment: &str) -> Result<String> {
    let mut bytes = Vec::with_capacity(segment.len());
    let raw = segment.as_bytes();
    let mut index = 0;
    while index < raw.len() {
        if raw[index] == b'%' {
            let hi = *raw
                .get(index + 1)
                .ok_or_else(|| invalid_request("compact callback segment has truncated escape"))?;
            let lo = *raw
                .get(index + 2)
                .ok_or_else(|| invalid_request("compact callback segment has truncated escape"))?;
            let hi = decode_hex_digit(hi).ok_or_else(|| {
                invalid_request("compact callback segment contains invalid escape")
            })?;
            let lo = decode_hex_digit(lo).ok_or_else(|| {
                invalid_request("compact callback segment contains invalid escape")
            })?;
            bytes.push((hi << 4) | lo);
            index += 3;
        } else {
            bytes.push(raw[index]);
            index += 1;
        }
    }

    String::from_utf8(bytes)
        .map_err(|_| invalid_request("compact callback segment is not valid UTF-8"))
}

/// Pluggable callback payload codec for inline keyboard buttons and callback routers.
pub trait CallbackCodec<T>: Send + Sync + 'static {
    fn encode_callback_data(payload: &T) -> Result<String>;
    fn decode_callback_data(data: &str) -> Result<T>;
}

/// Adapter codec for payload types that implement [`CallbackPayload`].
#[derive(Clone, Copy, Debug, Default)]
pub struct CallbackPayloadCodec;

impl<T> CallbackCodec<T> for CallbackPayloadCodec
where
    T: CallbackPayload,
{
    fn encode_callback_data(payload: &T) -> Result<String> {
        payload.encode_callback_data()
    }

    fn decode_callback_data(data: &str) -> Result<T> {
        T::decode_callback_data(data)
    }
}

/// JSON callback codec for serde-serializable payloads.
#[derive(Clone, Copy, Debug, Default)]
pub struct JsonCallbackCodec;

impl<T> CallbackCodec<T> for JsonCallbackCodec
where
    T: Serialize + DeserializeOwned,
{
    fn encode_callback_data(payload: &T) -> Result<String> {
        let encoded =
            serde_json::to_string(payload).map_err(|source| Error::SerializeRequest { source })?;
        validate_callback_data(encoded)
    }

    fn decode_callback_data(data: &str) -> Result<T> {
        serde_json::from_str(data).map_err(|source| {
            invalid_request(format!("failed to decode callback payload: {source}"))
        })
    }
}

/// Builder for compact callback payload strings.
#[derive(Clone, Debug, Default)]
pub struct CompactCallbackEncoder {
    segments: Vec<String>,
}

impl CompactCallbackEncoder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn tag(&mut self, tag: impl AsRef<str>) -> Result<&mut Self> {
        let tag = tag.as_ref().trim();
        if tag.is_empty() {
            return Err(invalid_request("compact callback tag cannot be empty"));
        }
        self.segments.push(encode_compact_callback_segment(tag));
        Ok(self)
    }

    pub fn push(&mut self, value: impl AsRef<str>) -> Result<&mut Self> {
        self.segments
            .push(encode_compact_callback_segment(value.as_ref()));
        Ok(self)
    }

    pub fn push_display(&mut self, value: impl Display) -> Result<&mut Self> {
        self.push(value.to_string())
    }

    pub fn finish(self) -> Result<String> {
        if self.segments.is_empty() {
            return Err(invalid_request("compact callback payload cannot be empty"));
        }
        validate_callback_data(self.segments.join(":"))
    }
}

/// Decoder for compact callback payload strings.
#[derive(Clone, Debug)]
pub struct CompactCallbackDecoder {
    segments: Vec<String>,
    index: usize,
}

impl CompactCallbackDecoder {
    pub fn new(data: &str) -> Result<Self> {
        if data.is_empty() {
            return Err(invalid_request("compact callback payload cannot be empty"));
        }
        let segments = data
            .split(':')
            .map(decode_compact_callback_segment)
            .collect::<Result<Vec<_>>>()?;
        Ok(Self { segments, index: 0 })
    }

    pub fn expect_tag(&mut self, expected: &str) -> Result<&mut Self> {
        let actual = self.next_string("callback tag")?;
        if actual == expected {
            Ok(self)
        } else {
            Err(invalid_request(format!(
                "unexpected compact callback tag `{actual}`, expected `{expected}`"
            )))
        }
    }

    pub fn next_string(&mut self, field: &str) -> Result<String> {
        let value = self.segments.get(self.index).cloned().ok_or_else(|| {
            invalid_request(format!(
                "compact callback payload is missing required field `{field}`"
            ))
        })?;
        self.index += 1;
        Ok(value)
    }

    pub fn next_parse<T>(&mut self, field: &str) -> Result<T>
    where
        T: FromStr,
        T::Err: Display,
    {
        let raw = self.next_string(field)?;
        raw.parse().map_err(|source| {
            invalid_request(format!(
                "failed to parse compact callback field `{field}`: {source}"
            ))
        })
    }

    pub fn remaining(&self) -> usize {
        self.segments.len().saturating_sub(self.index)
    }

    pub fn finish(self) -> Result<()> {
        if self.remaining() == 0 {
            Ok(())
        } else {
            Err(invalid_request(format!(
                "compact callback payload has {} unexpected trailing field(s)",
                self.remaining()
            )))
        }
    }
}

/// Manual compact callback payload contract for 64-byte-friendly callback data.
pub trait CompactCallbackPayload: Sized {
    fn encode_compact(&self, encoder: &mut CompactCallbackEncoder) -> Result<()>;
    fn decode_compact(decoder: &mut CompactCallbackDecoder) -> Result<Self>;

    fn encode_compact_data(&self) -> Result<String> {
        let mut encoder = CompactCallbackEncoder::new();
        self.encode_compact(&mut encoder)?;
        encoder.finish()
    }

    fn decode_compact_data(data: &str) -> Result<Self> {
        let mut decoder = CompactCallbackDecoder::new(data)?;
        let payload = Self::decode_compact(&mut decoder)?;
        decoder.finish()?;
        Ok(payload)
    }
}

/// Compact callback codec backed by [`CompactCallbackPayload`].
#[derive(Clone, Copy, Debug, Default)]
pub struct CompactCallbackCodec;

impl<T> CallbackCodec<T> for CompactCallbackCodec
where
    T: CompactCallbackPayload,
{
    fn encode_callback_data(payload: &T) -> Result<String> {
        payload.encode_compact_data()
    }

    fn decode_callback_data(data: &str) -> Result<T> {
        T::decode_compact_data(data)
    }
}

/// Strongly-typed callback payload codec for inline keyboard buttons.
pub trait CallbackPayload: Sized {
    fn encode_callback_data(&self) -> Result<String>;
    fn decode_callback_data(data: &str) -> Result<Self>;
}

impl<T> CallbackPayload for T
where
    T: Serialize + DeserializeOwned,
{
    fn encode_callback_data(&self) -> Result<String> {
        JsonCallbackCodec::encode_callback_data(self)
    }

    fn decode_callback_data(data: &str) -> Result<Self> {
        JsonCallbackCodec::decode_callback_data(data)
    }
}

/// Generic inline query result payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct InlineQueryResult(pub Value);

impl InlineQueryResult {
    pub fn new(value: Value) -> Self {
        Self(value)
    }

    pub fn try_from_typed<T>(value: T) -> std::result::Result<Self, serde_json::Error>
    where
        T: Serialize,
    {
        serde_json::to_value(value).map(Self)
    }

    pub fn from_typed<T>(value: T) -> std::result::Result<Self, serde_json::Error>
    where
        T: Serialize,
    {
        Self::try_from_typed(value)
    }

    pub fn article(
        id: impl Into<String>,
        title: impl Into<String>,
        message_text: impl Into<String>,
    ) -> std::result::Result<Self, serde_json::Error> {
        InlineQueryResult::try_from(InlineQueryResultArticle::new(id, title, message_text))
    }

    pub fn as_value(&self) -> &Value {
        &self.0
    }

    pub fn into_value(self) -> Value {
        self.0
    }
}

impl From<Value> for InlineQueryResult {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<InlineQueryResult> for Value {
    fn from(value: InlineQueryResult) -> Self {
        value.0
    }
}

/// Input text content for inline query article results.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InputTextMessageContent {
    pub message_text: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parse_mode: Option<ParseMode>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub entities: Option<Vec<MessageEntity>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub link_preview_options: Option<LinkPreviewOptions>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub disable_web_page_preview: Option<bool>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl InputTextMessageContent {
    pub fn new(message_text: impl Into<String>) -> Self {
        Self {
            message_text: message_text.into(),
            parse_mode: None,
            entities: None,
            link_preview_options: None,
            disable_web_page_preview: None,
            extra: BTreeMap::new(),
        }
    }
}

/// Typed inline query article result.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InlineQueryResultArticle {
    #[serde(rename = "type")]
    pub kind: String,
    pub id: String,
    pub title: String,
    pub input_message_content: InputTextMessageContent,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reply_markup: Option<InlineKeyboardMarkup>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hide_url: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thumbnail_url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thumbnail_width: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thumbnail_height: Option<u32>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl InlineQueryResultArticle {
    pub fn new(
        id: impl Into<String>,
        title: impl Into<String>,
        message_text: impl Into<String>,
    ) -> Self {
        Self {
            kind: "article".to_owned(),
            id: id.into(),
            title: title.into(),
            input_message_content: InputTextMessageContent::new(message_text),
            reply_markup: None,
            url: None,
            hide_url: None,
            description: None,
            thumbnail_url: None,
            thumbnail_width: None,
            thumbnail_height: None,
            extra: BTreeMap::new(),
        }
    }
}

impl TryFrom<InlineQueryResultArticle> for InlineQueryResult {
    type Error = serde_json::Error;

    fn try_from(value: InlineQueryResultArticle) -> std::result::Result<Self, Self::Error> {
        Self::try_from_typed(value)
    }
}

/// Generic checklist input payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct InputChecklist(pub Value);

impl InputChecklist {
    pub fn new(value: Value) -> Self {
        Self(value)
    }
}

impl From<Value> for InputChecklist {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<InputChecklist> for Value {
    fn from(value: InputChecklist) -> Self {
        value.0
    }
}

/// Generic story content payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct InputStoryContent(pub Value);

impl InputStoryContent {
    pub fn new(value: Value) -> Self {
        Self(value)
    }
}

impl From<Value> for InputStoryContent {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<InputStoryContent> for Value {
    fn from(value: InputStoryContent) -> Self {
        value.0
    }
}

/// Generic story area payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct StoryArea(pub Value);

impl StoryArea {
    pub fn new(value: Value) -> Self {
        Self(value)
    }
}

impl From<Value> for StoryArea {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<StoryArea> for Value {
    fn from(value: StoryArea) -> Self {
        value.0
    }
}

/// Generic paid media item payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct InputPaidMedia(pub Value);

impl InputPaidMedia {
    pub fn new(value: Value) -> Self {
        Self(value)
    }
}

impl From<Value> for InputPaidMedia {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<InputPaidMedia> for Value {
    fn from(value: InputPaidMedia) -> Self {
        value.0
    }
}

/// Generic suggested-post payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct SuggestedPostParameters(pub Value);

impl SuggestedPostParameters {
    pub fn new(value: Value) -> Self {
        Self(value)
    }
}

impl From<Value> for SuggestedPostParameters {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<SuggestedPostParameters> for Value {
    fn from(value: SuggestedPostParameters) -> Self {
        value.0
    }
}

/// Generic accepted-gift-types payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AcceptedGiftTypes(pub Value);

impl AcceptedGiftTypes {
    pub fn new(value: Value) -> Self {
        Self(value)
    }
}

impl From<Value> for AcceptedGiftTypes {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<AcceptedGiftTypes> for Value {
    fn from(value: AcceptedGiftTypes) -> Self {
        value.0
    }
}

/// Typed menu button union.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MenuButton {
    Typed(MenuButtonKind),
    Other(Value),
}

/// Known menu button variants.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum MenuButtonKind {
    Commands,
    Default,
    WebApp(MenuButtonWebApp),
}

impl MenuButton {
    pub fn new(value: Value) -> Self {
        Self::from(value)
    }

    pub fn commands() -> Self {
        Self::Typed(MenuButtonKind::Commands)
    }

    pub fn default_button() -> Self {
        Self::Typed(MenuButtonKind::Default)
    }

    pub fn web_app(text: impl Into<String>, web_app: impl Into<WebAppInfo>) -> Self {
        Self::Typed(MenuButtonKind::WebApp(MenuButtonWebApp::new(text, web_app)))
    }

    pub fn as_web_app(&self) -> Option<&MenuButtonWebApp> {
        match self {
            Self::Typed(MenuButtonKind::WebApp(value)) => Some(value),
            Self::Typed(_) | Self::Other(_) => None,
        }
    }
}

impl Default for MenuButton {
    fn default() -> Self {
        Self::default_button()
    }
}

impl From<Value> for MenuButton {
    fn from(value: Value) -> Self {
        match serde_json::from_value::<MenuButtonKind>(value.clone()) {
            Ok(known) => Self::Typed(known),
            Err(_error) => Self::Other(value),
        }
    }
}

impl From<MenuButtonKind> for MenuButton {
    fn from(value: MenuButtonKind) -> Self {
        Self::Typed(value)
    }
}

impl From<MenuButton> for Value {
    fn from(value: MenuButton) -> Self {
        match value {
            MenuButton::Typed(known) => match known {
                MenuButtonKind::Commands => serde_json::json!({"type": "commands"}),
                MenuButtonKind::Default => serde_json::json!({"type": "default"}),
                MenuButtonKind::WebApp(mut value) => {
                    let mut object = serde_json::Map::new();
                    let mut web_app = serde_json::Map::new();
                    web_app.insert("url".to_owned(), Value::String(value.web_app.url));
                    object.insert("type".to_owned(), Value::String("web_app".to_owned()));
                    object.insert("text".to_owned(), Value::String(value.text));
                    object.insert("web_app".to_owned(), Value::Object(web_app));
                    for (key, extra_value) in std::mem::take(&mut value.extra) {
                        object.insert(key, extra_value);
                    }
                    Value::Object(object)
                }
            },
            MenuButton::Other(value) => value,
        }
    }
}

/// Mini App Web App descriptor.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WebAppInfo {
    pub url: String,
}

impl WebAppInfo {
    pub fn new(url: impl Into<String>) -> Self {
        Self { url: url.into() }
    }
}

impl From<String> for WebAppInfo {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

impl From<&str> for WebAppInfo {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

/// Button shown above inline query results.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InlineQueryResultsButton {
    pub text: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub web_app: Option<WebAppInfo>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub start_parameter: Option<String>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl InlineQueryResultsButton {
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            web_app: None,
            start_parameter: None,
            extra: BTreeMap::new(),
        }
    }

    pub fn web_app(text: impl Into<String>, web_app: impl Into<WebAppInfo>) -> Self {
        Self::new(text).with_web_app(web_app)
    }

    pub fn start_parameter(text: impl Into<String>, start_parameter: impl Into<String>) -> Self {
        Self::new(text).with_start_parameter(start_parameter)
    }

    pub fn with_web_app(mut self, web_app: impl Into<WebAppInfo>) -> Self {
        self.web_app = Some(web_app.into());
        self.start_parameter = None;
        self
    }

    pub fn with_start_parameter(mut self, start_parameter: impl Into<String>) -> Self {
        self.start_parameter = Some(start_parameter.into());
        self.web_app = None;
        self
    }
}

/// Menu button launching a Mini App.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MenuButtonWebApp {
    pub text: String,
    pub web_app: WebAppInfo,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl MenuButtonWebApp {
    pub fn new(text: impl Into<String>, web_app: impl Into<WebAppInfo>) -> Self {
        Self {
            text: text.into(),
            web_app: web_app.into(),
            extra: BTreeMap::new(),
        }
    }
}

impl From<MenuButtonWebApp> for MenuButton {
    fn from(value: MenuButtonWebApp) -> Self {
        Self::Typed(MenuButtonKind::WebApp(value))
    }
}

/// Data sent from Mini App via `Telegram.WebApp.sendData`.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct WebAppData {
    pub data: String,
    pub button_text: String,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl WebAppData {
    pub fn new(data: impl Into<String>, button_text: impl Into<String>) -> Self {
        Self {
            data: data.into(),
            button_text: button_text.into(),
            extra: BTreeMap::new(),
        }
    }
}

impl crate::types::advanced::AdvancedSetChatMenuButtonRequest {
    pub fn chat_id(mut self, chat_id: i64) -> Self {
        self.chat_id = Some(chat_id);
        self
    }

    pub fn menu_button(mut self, menu_button: impl Into<MenuButton>) -> Self {
        self.menu_button = Some(menu_button.into());
        self
    }

    pub fn menu_button_default(mut self) -> Self {
        self.menu_button = Some(MenuButton::default_button());
        self
    }

    pub fn menu_button_commands(mut self) -> Self {
        self.menu_button = Some(MenuButton::commands());
        self
    }

    pub fn menu_button_web_app(
        mut self,
        text: impl Into<String>,
        web_app: impl Into<WebAppInfo>,
    ) -> Self {
        self.menu_button = Some(MenuButton::web_app(text, web_app));
        self
    }
}

/// Generic reaction type payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ReactionType(pub Value);

impl ReactionType {
    pub fn new(value: Value) -> Self {
        Self(value)
    }
}

impl From<Value> for ReactionType {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<ReactionType> for Value {
    fn from(value: ReactionType) -> Self {
        value.0
    }
}

/// Generic passport element error payload.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PassportElementError(pub Value);

impl PassportElementError {
    pub fn new(value: Value) -> Self {
        Self(value)
    }
}

impl From<Value> for PassportElementError {
    fn from(value: Value) -> Self {
        Self(value)
    }
}

impl From<PassportElementError> for Value {
    fn from(value: PassportElementError) -> Self {
        value.0
    }
}

/// Inline keyboard button.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InlineKeyboardButton {
    pub text: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub web_app: Option<WebAppInfo>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl InlineKeyboardButton {
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            web_app: None,
            extra: BTreeMap::new(),
        }
    }

    pub fn callback(text: impl Into<String>, data: impl Into<String>) -> Result<Self> {
        Self::new(text).with_callback_data(data)
    }

    pub fn typed_callback<T>(text: impl Into<String>, payload: &T) -> Result<Self>
    where
        T: CallbackPayload,
    {
        Self::new(text).with_typed_callback(payload)
    }

    pub fn typed_callback_with_codec<T, C>(text: impl Into<String>, payload: &T) -> Result<Self>
    where
        C: CallbackCodec<T>,
    {
        Self::new(text).with_typed_callback_with_codec::<T, C>(payload)
    }

    pub fn compact_callback<T>(text: impl Into<String>, payload: &T) -> Result<Self>
    where
        T: CompactCallbackPayload,
    {
        Self::typed_callback_with_codec::<T, CompactCallbackCodec>(text, payload)
    }

    pub fn web_app(mut self, web_app: impl Into<WebAppInfo>) -> Self {
        self.web_app = Some(web_app.into());
        self
    }

    pub fn with_callback_data(mut self, data: impl Into<String>) -> Result<Self> {
        self.extra.insert(
            "callback_data".to_owned(),
            Value::String(validate_callback_data(data)?),
        );
        Ok(self)
    }

    pub fn with_typed_callback<T>(self, payload: &T) -> Result<Self>
    where
        T: CallbackPayload,
    {
        self.with_callback_data(payload.encode_callback_data()?)
    }

    pub fn with_typed_callback_with_codec<T, C>(self, payload: &T) -> Result<Self>
    where
        C: CallbackCodec<T>,
    {
        self.with_callback_data(C::encode_callback_data(payload)?)
    }

    pub fn with_compact_callback<T>(self, payload: &T) -> Result<Self>
    where
        T: CompactCallbackPayload,
    {
        self.with_typed_callback_with_codec::<T, CompactCallbackCodec>(payload)
    }

    pub fn callback_data(&self) -> Option<&str> {
        self.extra.get("callback_data").and_then(Value::as_str)
    }

    pub fn decode_callback<T>(&self) -> Result<Option<T>>
    where
        T: CallbackPayload,
    {
        self.callback_data()
            .map(T::decode_callback_data)
            .transpose()
    }

    pub fn decode_callback_with_codec<T, C>(&self) -> Result<Option<T>>
    where
        C: CallbackCodec<T>,
    {
        self.callback_data()
            .map(C::decode_callback_data)
            .transpose()
    }

    pub fn decode_compact_callback<T>(&self) -> Result<Option<T>>
    where
        T: CompactCallbackPayload,
    {
        self.decode_callback_with_codec::<T, CompactCallbackCodec>()
    }
}

/// Inline keyboard markup.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct InlineKeyboardMarkup {
    pub inline_keyboard: Vec<Vec<InlineKeyboardButton>>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl InlineKeyboardMarkup {
    pub fn new(inline_keyboard: Vec<Vec<InlineKeyboardButton>>) -> Self {
        Self {
            inline_keyboard,
            extra: BTreeMap::new(),
        }
    }

    pub fn single_row(row: Vec<InlineKeyboardButton>) -> Self {
        Self::new(vec![row])
    }

    pub fn push_row(mut self, row: Vec<InlineKeyboardButton>) -> Self {
        self.inline_keyboard.push(row);
        self
    }
}

/// Reply keyboard button.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct KeyboardButton {
    pub text: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub web_app: Option<WebAppInfo>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl KeyboardButton {
    pub fn new(text: impl Into<String>) -> Self {
        Self {
            text: text.into(),
            web_app: None,
            extra: BTreeMap::new(),
        }
    }

    pub fn web_app(mut self, web_app: impl Into<WebAppInfo>) -> Self {
        self.web_app = Some(web_app.into());
        self
    }
}

/// Reply keyboard markup.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ReplyKeyboardMarkup {
    pub keyboard: Vec<Vec<KeyboardButton>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_persistent: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resize_keyboard: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub one_time_keyboard: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input_field_placeholder: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub selective: Option<bool>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl ReplyKeyboardMarkup {
    pub fn new(keyboard: Vec<Vec<KeyboardButton>>) -> Self {
        Self {
            keyboard,
            is_persistent: None,
            resize_keyboard: None,
            one_time_keyboard: None,
            input_field_placeholder: None,
            selective: None,
            extra: BTreeMap::new(),
        }
    }
}

/// Remove reply keyboard marker.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ReplyKeyboardRemove {
    pub remove_keyboard: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub selective: Option<bool>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl Default for ReplyKeyboardRemove {
    fn default() -> Self {
        Self {
            remove_keyboard: true,
            selective: None,
            extra: BTreeMap::new(),
        }
    }
}

/// Force reply marker.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ForceReply {
    pub force_reply: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input_field_placeholder: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub selective: Option<bool>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl Default for ForceReply {
    fn default() -> Self {
        Self {
            force_reply: true,
            input_field_placeholder: None,
            selective: None,
            extra: BTreeMap::new(),
        }
    }
}

/// Reply markup union accepted by Telegram send/edit methods.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ReplyMarkup {
    InlineKeyboardMarkup(InlineKeyboardMarkup),
    ReplyKeyboardMarkup(ReplyKeyboardMarkup),
    ReplyKeyboardRemove(ReplyKeyboardRemove),
    ForceReply(ForceReply),
}

impl From<InlineKeyboardMarkup> for ReplyMarkup {
    fn from(value: InlineKeyboardMarkup) -> Self {
        Self::InlineKeyboardMarkup(value)
    }
}

impl From<ReplyKeyboardMarkup> for ReplyMarkup {
    fn from(value: ReplyKeyboardMarkup) -> Self {
        Self::ReplyKeyboardMarkup(value)
    }
}

impl From<ReplyKeyboardRemove> for ReplyMarkup {
    fn from(value: ReplyKeyboardRemove) -> Self {
        Self::ReplyKeyboardRemove(value)
    }
}

impl From<ForceReply> for ReplyMarkup {
    fn from(value: ForceReply) -> Self {
        Self::ForceReply(value)
    }
}

/// Reply-to reference parameters.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ReplyParameters {
    pub message_id: MessageId,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub chat_id: Option<ChatId>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub allow_sending_without_reply: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quote: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quote_parse_mode: Option<ParseMode>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quote_entities: Option<Vec<MessageEntity>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub quote_position: Option<u32>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl ReplyParameters {
    pub fn new(message_id: MessageId) -> Self {
        Self {
            message_id,
            chat_id: None,
            allow_sending_without_reply: None,
            quote: None,
            quote_parse_mode: None,
            quote_entities: None,
            quote_position: None,
            extra: BTreeMap::new(),
        }
    }
}

/// Link preview options for text messages.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LinkPreviewOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub is_disabled: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prefer_small_media: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prefer_large_media: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub show_above_text: Option<bool>,
    #[serde(flatten)]
    pub extra: BTreeMap<String, Value>,
}

impl LinkPreviewOptions {
    pub fn new() -> Self {
        Self {
            is_disabled: None,
            url: None,
            prefer_small_media: None,
            prefer_large_media: None,
            show_above_text: None,
            extra: BTreeMap::new(),
        }
    }

    pub fn disabled() -> Self {
        let mut options = Self::new();
        options.is_disabled = Some(true);
        options
    }
}

impl Default for LinkPreviewOptions {
    fn default() -> Self {
        Self::new()
    }
}