vaegram 0.3.0

Telegram MTProto client library for users and bots
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
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;

use crate::Client;
use crate::dispatcher::Router;
use crate::errors::{Error, Result};
use crate::types::{
    BotSubscriptionUpdated, BusinessConnection, CallbackQuery, ChatActionUpdated, ChatBoostRemoved, ChatBoostUpdated,
    ChatJoinRequest, ChatMemberUpdated, ChosenInlineResult, DeletedBusinessMessages, DeletedEphemeralMessages,
    DeletedMessages, EphemeralMessage, GuestMessage, InlineQuery, ManagedBotUpdated, Message, MessageGenerationStopped,
    MessageReactionCountUpdated, MessageReactionUpdated, PendingJoinRequestsUpdated, PinnedMessagesUpdated, PollAnswer,
    PollUpdated, PreCheckoutQuery, PurchasedPaidMedia, RawUpdate, ReactionsUpdated, ReadHistoryUpdated,
    SharedWidgetQuery, ShippingQuery, StarsBalanceUpdated, Story, TonWalletState, UserStatusUpdated, WidgetDataUpdated,
    WidgetQuery,
};

pub use crate::dispatcher::listeners::Listenable;
pub use crate::dispatcher::router::UpdateKind;

pub(crate) type BoxFuture = Pin<Box<dyn Future<Output = Result<()>> + Send>>;
pub(crate) type Callback<T> = Arc<dyn Fn(T) -> BoxFuture + Send + Sync>;
pub(crate) type RawCallback = Arc<dyn Fn(Client, RawUpdate) -> BoxFuture + Send + Sync>;
pub(crate) type ErrorCallback = Arc<dyn Fn(HandlerError) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;

pub(crate) fn callback<T, F, Fut, R>(f: F) -> Callback<T>
where
    F: Fn(T) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = Result<R>> + Send + 'static,
{
    Arc::new(move |value| {
        let future = f(value);
        Box::pin(async move { future.await.map(drop) }) as BoxFuture
    })
}

/// An `Err` a handler or lifecycle hook returned, as [`Client::on_error`] receives it, or
/// [`ClientEvent::HandlerFailed`](crate::enums::ClientEvent::HandlerFailed) when that isn't set.
#[non_exhaustive]
#[derive(Debug)]
pub struct HandlerError {
    /// The route of the handler that failed: [`UpdateKind::Raw`] for raw handlers, `Start`, `Stop`, `Connect` or
    /// `Disconnect` for the hooks.
    pub kind: UpdateKind,
    /// Bot-API style id of the update's chat. `None` for raw handlers, the hooks and updates without a chat.
    pub chat_id: Option<i64>,
    /// Id of the message the update is about, within [`chat_id`](Self::chat_id): the message a handler failed on,
    /// the message a pressed button or widget is on, the message a reaction changed on, or the message a guest
    /// query answered. `None` for raw handlers, the hooks and updates about no single message.
    pub message_id: Option<i32>,
    /// Bot-API style id of the user who caused the update (its `from_user` or `user`): the sender of a message or
    /// story, the user who pressed the button, sent the query or paid, or the one who changed a membership or
    /// reacted. `None` for raw handlers, the hooks and updates with no user behind them. The one exception is
    /// [`UserStatusUpdated`], where Telegram reports on a user and nobody acted:
    /// there it is that user.
    pub user_id: Option<i64>,
    /// Id of the query the update carries, the one its `answer` method takes: callback, widget, inline, shipping,
    /// pre-checkout, guest and join-request queries. `None` for raw handlers, the hooks and updates without one.
    pub query_id: Option<i64>,
    /// The error the handler returned.
    pub error: Error,
}

impl HandlerError {
    /// A failure with no update behind it: a raw handler's or a lifecycle hook's.
    pub(crate) fn bare(kind: UpdateKind, error: Error) -> Self {
        HandlerError { kind, chat_id: None, message_id: None, user_id: None, query_id: None, error }
    }
}

/// A handler just registered by a `Client::on_*` method: set its group, make it stop later groups, or take its
/// [`HandlerId`]. It borrows the client; dropping it keeps the handler registered.
#[derive(Clone, Copy)]
pub struct HandlerRef<'a> {
    client: &'a Client,
    kind: UpdateKind,
    id: u32,
}

