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
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
//! Fetches updates via long polling or webhook and pushes them into a channel.
//!
//! Port of `telegram.ext._updater.Updater`.
//!
//! The `Updater` is the bridge between Telegram and the application: it either
//! polls `getUpdates` or starts a webhook server, then forwards every
//! `Update` into a `tokio::sync::mpsc` channel for the `Application` to
//! consume.

use std::sync::Arc;
use std::time::Duration;

use tokio::sync::{mpsc, watch, Mutex};
use tracing::{debug, error, warn};

use rust_tg_bot_raw::error::TelegramError;

use crate::utils::network_loop::{network_retry_loop, NetworkLoopConfig};

#[cfg(feature = "webhooks")]
use tokio::sync::Notify;

#[cfg(feature = "webhooks")]
use crate::utils::webhook_handler::WebhookServer;

#[cfg(feature = "webhooks")]
use rust_tg_bot_raw::types::update::Update;

// ---------------------------------------------------------------------------
// Function types
// ---------------------------------------------------------------------------

/// A function that fetches updates from the Telegram API.
/// Signature: `(offset, timeout, allowed_updates) -> Result<Vec<Value>>`.
pub type GetUpdatesFn = Arc<
    dyn Fn(
            i64,
            Duration,
            Option<Vec<String>>,
        ) -> std::pin::Pin<
            Box<
                dyn std::future::Future<Output = Result<Vec<serde_json::Value>, TelegramError>>
                    + Send,
            >,
        > + Send
        + Sync,
>;

/// A function that deletes the webhook. Signature: `(drop_pending) -> Result<()>`.
pub type DeleteWebhookFn = Arc<
    dyn Fn(
            bool,
        )
            -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<(), TelegramError>> + Send>>
        + Send
        + Sync,
>;

// ---------------------------------------------------------------------------
// Configuration types
// ---------------------------------------------------------------------------

/// Configuration for [`Updater::start_polling`].
#[derive(Clone)]
pub struct PollingConfig {
    /// Interval between successive poll requests.
    pub poll_interval: Duration,
    /// Long-polling timeout sent to the Telegram API.
    pub timeout: Duration,
    /// Maximum number of retries during the bootstrap phase.
    pub bootstrap_retries: i32,
    /// List of update types the bot should receive, or `None` for all types.
    pub allowed_updates: Option<Vec<String>>,
    /// Whether to drop pending updates before starting the polling loop.
    pub drop_pending_updates: bool,
    /// The function used to call `getUpdates`.
    pub get_updates: GetUpdatesFn,
    /// The function used to delete the webhook during bootstrap.
    pub delete_webhook: DeleteWebhookFn,
}

/// Configuration for [`Updater::start_webhook`].
#[cfg(feature = "webhooks")]
#[derive(Clone)]
pub struct WebhookConfig {
    pub listen: String,
    pub port: u16,
    pub url_path: String,
    pub webhook_url: Option<String>,
    pub secret_token: Option<String>,
    pub bootstrap_retries: i32,
    pub drop_pending_updates: bool,
    pub allowed_updates: Option<Vec<String>>,
    pub max_connections: u32,
    /// Path to a PEM-encoded TLS certificate file.
    ///
    /// When both `cert_path` and `key_path` are set the webhook server will
    /// serve over HTTPS using `tokio-rustls`. Requires the `webhooks-tls`
    /// feature.
    pub cert_path: Option<String>,
    /// Path to a PEM-encoded TLS private key file.
    ///
    /// When both `cert_path` and `key_path` are set the webhook server will
    /// serve over HTTPS using `tokio-rustls`. Requires the `webhooks-tls`
    /// feature.
    pub key_path: Option<String>,
}

#[cfg(feature = "webhooks")]
impl Default for WebhookConfig {
    fn default() -> Self {
        Self {
            listen: "127.0.0.1".into(),
            port: 80,
            url_path: String::new(),
            webhook_url: None,
            secret_token: None,
            bootstrap_retries: 0,
            drop_pending_updates: false,
            allowed_updates: None,
            max_connections: 40,
            cert_path: None,
            key_path: None,
        }
    }
}

#[cfg(feature = "webhooks")]
impl WebhookConfig {
    /// Create a new webhook config with the given URL.
    /// Defaults: listen 127.0.0.1:80, no secret token, no TLS.
    pub fn new(url: impl Into<String>) -> Self {
        let url = url.into();
        Self {
            webhook_url: Some(url),
            ..Default::default()
        }
    }

