rust-tg-bot-ext 1.0.0-rc.1

Application framework for Telegram bots -- handlers, filters, persistence, job queue
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
//! Default parameter values for bot methods.
//!
//! Ported from `python-telegram-bot/src/telegram/ext/_defaults.py`.
//!
//! [`Defaults`] gathers all user-defined default values that the [`ExtBot`](super::ext_bot::ExtBot)
//! and handlers consult when an explicit value is not provided by the caller.
//!
//! Once constructed, every field is immutable (no public setters).
//!
//! # Construction
//!
//! Use the builder pattern via [`Defaults::builder()`]:
//!
//! ```
//! # use rust_tg_bot_ext::defaults::Defaults;
//! let defaults = Defaults::builder()
//!     .parse_mode("HTML")
//!     .disable_notification(true)
//!     .build();
//!
//! assert_eq!(defaults.parse_mode(), Some("HTML"));
//! ```

use std::collections::HashMap;

use serde_json::Value;

use rust_tg_bot_raw::types::link_preview_options::LinkPreviewOptions;

/// Convenience struct to gather all parameters with a (user defined) default value.
///
/// Fields that are `None` indicate "no default was set" and will not be injected into API
/// calls.
#[derive(Debug, Clone)]
pub struct Defaults {
    parse_mode: Option<String>,
    disable_notification: Option<bool>,
    allow_sending_without_reply: Option<bool>,
    protect_content: Option<bool>,
    block: bool,
    link_preview_options: Option<LinkPreviewOptions>,
    do_quote: Option<bool>,
    /// Pre-computed map of non-`None` defaults keyed by the API parameter name.  Used by
    /// `ExtBot` to merge defaults into outgoing requests.
    api_defaults: HashMap<String, Value>,
}

impl Defaults {
    /// Creates a new `Defaults` instance.
    ///
    /// Only the values that are explicitly `Some(...)` will be injected into API calls.
    /// `block` defaults to `true` when `None` is passed.
    ///
    /// Prefer [`Defaults::builder()`] for public construction -- this avoids long
    /// `None, None, None` argument lists.
    #[must_use]
    pub(crate) fn new(
        parse_mode: Option<String>,
        disable_notification: Option<bool>,
        allow_sending_without_reply: Option<bool>,
        protect_content: Option<bool>,
        block: Option<bool>,
        link_preview_options: Option<LinkPreviewOptions>,
        do_quote: Option<bool>,
    ) -> Self {
        let block = block.unwrap_or(true);

        let mut api_defaults = HashMap::new();

        if let Some(ref pm) = parse_mode {
            let v = Value::String(pm.clone());
            api_defaults.insert("parse_mode".into(), v.clone());
            api_defaults.insert("explanation_parse_mode".into(), v.clone());
            api_defaults.insert("text_parse_mode".into(), v.clone());
            api_defaults.insert("question_parse_mode".into(), v);
        }
        if let Some(dn) = disable_notification {
            api_defaults.insert("disable_notification".into(), Value::Bool(dn));
        }
        if let Some(aswr) = allow_sending_without_reply {
            api_defaults.insert("allow_sending_without_reply".into(), Value::Bool(aswr));
        }
        if let Some(pc) = protect_content {
            api_defaults.insert("protect_content".into(), Value::Bool(pc));
        }
        if let Some(dq) = do_quote {
            api_defaults.insert("do_quote".into(), Value::Bool(dq));
        }
        if let Some(ref lpo) = link_preview_options {
            if let Ok(v) = serde_json::to_value(lpo) {
                api_defaults.insert("link_preview_options".into(), v);
            }
        }

        Self {
            parse_mode,
            disable_notification,
            allow_sending_without_reply,
            protect_content,
            block,
            link_preview_options,
            do_quote,
            api_defaults,
        }
    }

    /// Returns a new [`DefaultsBuilder`] for ergonomic construction.
    ///
    /// # Example
    ///
    /// ```
    /// # use rust_tg_bot_ext::defaults::Defaults;
    /// let defaults = Defaults::builder()
    ///     .parse_mode("HTML")
    ///     .protect_content(true)
    ///     .build();
    /// ```
    #[must_use]
    pub fn builder() -> DefaultsBuilder {
        DefaultsBuilder::new()
    }