// `group` and `stop` take effect at once; `self` comes back only for chaining, so dropping it is fine.
#[allow(clippy::return_self_not_must_use)]
impl HandlerRef<'_> {
    /// Groups run in ascending order, default 0. Within a group the first matching handler
    /// wins, and every group gets its own turn. Raw handlers always run and ignore this, as do the
    /// lifecycle hooks (`on_start`, `on_stop`, `on_connect`, `on_disconnect`).
    pub fn group(self, group: i32) -> Self {
        self.client.0.handlers.add(|r| r.configure(self.kind, self.id, Some(group), false));
        self
    }

    /// When this handler matches, the groups after it don't run for that update.
    pub fn stop(self) -> Self {
        self.client.0.handlers.add(|r| r.configure(self.kind, self.id, None, true));
        self
    }

    /// An id to pass to `Client::remove_handler`, which can be kept after the client borrow ends.
    pub fn id(self) -> HandlerId {
        HandlerId(Registered::Route(self.kind, self.id))
    }
}

/// One registered handler. Ids are never reused, so a stale one removes nothing.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct HandlerId(pub(crate) Registered);

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum Registered {
    Route(UpdateKind, u32),
    /// A next-step handler, which lives in the listener registry.
    Listener(u64),
}

/// A registered handler, as [`Client::handlers`] lists it.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct HandlerInfo {
    /// The id [`Client::remove_handler`] takes.
    pub id: HandlerId,
    /// The route the handler is on, named after the `Client::on_*` method that registered it.
    pub kind: UpdateKind,
    /// The group the handler runs in. `None` for raw handlers and lifecycle hooks, which have no group.
    pub group: Option<i32>,
    /// `true` if a match keeps the later groups from running for that update.
    pub stops: bool,
}

impl HandlerInfo {
    pub(crate) fn new(kind: UpdateKind, id: u32, group: Option<i32>, stops: bool) -> HandlerInfo {
        HandlerInfo { id: HandlerId(Registered::Route(kind, id)), kind, group, stops }
    }
}

impl Client {
    pub(crate) fn register(&self, kind: UpdateKind, add: impl FnOnce(&mut Router, u32)) -> HandlerRef<'_> {
        let id = self.0.handlers.add(|r| {
            let id = r.next_id();
            add(r, id);
            id
        });
        HandlerRef { client: self, kind, id }
    }
}

pub(crate) trait Routed: Clone + Send + 'static {
    fn chat_id(&self) -> Option<i64>;

    /// Id of the message the update is about, for [`HandlerError::message_id`].
    fn message_id(&self) -> Option<i32> {
        None
    }

    /// Id of the user the update is about, for [`HandlerError::user_id`].
    fn user_id(&self) -> Option<i64> {
        None
    }

    /// Id of the query the update carries, for [`HandlerError::query_id`].
    fn query_id(&self) -> Option<i64> {
        None
    }

    fn order_key(&self) -> Option<i64> {
        self.chat_id()
    }

    /// Runs once before the handlers get the item: fills what the client's fetch options ask for.
    fn prepare(self) -> impl Future<Output = Self> + Send {
        std::future::ready(self)
    }
}

impl Routed for Message {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn message_id(&self) -> Option<i32> {
        Some(self.id)
    }

    fn user_id(&self) -> Option<i64> {
        self.from_user.as_ref().map(|user| user.id)
    }

    fn prepare(self) -> impl Future<Output = Self> + Send {
        prefetched(self)
    }
}

async fn prefetched(mut message: Message) -> Message {
    if !message.client().0.fetch.any() {
        return message;
    }
    let client = message.client().clone();
    client.prefetch(std::slice::from_mut(&mut message)).await;
    message
}

impl Routed for CallbackQuery {
    fn chat_id(&self) -> Option<i64> {
        self.chat().map(|c| c.id)
    }

    // `CallbackQuery`'s own accessor, which wins over this method of the same name.
    fn message_id(&self) -> Option<i32> {
        CallbackQuery::message_id(self)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn query_id(&self) -> Option<i64> {
        Some(self.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.chat().map_or(self.from_user.id, |c| c.id))
    }
}

impl Routed for WidgetQuery {
    fn chat_id(&self) -> Option<i64> {
        self.chat().map(|c| c.id)
    }