    /// Set the listen address (default: "127.0.0.1").
    pub fn listen(mut self, addr: impl Into<String>) -> Self {
        self.listen = addr.into();
        self
    }

    /// Set the port (default: 80).
    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    /// Set the URL path the webhook listens on (default: "").
    pub fn url_path(mut self, path: impl Into<String>) -> Self {
        self.url_path = path.into();
        self
    }

    /// Set the secret token for webhook validation.
    pub fn secret_token(mut self, token: impl Into<String>) -> Self {
        self.secret_token = Some(token.into());
        self
    }

    /// Set the number of bootstrap retries (default: 0).
    pub fn bootstrap_retries(mut self, n: i32) -> Self {
        self.bootstrap_retries = n;
        self
    }

    /// Drop pending updates before starting (default: false).
    pub fn drop_pending_updates(mut self, drop: bool) -> Self {
        self.drop_pending_updates = drop;
        self
    }

    /// Set allowed update types.
    pub fn allowed_updates(mut self, types: Vec<String>) -> Self {
        self.allowed_updates = Some(types);
        self
    }

    /// Set max webhook connections (default: 40).
    pub fn max_connections(mut self, n: u32) -> Self {
        self.max_connections = n;
        self
    }

    /// Configure TLS with certificate and private key PEM files.
    ///
    /// When set, the webhook server will serve over HTTPS. The certificate
    /// file may contain the full chain. Requires the `webhooks-tls` feature
    /// to be enabled at compile time.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let config = WebhookConfig::new("https://mybot.example.com/telegram")
    ///     .port(8443)
    ///     .url_path("/telegram")
    ///     .secret_token("my-secret")
    ///     .tls("/path/to/cert.pem", "/path/to/key.pem");
    /// ```
    pub fn tls(mut self, cert: impl Into<String>, key: impl Into<String>) -> Self {
        self.cert_path = Some(cert.into());
        self.key_path = Some(key.into());
        self
    }

    /// Returns `true` when both `cert_path` and `key_path` are configured.
    pub fn has_tls(&self) -> bool {
        self.cert_path.is_some() && self.key_path.is_some()
    }
}

// ---------------------------------------------------------------------------
// Updater
// ---------------------------------------------------------------------------

/// Fetches updates for the bot via long polling or webhooks and forwards
/// them through [`take_update_rx`](Updater::take_update_rx).
pub struct Updater {
    update_tx: mpsc::Sender<serde_json::Value>,
    update_rx: Mutex<Option<mpsc::Receiver<serde_json::Value>>>,
    running: std::sync::atomic::AtomicBool,
    initialized: std::sync::atomic::AtomicBool,
    last_update_id: Mutex<i64>,
    /// Sending `true` signals the polling loop to stop.
    stop_tx: watch::Sender<bool>,
    /// The webhook server, if one was started.
    #[cfg(feature = "webhooks")]
    httpd: Mutex<Option<Arc<WebhookServer>>>,
}

impl std::fmt::Debug for Updater {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Updater")
            .field("running", &self.is_running())
            .field(
                "initialized",
                &self.initialized.load(std::sync::atomic::Ordering::Relaxed),
            )
            .finish()
    }
}

impl Updater {
    /// Create a new `Updater`.
    ///
    /// `channel_size` controls the bounded channel capacity.
    pub fn new(channel_size: usize) -> Self {
        let (update_tx, update_rx) = mpsc::channel(channel_size);
        let (stop_tx, _stop_rx) = watch::channel(false);
        Self {
            update_tx,
            update_rx: Mutex::new(Some(update_rx)),
            running: false.into(),
            initialized: false.into(),
            last_update_id: Mutex::new(0),
            stop_tx,
            #[cfg(feature = "webhooks")]
            httpd: Mutex::new(None),
        }
    }

    /// Take ownership of the receiving end of the update channel. Can only be
    /// called once; subsequent calls return `None`.
    pub async fn take_update_rx(&self) -> Option<mpsc::Receiver<serde_json::Value>> {
        self.update_rx.lock().await.take()
    }
    /// Returns `true` if the updater is currently running (polling or webhook).
    pub fn is_running(&self) -> bool {
        self.running.load(std::sync::atomic::Ordering::Relaxed)
    }