    // -- Read-only accessors (mirrors Python @property with no setter) --

    /// Send Markdown or HTML -- if you want Telegram apps to show bold, italic, fixed-width text
    /// or URLs in your bot's message.
    #[must_use]
    pub fn parse_mode(&self) -> Option<&str> {
        self.parse_mode.as_deref()
    }

    /// Alias for [`parse_mode`](Self::parse_mode), used for the corresponding parameter of
    /// `Bot::send_poll`.
    #[must_use]
    pub fn explanation_parse_mode(&self) -> Option<&str> {
        self.parse_mode.as_deref()
    }

    /// Alias for [`parse_mode`](Self::parse_mode), used for `InputPollOption` and
    /// `Bot::send_gift`.
    #[must_use]
    pub fn text_parse_mode(&self) -> Option<&str> {
        self.parse_mode.as_deref()
    }

    /// Alias for [`parse_mode`](Self::parse_mode), used for `Bot::send_poll`.
    #[must_use]
    pub fn question_parse_mode(&self) -> Option<&str> {
        self.parse_mode.as_deref()
    }

    /// Alias for [`parse_mode`](Self::parse_mode), used for `ReplyParameters`.
    #[must_use]
    pub fn quote_parse_mode(&self) -> Option<&str> {
        self.parse_mode.as_deref()
    }

    /// Sends the message silently.
    #[must_use]
    pub fn disable_notification(&self) -> Option<bool> {
        self.disable_notification
    }

    /// Pass `true` if the message should be sent even if the specified replied-to message is not
    /// found.
    #[must_use]
    pub fn allow_sending_without_reply(&self) -> Option<bool> {
        self.allow_sending_without_reply
    }

    /// Default setting for `BaseHandler.block` and error handlers.
    #[must_use]
    pub fn block(&self) -> bool {
        self.block
    }

    /// Protects the contents of the sent message from forwarding and saving.
    #[must_use]
    pub fn protect_content(&self) -> Option<bool> {
        self.protect_content
    }

    /// Link preview generation options for all outgoing messages.
    #[must_use]
    pub fn link_preview_options(&self) -> Option<&LinkPreviewOptions> {
        self.link_preview_options.as_ref()
    }

    /// Whether the bot should quote the replied-to message.
    #[must_use]
    pub fn do_quote(&self) -> Option<bool> {
        self.do_quote
    }

    /// Pre-computed mapping of non-`None` defaults keyed by API parameter name.
    #[must_use]
    pub fn api_defaults(&self) -> &HashMap<String, Value> {
        &self.api_defaults
    }
}

impl PartialEq for Defaults {
    fn eq(&self, other: &Self) -> bool {
        self.parse_mode == other.parse_mode
            && self.disable_notification == other.disable_notification
            && self.allow_sending_without_reply == other.allow_sending_without_reply
            && self.protect_content == other.protect_content
            && self.block == other.block
            && self.link_preview_options == other.link_preview_options
            && self.do_quote == other.do_quote
    }
}

impl Eq for Defaults {}

impl std::hash::Hash for Defaults {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.parse_mode.hash(state);
        self.disable_notification.hash(state);
        self.allow_sending_without_reply.hash(state);
        self.protect_content.hash(state);
        self.block.hash(state);
        self.do_quote.hash(state);
        // LinkPreviewOptions does not implement Hash, so we hash its JSON representation.
        if let Some(ref lpo) = self.link_preview_options {
            if let Ok(v) = serde_json::to_string(lpo) {
                v.hash(state);
            }
        }
    }
}

// ---------------------------------------------------------------------------
// DefaultsBuilder
// ---------------------------------------------------------------------------

/// Builder for [`Defaults`].
///
/// Provides ergonomic construction without `None, None, None` argument lists.
///
/// # Example
///
/// ```
/// # use rust_tg_bot_ext::defaults::Defaults;
/// let defaults = Defaults::builder()
///     .parse_mode("HTML")
///     .disable_notification(true)
///     .do_quote(true)
///     .build();
///
/// assert_eq!(defaults.parse_mode(), Some("HTML"));
/// assert_eq!(defaults.disable_notification(), Some(true));
/// assert!(defaults.block()); // defaults to true
/// ```
#[derive(Debug)]
pub struct DefaultsBuilder {
    parse_mode: Option<String>,
    disable_notification: Option<bool>,
    allow_sending_without_reply: Option<bool>,
    protect_content: Option<bool>,
    block: Option<bool>,
    link_preview_options: Option<LinkPreviewOptions>,
    do_quote: Option<bool>,
}

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