    // `WidgetQuery`'s own accessor, which wins over this method of the same name.
    fn message_id(&self) -> Option<i32> {
        WidgetQuery::message_id(self)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn query_id(&self) -> Option<i64> {
        Some(self.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.chat().map_or(self.from_user.id, |c| c.id))
    }
}

impl Routed for SharedWidgetQuery {
    fn chat_id(&self) -> Option<i64> {
        None
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn query_id(&self) -> Option<i64> {
        Some(self.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.from_user.id)
    }
}

impl Routed for InlineQuery {
    fn chat_id(&self) -> Option<i64> {
        None
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn query_id(&self) -> Option<i64> {
        Some(self.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.from_user.id)
    }
}

impl Routed for ChosenInlineResult {
    fn chat_id(&self) -> Option<i64> {
        None
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.from_user.id)
    }
}

impl Routed for ChatJoinRequest {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn query_id(&self) -> Option<i64> {
        self.query_id
    }
}

impl Routed for GuestMessage {
    fn chat_id(&self) -> Option<i64> {
        Some(self.message.chat.id)
    }

    fn message_id(&self) -> Option<i32> {
        Some(self.message.id)
    }

    fn user_id(&self) -> Option<i64> {
        // The sender of the message that invoked the bot, or who it came via when that message names no sender.
        self.message.from_user.as_deref().or(self.caller_user.as_ref()).map(|user| user.id)
    }

    fn query_id(&self) -> Option<i64> {
        Some(self.query_id)
    }
}

impl Routed for BotSubscriptionUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.user.id)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.user.id)
    }
}

impl Routed for MessageGenerationStopped {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }
}

impl Routed for WidgetDataUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn message_id(&self) -> Option<i32> {
        Some(self.message_id)
    }
}

impl Routed for ManagedBotUpdated {
    fn chat_id(&self) -> Option<i64> {
        None
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.user.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.bot.id)
    }
}

impl Routed for EphemeralMessage {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn message_id(&self) -> Option<i32> {
        Some(self.id)
    }

    fn user_id(&self) -> Option<i64> {
        self.from_user.as_ref().map(|user| user.id)
    }
}

impl Routed for MessageReactionUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn message_id(&self) -> Option<i32> {
        Some(self.message_id)
    }

    fn user_id(&self) -> Option<i64> {
        self.user.as_ref().map(|user| user.id)
    }
}

impl Routed for DeletedEphemeralMessages {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }
}

impl Routed for MessageReactionCountUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn message_id(&self) -> Option<i32> {
        Some(self.message_id)
    }
}

impl Routed for ReactionsUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn message_id(&self) -> Option<i32> {
        Some(self.message_id)
    }
}

impl Routed for BusinessConnection {
    fn chat_id(&self) -> Option<i64> {
        Some(self.user.id)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.user.id)
    }
}

impl Routed for DeletedBusinessMessages {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }
}

impl Routed for ShippingQuery {
    fn chat_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn query_id(&self) -> Option<i64> {
        Some(self.id)
    }
}

impl Routed for PreCheckoutQuery {
    fn chat_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn query_id(&self) -> Option<i64> {
        Some(self.id)
    }
}

impl Routed for PurchasedPaidMedia {
    fn chat_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }
}

impl Routed for ChatBoostUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }
}

impl Routed for ChatBoostRemoved {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }
}

impl Routed for ChatMemberUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.from_user.id)
    }
}

impl Routed for PollUpdated {
    fn chat_id(&self) -> Option<i64> {
        self.chat.as_ref().map(|c| c.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.poll.id)
    }
}

impl Routed for PollAnswer {
    fn chat_id(&self) -> Option<i64> {
        None
    }

    fn user_id(&self) -> Option<i64> {
        self.user.as_ref().map(|user| user.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.poll_id)
    }
}

impl Routed for DeletedMessages {
    fn chat_id(&self) -> Option<i64> {
        self.chat.as_ref().map(|c| c.id)
    }
}

impl Routed for PinnedMessagesUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }
}

impl Routed for UserStatusUpdated {
    fn chat_id(&self) -> Option<i64> {
        None
    }

    fn user_id(&self) -> Option<i64> {
        Some(self.user.id)
    }

    fn order_key(&self) -> Option<i64> {
        Some(self.user.id)
    }
}

impl Routed for TonWalletState {
    fn chat_id(&self) -> Option<i64> {
        None
    }
}

impl Routed for ChatActionUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn user_id(&self) -> Option<i64> {
        self.from_user.as_ref().map(|user| user.id)
    }
}

impl Routed for ReadHistoryUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }
}

impl Routed for PendingJoinRequestsUpdated {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }
}

impl Routed for StarsBalanceUpdated {
    fn chat_id(&self) -> Option<i64> {
        None
    }
}

impl Routed for Story {
    fn chat_id(&self) -> Option<i64> {
        Some(self.chat.id)
    }

    fn user_id(&self) -> Option<i64> {
        self.from_user.as_ref().map(|user| user.id)
    }
}