ferobot 0.1.0

A fully-featured, auto-generated Telegram Bot API library for Rust. All 285 types and 165 methods - strongly typed, fully async.
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
// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// ferobot: async Telegram Bot API framework written in Rust
// Repository: https://github.com/ankit-chaubey/ferobot
//
// Ferobot provides a fast and ergonomic framework for building Telegram bots
// using the official Telegram Bot API.
//
// Author: Ankit Chaubey
//
// If you use or modify this code, keep this notice at the top of your file
// and include the LICENSE-MIT or LICENSE-APACHE file from this repository.

//! Dispatcher - routes updates to handler groups in ascending order.
//!
//! Within a group the first matching handler runs; dispatcher then moves to the next group.
//! Handlers can alter flow by returning `Err(ContinueGroups)` or `Err(EndGroups)`.
//!

use std::{collections::BTreeMap, error::Error, panic::AssertUnwindSafe, sync::Arc};

use arc_swap::ArcSwap;
use futures::FutureExt as _;
use tokio::sync::Semaphore;
use tracing::{debug, error, warn};
#[cfg(feature = "per-chat")]
use {dashmap::DashMap, tokio::sync::mpsc};

use crate::{
    framework::{
        context::Context,
        handler::{ContinueGroups, EndGroups, Handler},
    },
    types::Update,
    Bot,
};

/// What the dispatcher does after an error hook returns.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum DispatcherAction {
    #[default]
    Noop, // stop current group, move to next
    ContinueGroups, // keep iterating handlers in current group
    EndGroups,      // abort all groups
}

pub type ErrorHook = Arc<
    dyn std::ops::Fn(&Bot, &Context, &(dyn Error + Send + Sync)) -> DispatcherAction + Send + Sync,
>;

pub type PanicHook = Arc<dyn std::ops::Fn(&Bot, &Context, String) + Send + Sync>;

/// Options for [`Dispatcher::new`].
#[derive(Clone, Default)]
pub struct DispatcherOpts {
    pub max_routines: Option<usize>,
    pub error_handler: Option<ErrorHook>,
    pub panic_handler: Option<PanicHook>,
    /// Middleware chain run before and after every update.
    pub middleware: Vec<crate::middleware::ArcMiddleware>,
    /// Route updates to per-chat sequential workers.
    ///
    /// When enabled, updates for the same `chat_id` are processed one-at-a-time
    /// in arrival order, while different chats run in parallel. This prevents
    /// message-ordering bugs under high concurrency.
    ///
    /// Requires the `per-chat` feature and a multi-threaded tokio runtime.
    #[cfg(feature = "per-chat")]
    pub per_chat_concurrency: bool,
    /// Per-chat worker channel buffer size (default: 256).
    #[cfg(feature = "per-chat")]
    pub per_chat_queue_size: usize,
}

impl DispatcherOpts {
    pub fn max_routines(mut self, n: usize) -> Self {
        self.max_routines = Some(n);
        self
    }

    pub fn on_error<F>(mut self, f: F) -> Self
    where
        F: std::ops::Fn(&Bot, &Context, &(dyn Error + Send + Sync)) -> DispatcherAction
            + Send
            + Sync
            + 'static,
    {
        self.error_handler = Some(Arc::new(f));
        self
    }

    pub fn on_panic<F>(mut self, f: F) -> Self
    where
        F: std::ops::Fn(&Bot, &Context, String) + Send + Sync + 'static,
    {
        self.panic_handler = Some(Arc::new(f));
        self
    }

    /// Enable per-chat sequential concurrency (requires `per-chat` feature).
    #[cfg(feature = "per-chat")]
    pub fn per_chat_concurrency(mut self) -> Self {
        self.per_chat_concurrency = true;
        self
    }

    /// Append a middleware to the chain.
    ///
    /// Middleware run in registration order before any handler, and in reverse
    /// order after. Return `false` from [`Middleware::before`] to drop the update.
    ///
    /// ```rust,no_run
    /// use ferobot::{DispatcherOpts, middleware::LoggingMiddleware};
    ///
    /// let opts = DispatcherOpts::default()
    ///     .middleware(LoggingMiddleware);
    /// ```
    pub fn middleware(mut self, m: impl crate::middleware::Middleware) -> Self {
        self.middleware.push(std::sync::Arc::new(m));
        self
    }

    /// Set the per-chat worker channel buffer depth (default: `256`).
    ///
    /// When a chat's buffer is full the excess update falls through to
    /// the standard concurrent pool so no updates are dropped.
    /// Requires the `per-chat` feature.
    #[cfg(feature = "per-chat")]
    pub fn per_chat_queue(mut self, size: usize) -> Self {
        self.per_chat_queue_size = if size == 0 { 256 } else { size };
        self
    }
}

type HandlerMap = BTreeMap<i32, Vec<Arc<dyn Handler>>>;

#[cfg(feature = "per-chat")]
struct ChatWork {
    bot: Bot,
    update: Update,
}