impl DefaultsBuilder {
    /// Creates a new builder with all fields unset.
    #[must_use]
    pub fn new() -> Self {
        Self {
            parse_mode: None,
            disable_notification: None,
            allow_sending_without_reply: None,
            protect_content: None,
            block: None,
            link_preview_options: None,
            do_quote: None,
        }
    }

    /// Sets the default parse mode (e.g. `"HTML"`, `"MarkdownV2"`).
    #[must_use]
    pub fn parse_mode(mut self, mode: impl Into<String>) -> Self {
        self.parse_mode = Some(mode.into());
        self
    }

    /// Sets whether messages are sent silently by default.
    #[must_use]
    pub fn disable_notification(mut self, value: bool) -> Self {
        self.disable_notification = Some(value);
        self
    }

    /// Sets whether messages should be sent even if the replied-to message is not found.
    #[must_use]
    pub fn allow_sending_without_reply(mut self, value: bool) -> Self {
        self.allow_sending_without_reply = Some(value);
        self
    }

    /// Sets whether message contents are protected from forwarding and saving.
    #[must_use]
    pub fn protect_content(mut self, value: bool) -> Self {
        self.protect_content = Some(value);
        self
    }

    /// Sets the default `block` value for handlers. Defaults to `true` if not set.
    #[must_use]
    pub fn block(mut self, value: bool) -> Self {
        self.block = Some(value);
        self
    }

    /// Sets the default link preview options.
    #[must_use]
    pub fn link_preview_options(mut self, options: LinkPreviewOptions) -> Self {
        self.link_preview_options = Some(options);
        self
    }

    /// Sets whether the bot should quote the replied-to message.
    #[must_use]
    pub fn do_quote(mut self, value: bool) -> Self {
        self.do_quote = Some(value);
        self
    }

    /// Builds the [`Defaults`] instance.
    #[must_use]
    pub fn build(self) -> Defaults {
        Defaults::new(
            self.parse_mode,
            self.disable_notification,
            self.allow_sending_without_reply,
            self.protect_content,
            self.block,
            self.link_preview_options,
            self.do_quote,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn defaults_immutable_accessors() {
        let d = Defaults::builder()
            .parse_mode("HTML")
            .disable_notification(true)
            .allow_sending_without_reply(false)
            .do_quote(true)
            .build();

        assert_eq!(d.parse_mode(), Some("HTML"));
        assert_eq!(d.explanation_parse_mode(), Some("HTML"));
        assert_eq!(d.text_parse_mode(), Some("HTML"));
        assert_eq!(d.question_parse_mode(), Some("HTML"));
        assert_eq!(d.disable_notification(), Some(true));
        assert_eq!(d.allow_sending_without_reply(), Some(false));
        assert!(d.block());
        assert_eq!(d.protect_content(), None);
        assert_eq!(d.do_quote(), Some(true));
    }

    #[test]
    fn defaults_api_defaults_map() {
        let d = Defaults::builder()
            .parse_mode("MarkdownV2")
            .protect_content(true)
            .build();

        let m = d.api_defaults();
        assert!(m.contains_key("parse_mode"));
        assert!(m.contains_key("explanation_parse_mode"));
        assert!(m.contains_key("protect_content"));
        assert!(!m.contains_key("disable_notification"));
    }

    #[test]
    fn defaults_equality() {
        let a = Defaults::builder().parse_mode("HTML").build();
        let b = Defaults::builder().parse_mode("HTML").build();
        let c = Defaults::builder().build();
        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    #[test]
    fn block_defaults_to_true() {
        let d = Defaults::builder().build();
        assert!(d.block());
    }

    #[test]
    fn block_can_be_set_to_false() {
        let d = Defaults::builder().block(false).build();
        assert!(!d.block());
    }

    #[test]
    fn builder_default_trait() {
        let b = DefaultsBuilder::default();
        let d = b.build();
        assert!(d.parse_mode().is_none());
        assert!(d.block());
    }
}