    // -----------------------------------------------------------------------
    // Lifecycle
    // -----------------------------------------------------------------------

    /// Initialize the updater.
    pub async fn initialize(&self) {
        if self.initialized.load(std::sync::atomic::Ordering::Relaxed) {
            debug!("Updater already initialized");
            return;
        }
        self.initialized
            .store(true, std::sync::atomic::Ordering::Relaxed);
        debug!("Updater initialized");
    }

    /// Shut down the updater. Must not be called while still running.
    pub async fn shutdown(&self) -> Result<(), UpdaterError> {
        if self.is_running() {
            return Err(UpdaterError::StillRunning);
        }
        if !self.initialized.load(std::sync::atomic::Ordering::Relaxed) {
            debug!("Updater already shut down");
            return Ok(());
        }
        self.initialized
            .store(false, std::sync::atomic::Ordering::Relaxed);
        debug!("Updater shut down");
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Polling
    // -----------------------------------------------------------------------

    /// Start polling for updates.
    ///
    /// Returns immediately after the bootstrap phase completes. Updates are
    /// sent through the channel returned by [`take_update_rx`](Self::take_update_rx).
    pub async fn start_polling(
        self: &Arc<Self>,
        config: PollingConfig,
    ) -> Result<(), UpdaterError> {
        if self.is_running() {
            return Err(UpdaterError::AlreadyRunning);
        }
        if !self.initialized.load(std::sync::atomic::Ordering::Relaxed) {
            return Err(UpdaterError::NotInitialized);
        }

        self.running
            .store(true, std::sync::atomic::Ordering::Relaxed);

        // Reset the stop signal from any prior run.
        let _ = self.stop_tx.send(false);

        // Bootstrap: delete any existing webhook.
        let delete_fn = config.delete_webhook.clone();
        let drop_pending = config.drop_pending_updates;
        let bootstrap_retries = config.bootstrap_retries;

        if let Err(e) = self
            .bootstrap_delete_webhook(delete_fn, drop_pending, bootstrap_retries)
            .await
        {
            self.running
                .store(false, std::sync::atomic::Ordering::Relaxed);
            return Err(UpdaterError::Bootstrap(e.to_string()));
        }

        debug!("Bootstrap complete, starting polling loop");

        let updater = Arc::clone(self);
        let stop_rx = self.stop_tx.subscribe();

        tokio::spawn(async move {
            let tx = updater.update_tx.clone();
            let timeout = config.timeout;
            let poll_interval = config.poll_interval;
            let allowed = config.allowed_updates.clone();
            let get_updates_fn = config.get_updates.clone();

            let result = network_retry_loop(NetworkLoopConfig {
                action_cb: || {
                    let tx = tx.clone();
                    let updater_inner = updater.clone();
                    let allowed_inner = allowed.clone();
                    let get_fn = get_updates_fn.clone();
                    async move {
                        let last_id = { *updater_inner.last_update_id.lock().await };
                        let updates: Vec<serde_json::Value> =
                            get_fn(last_id, timeout, allowed_inner).await?;
                        if !updates.is_empty() {
                            if !updater_inner.is_running() {
                                warn!(
                                    "Updater stopped unexpectedly. Pulled updates will be \
                                     ignored and pulled again on restart."
                                );
                                return Ok(());
                            }
                            for update in &updates {
                                if let Err(e) = tx.send(update.clone()).await {
                                    error!("Failed to enqueue update: {e}");
                                }
                            }
                            if let Some(last) = updates.last() {
                                if let Some(uid) = last.get("update_id").and_then(|v| v.as_i64()) {
                                    *updater_inner.last_update_id.lock().await = uid + 1;
                                }
                            }
                        }
                        Ok(())
                    }
                },
                on_err_cb: Some(|e: &TelegramError| {
                    error!("Error while polling for updates: {e}");
                }),
                description: "Polling Updates",
                interval: poll_interval.as_secs_f64(),
                stop_rx: Some(stop_rx),
                is_running: Some(Box::new({
                    let u = updater.clone();
                    move || u.is_running()
                })),
                max_retries: -1,
                repeat_on_success: true,
            })
            .await;

            if let Err(e) = result {
                error!("Polling loop exited with error: {e}");
            }
        });

        Ok(())
    }

    // -----------------------------------------------------------------------
    // Webhook
    // -----------------------------------------------------------------------

    /// Start a webhook server to receive updates.
    #[cfg(feature = "webhooks")]
    pub async fn start_webhook(
        self: &Arc<Self>,
        config: WebhookConfig,
    ) -> Result<(), UpdaterError> {
        if self.is_running() {
            return Err(UpdaterError::AlreadyRunning);
        }
        if !self.initialized.load(std::sync::atomic::Ordering::Relaxed) {
            return Err(UpdaterError::NotInitialized);
        }

        self.running
            .store(true, std::sync::atomic::Ordering::Relaxed);
        let _ = self.stop_tx.send(false);

        // WebhookServer expects Sender<Update> but the updater channel carries
        // serde_json::Value. Bridge the two with an intermediate typed channel.
        let (typed_tx, mut typed_rx) = mpsc::channel::<Update>(256);
        let value_tx = self.update_tx.clone();
        tokio::spawn(async move {
            while let Some(update) = typed_rx.recv().await {
                match serde_json::to_value(&update) {
                    Ok(v) => {
                        let _ = value_tx.send(v).await;
                    }
                    Err(e) => {
                        error!("Failed to serialize Update to Value: {e}");
                    }
                }
            }
        });

        // Build the TLS configuration if paths are provided.
        #[cfg(feature = "webhooks-tls")]
        let tls_config = if config.has_tls() {
            let cert_path = config
                .cert_path
                .as_deref()
                .expect("cert_path checked by has_tls");
            let key_path = config
                .key_path
                .as_deref()
                .expect("key_path checked by has_tls");
            match crate::utils::webhook_handler::TlsConfig::from_pem_files(cert_path, key_path)
                .await
            {
                Ok(tls) => Some(tls),
                Err(e) => {
                    self.running
                        .store(false, std::sync::atomic::Ordering::Relaxed);
                    return Err(UpdaterError::Bootstrap(format!(
                        "TLS configuration failed: {e}"
                    )));
                }
            }
        } else {
            None
        };

        // Warn at runtime if TLS paths were set but the feature is not enabled.
        #[cfg(not(feature = "webhooks-tls"))]
        if config.has_tls() {
            warn!(
                "TLS cert_path/key_path are set but the `webhooks-tls` feature is not enabled. \
                 The server will start without TLS. Enable the `webhooks-tls` feature to use HTTPS."
            );
        }

        let server = Arc::new(WebhookServer::new(
            &config.listen,
            config.port,
            &config.url_path,
            typed_tx,
            config.secret_token,
            #[cfg(feature = "webhooks-tls")]
            tls_config,
        ));

        let ready = Arc::new(Notify::new());
        let ready_clone = ready.clone();

        let srv = server.clone();
        tokio::spawn(async move {
            if let Err(e) = srv.serve_forever(Some(ready_clone)).await {
                error!("Webhook server error: {e}");
            }
        });

        ready.notified().await;
        debug!(
            "Webhook server started on {}:{}",
            config.listen, config.port
        );

        *self.httpd.lock().await = Some(server);

        Ok(())
    }

    // -----------------------------------------------------------------------
    // Stop
    // -----------------------------------------------------------------------

    /// Stop the updater (both polling and webhook).
    pub async fn stop(&self) -> Result<(), UpdaterError> {
        if !self.is_running() {
            return Err(UpdaterError::NotRunning);
        }
        debug!("Stopping updater");
        self.running
            .store(false, std::sync::atomic::Ordering::Relaxed);

        // Signal the polling loop to stop.
        let _ = self.stop_tx.send(true);

        // Shut down webhook server if present.
        #[cfg(feature = "webhooks")]
        {
            let httpd = self.httpd.lock().await;
            if let Some(ref server) = *httpd {
                server.shutdown();
            }
        }

        debug!("Updater stopped");
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Bootstrap helpers
    // -----------------------------------------------------------------------

    async fn bootstrap_delete_webhook(
        &self,
        delete_fn: DeleteWebhookFn,
        drop_pending: bool,
        max_retries: i32,
    ) -> Result<(), TelegramError> {
        debug!("Deleting webhook (bootstrap)");
        network_retry_loop(NetworkLoopConfig {
            action_cb: || {
                let f = delete_fn.clone();
                async move { f(drop_pending).await }
            },
            on_err_cb: None::<fn(&TelegramError)>,
            description: "Bootstrap delete webhook",
            interval: 1.0,
            stop_rx: None,
            is_running: None,
            max_retries,
            repeat_on_success: false,
        })
        .await
    }
}

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

#[derive(Debug, thiserror::Error)]
/// Errors that can occur within the [`Updater`] lifecycle.
#[non_exhaustive]
pub enum UpdaterError {
    /// The updater is already running and cannot be started again.
    #[error("this Updater is already running")]
    AlreadyRunning,

    /// The updater is not currently running.
    #[error("this Updater is not running")]
    NotRunning,

    /// The updater has not been initialized yet.
    #[error("this Updater was not initialized")]
    NotInitialized,

    /// The updater is still running and cannot be shut down.
    #[error("this Updater is still running")]
    StillRunning,

    /// The bootstrap phase (e.g. deleting webhooks) failed.
    #[error("bootstrap failed: {0}")]
    Bootstrap(String),
}

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

    fn noop_get_updates() -> GetUpdatesFn {
        Arc::new(|_offset, _timeout, _allowed| Box::pin(async { Ok(Vec::new()) }))
    }

    fn noop_delete_webhook() -> DeleteWebhookFn {
        Arc::new(|_drop_pending| Box::pin(async { Ok(()) }))
    }

    fn default_config() -> PollingConfig {
        PollingConfig {
            poll_interval: Duration::ZERO,
            timeout: Duration::from_secs(1),
            bootstrap_retries: 0,
            allowed_updates: None,
            drop_pending_updates: false,
            get_updates: noop_get_updates(),
            delete_webhook: noop_delete_webhook(),
        }
    }

    #[tokio::test]
    async fn lifecycle() {
        let updater = Arc::new(Updater::new(16));
        assert!(!updater.is_running());

        updater.initialize().await;

        // Can't stop before starting.
        assert!(updater.stop().await.is_err());

        updater.shutdown().await.unwrap();
    }

    #[tokio::test]
    async fn start_polling_requires_init() {
        let updater = Arc::new(Updater::new(16));
        let result = updater.start_polling(default_config()).await;
        assert!(matches!(result, Err(UpdaterError::NotInitialized)));
    }

    #[tokio::test]
    async fn start_and_stop_polling() {
        let updater = Arc::new(Updater::new(16));
        updater.initialize().await;
        updater.start_polling(default_config()).await.unwrap();
        assert!(updater.is_running());

        // Can't start twice.
        let result = updater.start_polling(default_config()).await;
        assert!(matches!(result, Err(UpdaterError::AlreadyRunning)));

        updater.stop().await.unwrap();
        assert!(!updater.is_running());
    }

    #[tokio::test]
    async fn take_update_rx_once() {
        let updater = Arc::new(Updater::new(16));
        let rx = updater.take_update_rx().await;
        assert!(rx.is_some());
        let rx2 = updater.take_update_rx().await;
        assert!(rx2.is_none());
    }

    #[tokio::test]
    async fn polling_delivers_updates() {
        let updater = Arc::new(Updater::new(16));
        updater.initialize().await;

        let mut rx = updater.take_update_rx().await.unwrap();

        // A get_updates that returns one update then empty.
        let call_count = Arc::new(std::sync::atomic::AtomicU32::new(0));
        let cc = call_count.clone();
        let get_fn: GetUpdatesFn = Arc::new(move |_offset, _timeout, _allowed| {
            let cc = cc.clone();
            Box::pin(async move {
                let n = cc.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                if n == 0 {
                    Ok(vec![serde_json::json!({"update_id": 100, "message": {}})])
                } else {
                    Ok(Vec::new())
                }
            })
        });

        let config = PollingConfig {
            poll_interval: Duration::from_millis(10),
            timeout: Duration::from_secs(1),
            bootstrap_retries: 0,
            allowed_updates: None,
            drop_pending_updates: false,
            get_updates: get_fn,
            delete_webhook: noop_delete_webhook(),
        };

        updater.start_polling(config).await.unwrap();

        // Should receive the update within a reasonable time.
        let update = tokio::time::timeout(Duration::from_secs(2), rx.recv())
            .await
            .expect("timeout waiting for update")
            .expect("channel closed");

        assert_eq!(update["update_id"], 100);

        updater.stop().await.unwrap();
    }
}