pub struct Dispatcher {
    handlers: Arc<ArcSwap<HandlerMap>>,
    error_handler: Option<ErrorHook>,
    panic_handler: Option<PanicHook>,
    semaphore: Option<Arc<Semaphore>>,
    middleware: Vec<crate::middleware::ArcMiddleware>,
    #[cfg(feature = "per-chat")]
    chat_workers: Option<Arc<DashMap<i64, mpsc::Sender<ChatWork>>>>,
    #[cfg(feature = "per-chat")]
    per_chat_queue_size: usize,
}

impl Dispatcher {
    pub fn new(opts: DispatcherOpts) -> Self {
        Self {
            handlers: Arc::new(ArcSwap::from_pointee(BTreeMap::new())),
            error_handler: opts.error_handler,
            panic_handler: opts.panic_handler,
            semaphore: opts.max_routines.map(|n| Arc::new(Semaphore::new(n))),
            middleware: opts.middleware,
            #[cfg(feature = "per-chat")]
            chat_workers: {
                #[allow(clippy::needless_bool)]
                if opts.per_chat_concurrency {
                    Some(Arc::new(DashMap::new()))
                } else {
                    None
                }
            },
            #[cfg(feature = "per-chat")]
            per_chat_queue_size: if opts.per_chat_queue_size == 0 {
                256
            } else {
                opts.per_chat_queue_size
            },
        }
    }

    pub fn add_handler<H: Handler + 'static>(&mut self, handler: H) {
        self.add_handler_to_group(handler, 0);
    }

    pub fn add_handler_to_group<H: Handler + 'static>(&mut self, handler: H, group: i32) {
        // Create the Arc once outside the rcu closure so retries don't double-allocate.
        let h: Arc<dyn Handler> = Arc::new(handler);
        self.handlers.rcu(|map| {
            let mut m: HandlerMap = (**map).clone();
            m.entry(group).or_default().push(Arc::clone(&h));
            m
        });
    }

    pub fn remove_handler(&mut self, name: &str, group: i32) -> bool {
        let mut removed = false;
        self.handlers.rcu(|map| {
            let mut m: HandlerMap = (**map).clone();
            if let Some(vec) = m.get_mut(&group) {
                if let Some(pos) = vec.iter().position(|h| h.name() == name) {
                    vec.remove(pos);
                    removed = true;
                }
            }
            m
        });
        removed
    }

    pub fn remove_handler_any_group(&mut self, name: &str) -> Option<i32> {
        let mut found_group: Option<i32> = None;
        self.handlers.rcu(|map| {
            let mut m: HandlerMap = (**map).clone();
            'search: for (&group, vec) in m.iter_mut() {
                if let Some(pos) = vec.iter().position(|h| h.name() == name) {
                    vec.remove(pos);
                    found_group = Some(group);
                    break 'search;
                }
            }
            m
        });
        found_group
    }

    pub fn remove_group(&mut self, group: i32) -> bool {
        let mut removed = false;
        self.handlers.rcu(|map| {
            let mut m: HandlerMap = (**map).clone();
            removed = m.remove(&group).is_some();
            m
        });
        removed
    }

    /// Dispatch an update. The update is handled in a spawned task.
    pub fn dispatch(&self, bot: Bot, update: Update) {
        let handlers_arc = Arc::clone(&self.handlers);
        let error_hook = self.error_handler.clone();
        let panic_hook = self.panic_handler.clone();
        let semaphore = self.semaphore.clone();
        let middleware = self.middleware.clone();

        // per-chat routing send to per-chat sequential worker if enabled.
        #[cfg(feature = "per-chat")]
        if let Some(ref workers) = self.chat_workers {
            let chat_id = Context::new(update.clone()).effective_chat().map(|c| c.id);
            if let Some(id) = chat_id {
                let queue_sz = self.per_chat_queue_size;
                let tx = get_or_spawn_worker(
                    workers,
                    id,
                    Arc::clone(&handlers_arc),
                    error_hook.clone(),
                    panic_hook.clone(),
                    semaphore.clone(),
                    middleware.clone(),
                    queue_sz,
                );
                // try_send is non-blocking: if the 256-slot buffer is full the
                // update falls through to regular parallel dispatch below.
                if tx.try_send(ChatWork { bot, update }).is_ok() {
                    return;
                }
            }
        }

        tokio::spawn(async move {
            // Run before-hooks; any hook returning false drops the update.
            if !crate::middleware::run_before(&middleware, &bot, &update).await {
                return;
            }

            let _permit = if let Some(sem) = &semaphore {
                Some(sem.clone().acquire_owned().await.ok())
            } else {
                None
            };

            let ctx = Context::new(update.clone());
            let snapshot = handlers_arc.load_full();
            run_handlers(snapshot, bot.clone(), ctx, error_hook, panic_hook).await;

            // Run after-hooks regardless of handler outcome.
            crate::middleware::run_after(&middleware, &bot, &update).await;
        });
    }

    /// Run an update synchronously in the calling task (no panic recovery).
    ///
    /// Useful for tests and for wrappers that already manage their own task.
    pub async fn process_update(&self, bot: &Bot, update: Update) {
        let ctx = Context::new(update);
        let snapshot = self.handlers.load_full();

        'groups: for (_, handlers) in snapshot.iter() {
            for handler in handlers {
                if !handler.check_update(&ctx) {
                    continue;
                }
                match handler.handle_update(bot.clone(), ctx.clone()).await {
                    Err(e) if e.is::<ContinueGroups>() => continue,
                    Err(e) if e.is::<EndGroups>() => break 'groups,
                    Err(e) => {
                        let action = self
                            .error_handler
                            .as_ref()
                            .map(|h| h(bot, &ctx, e.as_ref()))
                            .unwrap_or_default();
                        match action {
                            DispatcherAction::Noop => break,
                            DispatcherAction::ContinueGroups => continue,
                            DispatcherAction::EndGroups => break 'groups,
                        }
                    }
                    Ok(()) => break,
                }
            }
        }
    }
}

/// Execute the handler chain for one update.
/// Uses `catch_unwind` to isolate panics without spawning an extra task.
async fn run_handlers(
    snapshot: Arc<HandlerMap>,
    bot: Bot,
    ctx: Context,
    error_hook: Option<ErrorHook>,
    panic_hook: Option<PanicHook>,
) {
    'groups: for (group, handlers) in snapshot.iter() {
        for handler in handlers {
            if !handler.check_update(&ctx) {
                continue;
            }

            debug!(handler = handler.name(), group, "matched");

            let h = Arc::clone(handler);
            let bot2 = bot.clone();
            let ctx2 = ctx.clone();

            let result = AssertUnwindSafe(async move { h.handle_update(bot2, ctx2).await })
                .catch_unwind()
                .await;

            match result {
                Err(panic_payload) => {
                    let msg = panic_payload
                        .downcast::<String>()
                        .map(|s| *s)
                        .or_else(|p| p.downcast::<&str>().map(|s| s.to_string()))
                        .unwrap_or_else(|_| "<non-string panic>".into());
                    if let Some(ref hook) = panic_hook {
                        hook(&bot, &ctx, msg);
                    } else {
                        error!(handler = handler.name(), group, panic = %msg, "panicked");
                    }
                    break; // stop this group, let other groups continue
                }
                Ok(Err(e)) => {
                    if e.is::<ContinueGroups>() {
                        debug!(handler = handler.name(), "ContinueGroups");
                        continue;
                    }
                    if e.is::<EndGroups>() {
                        debug!(handler = handler.name(), "EndGroups");
                        break 'groups;
                    }
                    warn!(handler = handler.name(), group, error = %e);
                    let action = error_hook
                        .as_ref()
                        .map(|h| h(&bot, &ctx, e.as_ref()))
                        .unwrap_or_default();
                    match action {
                        DispatcherAction::Noop => break,
                        DispatcherAction::ContinueGroups => continue,
                        DispatcherAction::EndGroups => break 'groups,
                    }
                }
                Ok(Ok(())) => {
                    debug!(handler = handler.name(), group, "ok");
                    break;
                }
            }
        }
    }
}

/// Return the live sender for `chat_id`, spawning a new sequential worker if
/// the slot is empty or the previous worker has exited.
///
/// Each worker task processes updates for one chat sequentially (one `.await`
/// after another), so message order is always preserved. Chats run fully in
/// parallel with each other.
#[cfg(feature = "per-chat")]
fn get_or_spawn_worker(
    workers: &Arc<DashMap<i64, mpsc::Sender<ChatWork>>>,
    chat_id: i64,
    handlers_arc: Arc<ArcSwap<HandlerMap>>,
    error_hook: Option<ErrorHook>,
    panic_hook: Option<PanicHook>,
    semaphore: Option<Arc<Semaphore>>,
    middleware: Vec<crate::middleware::ArcMiddleware>,
    queue_size: usize,
) -> mpsc::Sender<ChatWork> {
    // Fast path: a live worker already exists for this chat.
    if let Some(entry) = workers.get(&chat_id) {
        if !entry.value().is_closed() {
            return entry.value().clone();
        }
    }

    // Slow path: spawn a fresh sequential worker task.
    let (tx, mut rx) = mpsc::channel::<ChatWork>(queue_size);
    workers.insert(chat_id, tx.clone());

    let workers_weak = Arc::downgrade(workers);
    tokio::spawn(async move {
        while let Some(work) = rx.recv().await {
            // Run before-middleware; drop update if any hook returns false.
            if !crate::middleware::run_before(&middleware, &work.bot, &work.update).await {
                continue;
            }

            // Each update in this chat is processed one at a time.
            let _permit = if let Some(sem) = &semaphore {
                Some(sem.clone().acquire_owned().await.ok())
            } else {
                None
            };

            let ctx = Context::new(work.update.clone());
            let snapshot = handlers_arc.load_full();
            run_handlers(
                snapshot,
                work.bot.clone(),
                ctx,
                error_hook.clone(),
                panic_hook.clone(),
            )
            .await;

            crate::middleware::run_after(&middleware, &work.bot, &work.update).await;
        }

        // all senders dropped, channel closed; remove the entry so the next
        // update for this chat spawns a fresh worker
        if let Some(w) = workers_weak.upgrade() {
            w.remove(&chat_id);
        }
    });

    tx
}