aiogram 0.1.0

An ergonomic asynchronous Telegram Bot API framework for Rust, ported from aiogram
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
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
use std::any::{Any, TypeId};
use std::collections::{BTreeSet, HashMap};
use std::fmt;
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex, RwLock};
use std::time::Duration;

use async_trait::async_trait;
use tokio::sync::{Notify, Semaphore, watch};
use tokio::task::JoinSet;

use crate::bot::Bot;
use crate::client::BotRequest;
use crate::error::{Error, Result};
use crate::filters::Filter;
use crate::fsm::FsmMiddleware;
use crate::methods::{
    AnswerCallbackQuery, EditMessageText, GetUpdates, MessageOrBool, SendMessage, TelegramMethod,
};
use crate::types::{
    CallbackQuery, Chat, ChatBoostSourceUnion, MaybeInaccessibleMessageUnion, Message,
    ReplyParameters, Update, User,
};
use crate::utils::backoff::{Backoff, BackoffConfig};

type HandlerFuture = Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>;
type Handler = Arc<dyn Fn(UpdateContext) -> HandlerFuture + Send + Sync>;
type LifecycleHandler = Arc<dyn Fn(Bot) -> HandlerFuture + Send + Sync>;
type DispatchFuture<'a> = Pin<Box<dyn Future<Output = Result<bool>> + Send + 'a>>;

#[derive(Default)]
struct PollingControl {
    shutdown: Mutex<Option<watch::Sender<bool>>>,
    stopped: Notify,
}

impl PollingControl {
    fn start(&self) -> Result<(watch::Sender<bool>, watch::Receiver<bool>)> {
        let mut shutdown = self
            .shutdown
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        if shutdown.is_some() {
            return Err(Error::PollingAlreadyStarted);
        }
        let (sender, receiver) = watch::channel(false);
        *shutdown = Some(sender.clone());
        Ok((sender, receiver))
    }

    fn finish(&self) {
        self.shutdown
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .take();
        self.stopped.notify_waiters();
    }

    fn is_running(&self) -> bool {
        self.shutdown
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .is_some()
    }

    async fn stop(&self) -> Result<()> {
        let sender = self
            .shutdown
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .clone()
            .ok_or(Error::PollingNotStarted)?;
        let _ = sender.send(true);
        self.wait_stopped().await;
        Ok(())
    }

    async fn wait_stopped(&self) {
        loop {
            let notified = self.stopped.notified();
            if !self.is_running() {
                return;
            }
            notified.await;
        }
    }
}

#[cfg(unix)]
async fn polling_shutdown_signal() {
    use tokio::signal::unix::{SignalKind, signal};

    let mut terminate = signal(SignalKind::terminate()).ok();
    tokio::select! {
        _ = tokio::signal::ctrl_c() => {}
        _ = async {
            if let Some(signal) = &mut terminate {
                signal.recv().await;
            } else {
                std::future::pending::<()>().await;
            }
        } => {}
    }
}

#[cfg(not(unix))]
async fn polling_shutdown_signal() {
    let _ = tokio::signal::ctrl_c().await;
}

#[derive(Clone, Default)]
struct Dependencies(Arc<RwLock<HashMap<TypeId, Arc<dyn Any + Send + Sync>>>>);

impl Dependencies {
    fn new(values: HashMap<TypeId, Arc<dyn Any + Send + Sync>>) -> Self {
        Self(Arc::new(RwLock::new(values)))
    }

    fn get<T: Any + Send + Sync>(&self) -> Option<Arc<T>> {
        self.0
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .get(&TypeId::of::<T>())
            .cloned()?
            .downcast()
            .ok()
    }

    fn with<T: Any + Send + Sync>(&self, value: T) -> Self {
        let mut dependencies = self.snapshot();
        dependencies.insert(TypeId::of::<T>(), Arc::new(value));
        Self::new(dependencies)
    }

    fn insert<T: Any + Send + Sync>(&self, value: T) {
        self.0
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .insert(TypeId::of::<T>(), Arc::new(value));
    }

    fn fork(&self) -> Self {
        Self::new(self.snapshot())
    }

    fn snapshot(&self) -> HashMap<TypeId, Arc<dyn Any + Send + Sync>> {
        self.0
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
            .clone()
    }
}

/// Typed, named metadata attached to a handler registration.
///
/// Flags are visible to filters, middleware, and the handler through
/// [`UpdateContext::handler_flags`]. They are the Rust equivalent of aiogram's
/// handler flag dictionary without sacrificing static typing.
#[derive(Clone, Default)]
pub struct HandlerFlags(Arc<HashMap<String, Arc<dyn Any + Send + Sync>>>);

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

    pub fn with<T: Any + Send + Sync>(mut self, name: impl Into<String>, value: T) -> Self {
        self.insert(name, value);
        self
    }

    pub fn insert<T: Any + Send + Sync>(&mut self, name: impl Into<String>, value: T) {
        Arc::make_mut(&mut self.0).insert(name.into(), Arc::new(value));
    }

    pub fn get<T: Any + Send + Sync>(&self, name: &str) -> Option<Arc<T>> {
        self.0.get(name).cloned()?.downcast().ok()
    }

    pub fn get_cloned<T: Any + Clone + Send + Sync>(&self, name: &str) -> Option<T> {
        self.get::<T>(name).map(|value| value.as_ref().clone())
    }

    pub fn contains(&self, name: &str) -> bool {
        self.0.contains_key(name)
    }

    pub fn names(&self) -> impl Iterator<Item = &str> {
        self.0.keys().map(String::as_str)
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl fmt::Debug for HandlerFlags {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut names = self.names().collect::<Vec<_>>();
        names.sort_unstable();
        formatter
            .debug_struct("HandlerFlags")
            .field("names", &names)
            .finish()
    }
}

/// Chat/user coordinates resolved from every Telegram update variant.
#[derive(Debug, Clone, Default)]
pub struct EventContext {
    pub chat: Option<Chat>,
    pub user: Option<User>,
    pub thread_id: Option<i64>,
    pub business_connection_id: Option<String>,
}

impl EventContext {
    pub fn chat_id(&self) -> Option<i64> {
        self.chat.as_ref().map(|chat| chat.id)
    }

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

    pub fn resolve(update: &Update) -> Self {
        let message_context = |message: &Message, user: bool| Self {
            chat: Some(message.chat.as_ref().clone()),
            user: user.then(|| message.from_user.as_ref().cloned()).flatten(),
            thread_id: message.message_thread_id,
            business_connection_id: message.business_connection_id.clone(),
        };
        if let Some(message) = update.message.as_deref() {
            return message_context(message, true);
        }
        if let Some(message) = update.edited_message.as_deref() {
            return message_context(message, true);
        }
        if let Some(message) = update.channel_post.as_deref() {
            return message_context(message, false);
        }
        if let Some(message) = update.edited_channel_post.as_deref() {
            return message_context(message, false);
        }
        if let Some(message) = update.business_message.as_deref() {
            return message_context(message, true);
        }
        if let Some(message) = update.edited_business_message.as_deref() {
            return message_context(message, true);
        }
        if let Some(message) = update.guest_message.as_deref() {
            return message_context(message, true);
        }
        if let Some(query) = &update.callback_query {
            let (chat, thread_id, business_connection_id) = match query.message.as_ref() {
                Some(MaybeInaccessibleMessageUnion::Message(message)) => (
                    Some(message.chat.as_ref().clone()),
                    message.message_thread_id,
                    message.business_connection_id.clone(),
                ),
                Some(MaybeInaccessibleMessageUnion::InaccessibleMessage(message)) => {
                    (Some(message.chat.as_ref().clone()), None, None)
                }
                None => (None, None, None),
            };
            return Self {
                chat,
                user: Some(query.from_user.clone()),
                thread_id,
                business_connection_id,
            };
        }
        if let Some(query) = &update.inline_query {
            return Self::user(query.from_user.clone());
        }
        if let Some(result) = &update.chosen_inline_result {
            return Self::user(result.from_user.clone());
        }
        if let Some(query) = &update.shipping_query {
            return Self::user(query.from_user.clone());
        }
        if let Some(query) = &update.pre_checkout_query {
            return Self::user(query.from_user.clone());
        }
        if let Some(purchase) = &update.purchased_paid_media {
            return Self::user(purchase.from_user.clone());
        }
        if let Some(answer) = &update.poll_answer {
            return Self {
                chat: answer.voter_chat.as_deref().cloned(),
                user: answer.user.clone(),
                ..Self::default()
            };
        }
        if let Some(member) = update
            .my_chat_member
            .as_ref()
            .or(update.chat_member.as_ref())
        {
            return Self {
                chat: Some(member.chat.as_ref().clone()),
                user: Some(member.from_user.clone()),
                ..Self::default()
            };
        }
        if let Some(request) = &update.chat_join_request {
            return Self {
                chat: Some(request.chat.as_ref().clone()),
                user: Some(request.from_user.clone()),
                ..Self::default()
            };
        }
        if let Some(reaction) = &update.message_reaction {
            return Self {
                chat: Some(reaction.chat.as_ref().clone()),
                user: reaction.user.clone(),
                ..Self::default()
            };
        }
        if let Some(reaction) = &update.message_reaction_count {
            return Self::chat(reaction.chat.as_ref().clone());
        }
        if let Some(boost) = &update.chat_boost {
            let user = match &boost.boost.source {
                ChatBoostSourceUnion::ChatBoostSourcePremium(source) => Some(source.user.clone()),
                _ => None,
            };
            return Self {
                chat: Some(boost.chat.as_ref().clone()),
                user,
                ..Self::default()
            };
        }
        if let Some(boost) = &update.removed_chat_boost {
            return Self::chat(boost.chat.as_ref().clone());
        }
        if let Some(deleted) = &update.deleted_business_messages {
            return Self {
                chat: Some(deleted.chat.as_ref().clone()),
                business_connection_id: Some(deleted.business_connection_id.clone()),
                ..Self::default()
            };
        }
        if let Some(connection) = &update.business_connection {
            return Self {
                user: Some(connection.user.clone()),
                business_connection_id: Some(connection.id.clone()),
                ..Self::default()
            };
        }
        if let Some(managed) = &update.managed_bot {
            return Self::user(managed.user.clone());
        }
        if let Some(subscription) = &update.subscription {
            return Self::user(subscription.user.clone());
        }
        Self::default()
    }

    fn user(user: User) -> Self {
        Self {
            user: Some(user),
            ..Self::default()
        }
    }

    fn chat(chat: Chat) -> Self {
        Self {
            chat: Some(chat),
            ..Self::default()
        }
    }
}

/// Data passed to filters, middleware, and handlers for one update.
#[derive(Clone)]
pub struct UpdateContext {
    pub bot: Bot,
    pub update: Arc<Update>,
    dependencies: Dependencies,
    webhook_response: Option<Arc<Mutex<Option<BotRequest>>>>,
    handler_error: Option<Arc<Error>>,
}

impl UpdateContext {
    pub fn event_type(&self) -> Option<&'static str> {
        if self.handler_error.is_some() {
            Some("error")
        } else {
            self.update.event_type()
        }
    }

    pub fn message(&self) -> Option<&Message> {
        self.update.message_event()
    }

    pub fn callback_query(&self) -> Option<&CallbackQuery> {
        self.update.callback_query.as_ref()
    }

    pub fn event_context(&self) -> Option<Arc<EventContext>> {
        self.dependency::<EventContext>()
    }

    pub fn dependency<T: Any + Send + Sync>(&self) -> Option<Arc<T>> {
        self.dependencies.get::<T>()
    }

    pub fn handler_flags(&self) -> Option<Arc<HandlerFlags>> {
        self.dependency::<HandlerFlags>()
    }

    pub fn handler_flag<T: Any + Send + Sync>(&self, name: &str) -> Option<Arc<T>> {
        self.handler_flags()?.get(name)
    }

    /// Error raised by a handler or middleware while dispatching this update.
    /// It is available inside routes registered with [`Router::error`].
    pub fn error(&self) -> Option<&Error> {
        self.handler_error.as_deref()
    }

    pub fn with_dependency<T: Any + Send + Sync>(&self, value: T) -> Self {
        let mut context = self.clone();
        context.dependencies = self.dependencies.with(value);
        context
    }

    /// Injects data produced by a filter for the current route attempt.
    pub fn inject_dependency<T: Any + Send + Sync>(&self, value: T) {
        self.dependencies.insert(value);
    }

    fn fork_dependencies(&self) -> Self {
        let mut context = self.clone();
        context.dependencies = self.dependencies.fork();
        context
    }

    fn with_error(&self, error: Arc<Error>) -> Self {
        let mut context = self.clone();
        context.handler_error = Some(error);
        context
    }

    /// Sends a text reply to the chat associated with the current message.
    pub async fn answer(&self, text: impl Into<String>) -> Result<Message> {
        let message = self
            .message()
            .ok_or_else(|| Error::Handler("answer() requires a message update".to_owned()))?;
        self.bot.send_message(message.chat.id, text).await
    }

    /// Replies to the current message and preserves Telegram's reply context.
    pub async fn reply(&self, text: impl Into<String>) -> Result<Message> {
        let message = self
            .message()
            .ok_or_else(|| Error::Handler("reply() requires a message update".to_owned()))?;
        self.bot
            .execute(
                &SendMessage::new(message.chat.id, text)
                    .reply_parameters(ReplyParameters::new().message_id(message.message_id)),
            )
            .await
    }

    /// Edits the text of the current message.
    pub async fn edit_message_text(&self, text: impl Into<String>) -> Result<MessageOrBool> {
        let message = self.message().ok_or_else(|| {
            Error::Handler("edit_message_text() requires a message update".to_owned())
        })?;
        self.bot
            .edit_message_text(
                EditMessageText::new()
                    .chat_id(message.chat.id)
                    .message_id(message.message_id)
                    .text(text),
            )
            .await
    }

    pub async fn delete_message(&self) -> Result<bool> {
        let message = self.message().ok_or_else(|| {
            Error::Handler("delete_message() requires a message update".to_owned())
        })?;
        self.bot
            .delete_message(message.chat.id, message.message_id)
            .await
    }

    /// Answers the current callback query with optional notification text.
    pub async fn answer_callback(&self, text: Option<impl Into<String>>) -> Result<bool> {
        let query = self.callback_query().ok_or_else(|| {
            Error::Handler("answer_callback() requires a callback query update".to_owned())
        })?;
        let mut method = AnswerCallbackQuery::new(query.id.clone());
        if let Some(text) = text {
            method = method.text(text);
        }
        self.bot.execute(&method).await
    }

    /// Stores a Bot API method as the direct JSON or multipart response to a webhook.
    pub fn answer_webhook<M: TelegramMethod>(&self, method: &M) -> Result<()> {
        let response = self.webhook_response.as_ref().ok_or_else(|| {
            Error::Handler("answer_webhook() is only available for webhook updates".to_owned())
        })?;
        let request = self.bot.prepare_request(method)?;
        *response
            .lock()
            .map_err(|_| Error::Handler("webhook response lock poisoned".to_owned()))? =
            Some(request);
        Ok(())
    }
}

/// The remaining middleware/handler chain.
#[derive(Clone)]
pub struct Next {
    middlewares: Arc<Vec<Arc<dyn Middleware>>>,
    handler: Handler,
    index: usize,
}

impl Next {
    fn new(middlewares: Vec<Arc<dyn Middleware>>, handler: Handler) -> Self {
        Self {
            middlewares: Arc::new(middlewares),
            handler,
            index: 0,
        }
    }

    pub async fn run(self, context: UpdateContext) -> Result<()> {
        if let Some(middleware) = self.middlewares.get(self.index).cloned() {
            let next = Self {
                index: self.index + 1,
                ..self
            };
            middleware.handle(context, next).await
        } else {
            (self.handler)(context).await
        }
    }
}

/// Incoming update middleware. It can modify dependencies indirectly, perform
/// checks, skip `next`, or wrap the downstream handler with before/after work.
#[async_trait]
pub trait Middleware: Send + Sync {
    async fn handle(&self, context: UpdateContext, next: Next) -> Result<()>;
}

/// Stateful or class-style update handler. This is the Rust counterpart of
/// aiogram's `BaseHandler` subclasses; closure handlers remain available for
/// concise routes.
#[async_trait]
pub trait ClassHandler: Send + Sync + 'static {
    async fn handle(&self, context: UpdateContext) -> Result<()>;
}

/// The remaining router-level outer-middleware chain.
pub struct OuterNext<'a> {
    router: &'a Router,
    middlewares: Arc<Vec<Arc<dyn OuterMiddleware>>>,
    inherited_middlewares: Vec<Arc<dyn Middleware>>,
    index: usize,
}

impl<'a> OuterNext<'a> {
    fn new(
        router: &'a Router,
        inherited_middlewares: Vec<Arc<dyn Middleware>>,
        middlewares: Vec<Arc<dyn OuterMiddleware>>,
    ) -> Self {
        Self {
            router,
            middlewares: Arc::new(middlewares),
            inherited_middlewares,
            index: 0,
        }
    }

    pub async fn run(self, context: UpdateContext) -> Result<bool> {
        if let Some(middleware) = self.middlewares.get(self.index).cloned() {
            let next = Self {
                index: self.index + 1,
                ..self
            };
            middleware.handle(context, next).await
        } else {
            self.router
                .dispatch_inner(context, self.inherited_middlewares)
                .await
        }
    }
}

/// Router middleware that wraps filter evaluation, handlers and child routers.
#[async_trait]
pub trait OuterMiddleware: Send + Sync {
    async fn handle(&self, context: UpdateContext, next: OuterNext<'_>) -> Result<bool>;
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum EventKind {
    Any,
    Named(String),
}

#[derive(Clone)]
struct Route {
    event: EventKind,
    filters: Vec<Arc<dyn Filter>>,
    flags: HandlerFlags,
    handler: Handler,
}

/// A composable collection of update handlers, similar to aiogram's Router.
#[derive(Default, Clone)]
pub struct Router {
    name: Option<String>,
    routes: Vec<Route>,
    root_filters: HashMap<String, Vec<Arc<dyn Filter>>>,
    middlewares: Vec<Arc<dyn Middleware>>,
    event_middlewares: HashMap<String, Vec<Arc<dyn Middleware>>>,
    outer_middlewares: Vec<Arc<dyn OuterMiddleware>>,
    event_outer_middlewares: HashMap<String, Vec<Arc<dyn OuterMiddleware>>>,
    startup_handlers: Vec<LifecycleHandler>,
    shutdown_handlers: Vec<LifecycleHandler>,
    sub_routers: Vec<Router>,
}

macro_rules! event_shortcuts {
    ($($name:ident => $event:literal),+ $(,)?) => {
        $(
            pub fn $name<F, Fut>(
                &mut self,
                filter: impl Filter + 'static,
                handler: F,
            ) -> &mut Self
            where
                F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
                Fut: Future<Output = Result<()>> + Send + 'static,
            {
                self.event($event, filter, handler)
            }
        )+
    };
}

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

    pub fn named(name: impl Into<String>) -> Self {
        Self {
            name: Some(name.into()),
            ..Self::default()
        }
    }

    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn include_router(&mut self, router: Router) -> &mut Self {
        self.sub_routers.push(router);
        self
    }

    pub fn include_routers(&mut self, routers: impl IntoIterator<Item = Router>) -> &mut Self {
        self.sub_routers.extend(routers);
        self
    }

    /// Adds a filter that gates every handler and child router for this event,
    /// corresponding to `router.<event>.filter(...)` in aiogram.
    pub fn event_filter(
        &mut self,
        event_type: impl Into<String>,
        filter: impl Filter + 'static,
    ) -> &mut Self {
        self.root_filters
            .entry(event_type.into())
            .or_default()
            .push(Arc::new(filter));
        self
    }

    pub fn message_filter(&mut self, filter: impl Filter + 'static) -> &mut Self {
        self.event_filter("message", filter)
    }

    pub fn middleware(&mut self, middleware: impl Middleware + 'static) -> &mut Self {
        self.middlewares.push(Arc::new(middleware));
        self
    }

    pub fn event_middleware(
        &mut self,
        event_type: impl Into<String>,
        middleware: impl Middleware + 'static,
    ) -> &mut Self {
        self.event_middlewares
            .entry(event_type.into())
            .or_default()
            .push(Arc::new(middleware));
        self
    }

    pub fn message_middleware(&mut self, middleware: impl Middleware + 'static) -> &mut Self {
        self.event_middleware("message", middleware)
    }

    pub fn outer_middleware(&mut self, middleware: impl OuterMiddleware + 'static) -> &mut Self {
        self.outer_middlewares.push(Arc::new(middleware));
        self
    }

    pub fn event_outer_middleware(
        &mut self,
        event_type: impl Into<String>,
        middleware: impl OuterMiddleware + 'static,
    ) -> &mut Self {
        self.event_outer_middlewares
            .entry(event_type.into())
            .or_default()
            .push(Arc::new(middleware));
        self
    }

    pub fn message_outer_middleware(
        &mut self,
        middleware: impl OuterMiddleware + 'static,
    ) -> &mut Self {
        self.event_outer_middleware("message", middleware)
    }

    pub fn startup<F, Fut>(&mut self, handler: F) -> &mut Self
    where
        F: Fn(Bot) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.startup_handlers
            .push(Arc::new(move |bot| Box::pin(handler(bot))));
        self
    }

    pub fn shutdown<F, Fut>(&mut self, handler: F) -> &mut Self
    where
        F: Fn(Bot) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.shutdown_handlers
            .push(Arc::new(move |bot| Box::pin(handler(bot))));
        self
    }

    pub fn message<F, Fut>(&mut self, filter: impl Filter + 'static, handler: F) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.event("message", filter, handler)
    }

    pub fn message_handler(
        &mut self,
        filter: impl Filter + 'static,
        handler: impl ClassHandler,
    ) -> &mut Self {
        self.event_handler("message", filter, handler)
    }

    pub fn message_with_flags<F, Fut>(
        &mut self,
        filter: impl Filter + 'static,
        flags: HandlerFlags,
        handler: F,
    ) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.event_with_flags("message", filter, flags, handler)
    }

    pub fn message_filters<F, Fut>(
        &mut self,
        filters: Vec<Arc<dyn Filter>>,
        handler: F,
    ) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.route(EventKind::Named("message".to_owned()), filters, handler)
    }

    pub fn message_filters_with_flags<F, Fut>(
        &mut self,
        filters: Vec<Arc<dyn Filter>>,
        flags: HandlerFlags,
        handler: F,
    ) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.route_with_flags(
            EventKind::Named("message".to_owned()),
            filters,
            flags,
            handler,
        )
    }

    pub fn edited_message<F, Fut>(&mut self, filter: impl Filter + 'static, handler: F) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.event("edited_message", filter, handler)
    }

    pub fn channel_post<F, Fut>(&mut self, filter: impl Filter + 'static, handler: F) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.event("channel_post", filter, handler)
    }

    pub fn callback_query<F, Fut>(&mut self, filter: impl Filter + 'static, handler: F) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.event("callback_query", filter, handler)
    }

    pub fn callback_query_handler(
        &mut self,
        filter: impl Filter + 'static,
        handler: impl ClassHandler,
    ) -> &mut Self {
        self.event_handler("callback_query", filter, handler)
    }

    event_shortcuts! {
        edited_channel_post => "edited_channel_post",
        business_connection => "business_connection",
        business_message => "business_message",
        edited_business_message => "edited_business_message",
        deleted_business_messages => "deleted_business_messages",
        guest_message => "guest_message",
        message_reaction => "message_reaction",
        message_reaction_count => "message_reaction_count",
        inline_query => "inline_query",
        chosen_inline_result => "chosen_inline_result",
        shipping_query => "shipping_query",
        pre_checkout_query => "pre_checkout_query",
        purchased_paid_media => "purchased_paid_media",
        poll => "poll",
        poll_answer => "poll_answer",
        my_chat_member => "my_chat_member",
        chat_member => "chat_member",
        chat_join_request => "chat_join_request",
        chat_boost => "chat_boost",
        removed_chat_boost => "removed_chat_boost",
        managed_bot => "managed_bot",
        subscription => "subscription",
    }

    pub fn error<F, Fut>(&mut self, filter: impl Filter + 'static, handler: F) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.event("error", filter, handler)
    }

    /// Registers any Telegram update type supported by the pinned schema.
    pub fn event<F, Fut>(
        &mut self,
        event_type: impl Into<String>,
        filter: impl Filter + 'static,
        handler: F,
    ) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.event_with_flags(event_type, filter, HandlerFlags::default(), handler)
    }

    pub fn event_handler(
        &mut self,
        event_type: impl Into<String>,
        filter: impl Filter + 'static,
        handler: impl ClassHandler,
    ) -> &mut Self {
        let handler = Arc::new(handler);
        self.event(event_type, filter, move |context| {
            let handler = handler.clone();
            async move { handler.handle(context).await }
        })
    }

    pub fn event_with_flags<F, Fut>(
        &mut self,
        event_type: impl Into<String>,
        filter: impl Filter + 'static,
        flags: HandlerFlags,
        handler: F,
    ) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.route_with_flags(
            EventKind::Named(event_type.into()),
            vec![Arc::new(filter)],
            flags,
            handler,
        )
    }

    pub fn update<F, Fut>(&mut self, filter: impl Filter + 'static, handler: F) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.route(EventKind::Any, vec![Arc::new(filter)], handler)
    }

    pub fn update_with_flags<F, Fut>(
        &mut self,
        filter: impl Filter + 'static,
        flags: HandlerFlags,
        handler: F,
    ) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.route_with_flags(EventKind::Any, vec![Arc::new(filter)], flags, handler)
    }

    fn route<F, Fut>(
        &mut self,
        event: EventKind,
        filters: Vec<Arc<dyn Filter>>,
        handler: F,
    ) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.route_with_flags(event, filters, HandlerFlags::default(), handler)
    }

    fn route_with_flags<F, Fut>(
        &mut self,
        event: EventKind,
        filters: Vec<Arc<dyn Filter>>,
        mut flags: HandlerFlags,
        handler: F,
    ) -> &mut Self
    where
        F: Fn(UpdateContext) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        for filter in &filters {
            filter.update_handler_flags(&mut flags);
        }
        self.routes.push(Route {
            event,
            filters,
            flags,
            handler: Arc::new(move |context| Box::pin(handler(context))),
        });
        self
    }

    fn dispatch<'a>(
        &'a self,
        context: UpdateContext,
        inherited_middlewares: Vec<Arc<dyn Middleware>>,
    ) -> DispatchFuture<'a> {
        Box::pin(async move {
            let mut outer_middlewares = self.outer_middlewares.clone();
            if let Some(event_type) = context.event_type()
                && let Some(event_middlewares) = self.event_outer_middlewares.get(event_type)
            {
                outer_middlewares.extend(event_middlewares.iter().cloned());
            }
            OuterNext::new(self, inherited_middlewares, outer_middlewares)
                .run(context)
                .await
        })
    }

    fn dispatch_inner<'a>(
        &'a self,
        context: UpdateContext,
        mut inherited_middlewares: Vec<Arc<dyn Middleware>>,
    ) -> DispatchFuture<'a> {
        Box::pin(async move {
            inherited_middlewares.extend(self.middlewares.iter().cloned());
            if let Some(event_type) = context.event_type() {
                if let Some(filters) = self.root_filters.get(event_type) {
                    for filter in filters {
                        if !filter.check(&context).await {
                            return Ok(false);
                        }
                    }
                }
                if let Some(event_middlewares) = self.event_middlewares.get(event_type) {
                    inherited_middlewares.extend(event_middlewares.iter().cloned());
                }
            }
            for route in &self.routes {
                if !event_matches(&route.event, &context) {
                    continue;
                }
                let route_context = context.fork_dependencies();
                route_context.inject_dependency(route.flags.clone());
                let mut matched = true;
                for filter in &route.filters {
                    if !filter.check(&route_context).await {
                        matched = false;
                        break;
                    }
                }
                if matched {
                    let next = Next::new(inherited_middlewares.clone(), route.handler.clone());
                    match next.run(route_context).await {
                        Ok(()) | Err(Error::CancelHandler) => return Ok(true),
                        Err(Error::SkipHandler) => continue,
                        Err(error) => return Err(error),
                    }
                }
            }
            for router in &self.sub_routers {
                if router
                    .dispatch(context.clone(), inherited_middlewares.clone())
                    .await?
                {
                    return Ok(true);
                }
            }
            Ok(false)
        })
    }

    fn collect_used_update_types(&self, output: &mut BTreeSet<String>) -> bool {
        let mut has_catch_all = false;
        for route in &self.routes {
            match &route.event {
                EventKind::Named(event) => {
                    if event != "error" {
                        output.insert(event.clone());
                    }
                }
                EventKind::Any => has_catch_all = true,
            }
        }
        for router in &self.sub_routers {
            has_catch_all |= router.collect_used_update_types(output);
        }
        has_catch_all
    }

    fn emit_lifecycle<'a>(
        &'a self,
        bot: Bot,
        startup: bool,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
        Box::pin(async move {
            let handlers = if startup {
                &self.startup_handlers
            } else {
                &self.shutdown_handlers
            };
            for handler in handlers {
                handler(bot.clone()).await?;
            }
            for router in &self.sub_routers {
                router.emit_lifecycle(bot.clone(), startup).await?;
            }
            Ok(())
        })
    }
}

fn event_matches(kind: &EventKind, context: &UpdateContext) -> bool {
    match kind {
        EventKind::Any => context.handler_error.is_none(),
        EventKind::Named(expected) => context.event_type() == Some(expected.as_str()),
    }
}

/// Receives updates and dispatches each one through included routers.
#[derive(Clone)]
pub struct Dispatcher {
    routers: Vec<Router>,
    dependencies: HashMap<TypeId, Arc<dyn Any + Send + Sync>>,
    allowed_updates: Option<Vec<String>>,
    polling_timeout: Duration,
    backoff_config: BackoffConfig,
    handle_as_tasks: bool,
    tasks_concurrency_limit: Option<usize>,
    fsm_middleware: Option<FsmMiddleware>,
    startup_handlers: Vec<LifecycleHandler>,
    shutdown_handlers: Vec<LifecycleHandler>,
    polling_control: Arc<PollingControl>,
}

impl Default for Dispatcher {
    fn default() -> Self {
        Self {
            routers: Vec::new(),
            dependencies: HashMap::new(),
            allowed_updates: None,
            polling_timeout: Duration::from_secs(30),
            backoff_config: BackoffConfig::default(),
            handle_as_tasks: true,
            tasks_concurrency_limit: None,
            fsm_middleware: None,
            startup_handlers: Vec::new(),
            shutdown_handlers: Vec::new(),
            polling_control: Arc::new(PollingControl::default()),
        }
    }
}

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

    pub fn include_router(&mut self, router: Router) -> &mut Self {
        self.routers.push(router);
        self
    }

    /// Adds typed application data available through `UpdateContext::dependency`.
    pub fn provide<T: Any + Send + Sync>(&mut self, value: T) -> &mut Self {
        self.dependencies.insert(TypeId::of::<T>(), Arc::new(value));
        self
    }

    pub fn allowed_updates(
        &mut self,
        updates: impl IntoIterator<Item = impl Into<String>>,
    ) -> &mut Self {
        self.allowed_updates = Some(updates.into_iter().map(Into::into).collect());
        self
    }

    pub fn polling_timeout(&mut self, timeout: Duration) -> &mut Self {
        self.polling_timeout = timeout;
        self
    }

    pub fn backoff_config(&mut self, config: BackoffConfig) -> &mut Self {
        self.backoff_config = config;
        self
    }

    pub fn handle_as_tasks(&mut self, value: bool) -> &mut Self {
        self.handle_as_tasks = value;
        self
    }

    pub fn tasks_concurrency_limit(&mut self, limit: usize) -> Result<&mut Self> {
        if limit == 0 {
            return Err(Error::InvalidPayload(
                "tasks concurrency limit must be greater than zero".to_owned(),
            ));
        }
        self.tasks_concurrency_limit = Some(limit);
        Ok(self)
    }

    /// Installs FSM context and event isolation before filters are evaluated.
    pub fn fsm(&mut self, middleware: FsmMiddleware) -> &mut Self {
        self.fsm_middleware = Some(middleware);
        self
    }

    pub fn fsm_middleware(&self) -> Option<&FsmMiddleware> {
        self.fsm_middleware.as_ref()
    }

    pub fn storage(&self) -> Option<Arc<dyn crate::fsm::Storage>> {
        self.fsm_middleware.as_ref().map(FsmMiddleware::storage)
    }

    pub fn startup<F, Fut>(&mut self, handler: F) -> &mut Self
    where
        F: Fn(Bot) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.startup_handlers
            .push(Arc::new(move |bot| Box::pin(handler(bot))));
        self
    }

    pub fn shutdown<F, Fut>(&mut self, handler: F) -> &mut Self
    where
        F: Fn(Bot) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<()>> + Send + 'static,
    {
        self.shutdown_handlers
            .push(Arc::new(move |bot| Box::pin(handler(bot))));
        self
    }

    pub async fn emit_startup(&self, bot: Bot) -> Result<()> {
        Self::emit_lifecycle(&self.startup_handlers, bot.clone()).await?;
        for router in &self.routers {
            router.emit_lifecycle(bot.clone(), true).await?;
        }
        Ok(())
    }

    pub async fn emit_shutdown(&self, bot: Bot) -> Result<()> {
        let lifecycle_result = async {
            Self::emit_lifecycle(&self.shutdown_handlers, bot.clone()).await?;
            for router in &self.routers {
                router.emit_lifecycle(bot.clone(), false).await?;
            }
            Ok(())
        }
        .await;
        let fsm_result = match &self.fsm_middleware {
            Some(fsm) => fsm.close().await,
            None => Ok(()),
        };
        lifecycle_result.and(fsm_result)
    }

    async fn emit_lifecycle(handlers: &[LifecycleHandler], bot: Bot) -> Result<()> {
        for handler in handlers {
            handler(bot.clone()).await?;
        }
        Ok(())
    }

    /// Computes Telegram update types registered across the complete router tree.
    /// Returns `None` if a catch-all update handler requires unrestricted updates.
    pub fn resolve_used_update_types(&self) -> Option<Vec<String>> {
        let mut updates = BTreeSet::new();
        let mut has_catch_all = false;
        for router in &self.routers {
            has_catch_all |= router.collect_used_update_types(&mut updates);
        }
        (!has_catch_all).then(|| updates.into_iter().collect())
    }

    pub async fn feed_update(&self, bot: Bot, update: Update) -> Result<bool> {
        self.feed_update_inner(bot, update, None).await
    }

    /// Validates and dispatches an update represented as raw JSON data.
    ///
    /// This is the typed Rust counterpart of aiogram's `feed_raw_update` and
    /// is useful for brokers, recorded updates and framework integrations that
    /// have already parsed the HTTP request body.
    pub async fn feed_raw_update(&self, bot: Bot, update: serde_json::Value) -> Result<bool> {
        self.feed_update(bot, serde_json::from_value(update)?).await
    }

    pub async fn feed_webhook_update(
        &self,
        bot: Bot,
        update: Update,
    ) -> Result<(bool, Option<BotRequest>)> {
        let response = Arc::new(Mutex::new(None));
        let handled = self
            .feed_update_inner(bot, update, Some(response.clone()))
            .await?;
        let answer = response
            .lock()
            .map_err(|_| Error::Handler("webhook response lock poisoned".to_owned()))?
            .take();
        Ok((handled, answer))
    }

    async fn feed_update_inner(
        &self,
        bot: Bot,
        update: Update,
        webhook_response: Option<Arc<Mutex<Option<BotRequest>>>>,
    ) -> Result<bool> {
        let mut context = UpdateContext {
            bot,
            update: Arc::new(update),
            dependencies: Dependencies::new(self.dependencies.clone()),
            webhook_response,
            handler_error: None,
        };
        context = context.with_dependency(EventContext::resolve(&context.update));
        let _fsm_guard = if let Some(middleware) = &self.fsm_middleware {
            if let Some((fsm, guard)) = middleware.resolve(&context).await? {
                context = context.with_dependency(fsm);
                Some(guard)
            } else {
                None
            }
        } else {
            None
        };
        for router in &self.routers {
            match router.dispatch(context.clone(), Vec::new()).await {
                Ok(true) => return Ok(true),
                Ok(false) => {}
                Err(error) => return self.dispatch_error(context, error).await,
            }
        }
        Ok(false)
    }

    async fn dispatch_error(&self, context: UpdateContext, error: Error) -> Result<bool> {
        let error = Arc::new(error);
        let error_context = context.with_error(error.clone());
        for router in &self.routers {
            match router.dispatch(error_context.clone(), Vec::new()).await {
                Ok(true) => return Ok(true),
                Ok(false) => {}
                Err(error) => return Err(error),
            }
        }
        drop(error_context);
        Err(Arc::try_unwrap(error).unwrap_or_else(|error| Error::Handler(error.to_string())))
    }

    /// Starts long polling and shuts down on Ctrl+C/SIGINT.
    pub async fn start_polling(&self, bot: Bot) -> Result<()> {
        self.start_polling_many([bot]).await
    }

    /// Starts one polling worker per bot under one shared lifecycle and stop
    /// signal, matching aiogram's multi-bot polling capability.
    pub async fn start_polling_many(&self, bots: impl IntoIterator<Item = Bot>) -> Result<()> {
        let bots: Vec<_> = bots.into_iter().collect();
        if bots.is_empty() {
            return Err(Error::InvalidPayload(
                "at least one bot is required to start polling".to_owned(),
            ));
        }
        let (shutdown_tx, shutdown_rx) = self.polling_control.start()?;
        let signal_tx = shutdown_tx.clone();
        let signal = tokio::spawn(async move {
            polling_shutdown_signal().await;
            let _ = signal_tx.send(true);
        });
        let result = self.poll_many(bots, shutdown_rx).await;
        signal.abort();
        self.polling_control.finish();
        result
    }

    pub fn is_polling(&self) -> bool {
        self.polling_control.is_running()
    }

    /// Requests graceful shutdown and waits until all polling workers and the
    /// dispatcher shutdown lifecycle have finished.
    pub async fn stop_polling(&self) -> Result<()> {
        self.polling_control.stop().await
    }

    pub async fn wait_stopped(&self) {
        self.polling_control.wait_stopped().await;
    }

    /// Long-polls until `shutdown` becomes true.
    pub async fn poll(&self, bot: Bot, mut shutdown: watch::Receiver<bool>) -> Result<()> {
        self.emit_startup(bot.clone()).await?;
        let result = self.poll_updates(bot.clone(), &mut shutdown).await;
        let shutdown_result = self.emit_shutdown(bot).await;
        result.and(shutdown_result)
    }

    /// Polls several bots until the shared shutdown receiver changes. Startup
    /// and shutdown observers are emitted once, using the final bot as aiogram
    /// does for its lifecycle context.
    pub async fn poll_many(
        &self,
        bots: impl IntoIterator<Item = Bot>,
        shutdown: watch::Receiver<bool>,
    ) -> Result<()> {
        let bots: Vec<_> = bots.into_iter().collect();
        let lifecycle_bot = bots.last().cloned().ok_or_else(|| {
            Error::InvalidPayload("at least one bot is required to poll".to_owned())
        })?;
        self.emit_startup(lifecycle_bot.clone()).await?;
        let mut workers = JoinSet::new();
        for bot in bots {
            let dispatcher = self.clone();
            let mut bot_shutdown = shutdown.clone();
            workers.spawn(async move { dispatcher.poll_updates(bot, &mut bot_shutdown).await });
        }
        let mut result = Ok(());
        while let Some(worker) = workers.join_next().await {
            match worker {
                Ok(Ok(())) => {}
                Ok(Err(error)) if result.is_ok() => result = Err(error),
                Err(error) if result.is_ok() => {
                    result = Err(Error::Handler(format!("polling worker failed: {error}")));
                }
                _ => {}
            }
        }
        let shutdown_result = self.emit_shutdown(lifecycle_bot).await;
        result.and(shutdown_result)
    }

    async fn poll_updates(&self, bot: Bot, shutdown: &mut watch::Receiver<bool>) -> Result<()> {
        let mut offset = None;
        let auto_updates = self.resolve_used_update_types();
        let mut backoff = Backoff::new(self.backoff_config);
        let semaphore = self
            .tasks_concurrency_limit
            .map(|limit| Arc::new(Semaphore::new(limit)));
        let mut tasks = JoinSet::new();
        'polling: loop {
            if *shutdown.borrow() {
                break;
            }
            let request = GetUpdates {
                offset,
                limit: Some(100),
                timeout: Some(self.polling_timeout.as_secs().min(i64::MAX as u64) as i64),
                allowed_updates: self
                    .allowed_updates
                    .clone()
                    .or_else(|| auto_updates.clone()),
                extra: Default::default(),
            };
            let fetched = tokio::select! {
                changed = shutdown.changed() => {
                    if changed.is_err() || *shutdown.borrow() { break 'polling; }
                    continue;
                }
                updates = bot.get_updates(request) => updates,
            };
            let updates = match fetched {
                Ok(updates) => {
                    if backoff.counter() > 0 {
                        tracing::info!(attempts = backoff.counter(), "polling connection restored");
                        backoff.reset();
                    }
                    updates
                }
                Err(error) => {
                    let delay = backoff.advance();
                    tracing::error!(%error, ?delay, attempt = backoff.counter(), "failed to fetch updates; retrying");
                    tokio::select! {
                        _ = tokio::time::sleep(delay) => {}
                        changed = shutdown.changed() => {
                            if changed.is_err() || *shutdown.borrow() { break 'polling; }
                        }
                    }
                    continue;
                }
            };
            for update in updates {
                offset = Some(update.update_id + 1);
                if self.handle_as_tasks {
                    let permit = if let Some(semaphore) = &semaphore {
                        Some(
                            semaphore
                                .clone()
                                .acquire_owned()
                                .await
                                .map_err(|_| Error::DispatcherStopped)?,
                        )
                    } else {
                        None
                    };
                    let dispatcher = self.clone();
                    let bot = bot.clone();
                    tasks.spawn(async move {
                        let _permit = permit;
                        if let Err(error) = dispatcher.feed_update(bot, update).await {
                            tracing::error!(%error, "update handler failed");
                        }
                    });
                } else if let Err(error) = self.feed_update(bot.clone(), update).await {
                    tracing::error!(%error, "update handler failed");
                }
            }
            while let Some(result) = tasks.try_join_next() {
                if let Err(error) = result {
                    tracing::error!(%error, "update task panicked");
                }
            }
        }
        while let Some(result) = tasks.join_next().await {
            if let Err(error) = result {
                tracing::error!(%error, "update task panicked during shutdown");
            }
        }
        Ok(())
    }
}

/// Continue looking for another handler after the current one matched.
pub fn skip() -> Result<()> {
    Err(Error::SkipHandler)
}

/// Stop propagation and treat the update as handled.
pub fn cancel() -> Result<()> {
    Err(Error::CancelHandler)
}

#[cfg(test)]
mod tests {
    use std::sync::Mutex;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

    use super::*;
    use crate::filters;
    use crate::filters::FilterExt;

    fn update(field: &str, text: &str) -> Update {
        serde_json::from_value(serde_json::json!({
            "update_id": 1,
            (field): {
                "message_id": 2,
                "date": 3,
                "chat": {"id": 4, "type": "private"},
                "from": {"id": 4, "is_bot": false, "first_name": "Ada"},
                "text": text
            }
        }))
        .unwrap()
    }

    fn bot() -> Bot {
        Bot::new("123456:abcdefghijklmnopqrstuvwxyzABCDE").unwrap()
    }

    #[tokio::test]
    async fn routes_exact_event_and_injects_dependency() {
        let mut router = Router::new();
        router.message(filters::command("start"), |context| async move {
            context
                .dependency::<AtomicUsize>()
                .unwrap()
                .fetch_add(1, Ordering::SeqCst);
            Ok(())
        });
        let mut dispatcher = Dispatcher::new();
        dispatcher
            .include_router(router)
            .provide(AtomicUsize::new(0));

        assert!(
            dispatcher
                .feed_update(bot(), update("message", "/start payload"))
                .await
                .unwrap()
        );
        assert!(
            !dispatcher
                .feed_update(bot(), update("edited_message", "/start payload"))
                .await
                .unwrap()
        );
        assert_eq!(
            dispatcher
                .dependencies
                .get(&TypeId::of::<AtomicUsize>())
                .unwrap()
                .clone()
                .downcast::<AtomicUsize>()
                .unwrap()
                .load(Ordering::SeqCst),
            1
        );
    }

    #[tokio::test]
    async fn feed_raw_update_validates_and_dispatches_json_values() {
        let seen = Arc::new(AtomicBool::new(false));
        let mut router = Router::new();
        let handler_seen = seen.clone();
        router.message(filters::any(), move |_| {
            let handler_seen = handler_seen.clone();
            async move {
                handler_seen.store(true, Ordering::SeqCst);
                Ok(())
            }
        });
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        let raw = serde_json::to_value(update("message", "hello")).unwrap();
        assert!(dispatcher.feed_raw_update(bot(), raw).await.unwrap());
        assert!(seen.load(Ordering::SeqCst));

        let invalid = serde_json::json!({"update_id": "not-an-integer"});
        assert!(matches!(
            dispatcher.feed_raw_update(bot(), invalid).await,
            Err(Error::Json(_))
        ));
    }

    #[tokio::test]
    async fn nested_router_and_multiple_filters_propagate() {
        let calls = Arc::new(AtomicUsize::new(0));
        let captured = calls.clone();
        let mut child = Router::named("child");
        child.message_filters(
            vec![
                filters::boxed(filters::command("start")),
                filters::boxed(filters::text("/start")),
            ],
            move |_| {
                let captured = captured.clone();
                async move {
                    captured.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            },
        );
        let mut root = Router::named("root");
        root.include_router(child);
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(root);

        assert!(
            dispatcher
                .feed_update(bot(), update("message", "/start"))
                .await
                .unwrap()
        );
        assert_eq!(calls.load(Ordering::SeqCst), 1);
        assert_eq!(
            dispatcher.resolve_used_update_types(),
            Some(vec!["message".to_owned()])
        );
    }

    #[tokio::test]
    async fn typed_handler_flags_reach_filters_middleware_and_handler() {
        struct FlagMiddleware;

        #[async_trait]
        impl Middleware for FlagMiddleware {
            async fn handle(&self, context: UpdateContext, next: Next) -> Result<()> {
                assert_eq!(*context.handler_flag::<u32>("rate_limit").unwrap(), 7);
                assert_eq!(
                    context
                        .handler_flags()
                        .unwrap()
                        .get::<Vec<filters::Command>>("commands")
                        .unwrap()
                        .len(),
                    1
                );
                next.run(context).await
            }
        }

        let flags = HandlerFlags::new().with("rate_limit", 7_u32);
        let mut router = Router::new();
        router.middleware(FlagMiddleware);
        router.message_filters_with_flags(
            vec![
                filters::boxed(filters::FnFilter::new(|context| {
                    Box::pin(async move {
                        context
                            .handler_flag::<u32>("rate_limit")
                            .is_some_and(|value| *value == 7)
                    })
                })),
                filters::boxed(filters::Command::new("start")),
            ],
            flags,
            |context| async move {
                assert_eq!(*context.handler_flag::<u32>("rate_limit").unwrap(), 7);
                Ok(())
            },
        );
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        assert!(
            dispatcher
                .feed_update(bot(), update("message", "/start"))
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn command_filter_validates_mentions_and_arguments() {
        let mut router = Router::new();
        router.message(
            filters::Command::new("start").bot_username("right_bot"),
            |context| async move {
                let command = context.dependency::<filters::CommandMatch>().unwrap();
                assert_eq!(command.command, "start");
                assert_eq!(command.mention.as_deref(), Some("right_bot"));
                assert_eq!(command.args.as_deref(), Some("payload"));
                Ok(())
            },
        );
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        assert!(
            !dispatcher
                .feed_update(bot(), update("message", "/start@wrong_bot payload"))
                .await
                .unwrap()
        );
        assert!(
            dispatcher
                .feed_update(bot(), update("message", "/start@right_bot payload"))
                .await
                .unwrap()
        );
        assert_eq!(
            filters::parse_command("/start one two").unwrap().args,
            Some("one two")
        );
    }

    #[tokio::test]
    async fn command_start_decodes_payload_and_regex_captures_are_injected() {
        let mut router = Router::new();
        router.message(
            filters::CommandStart::new().deep_link(true).encoded(true),
            |context| async move {
                assert_eq!(
                    context
                        .dependency::<filters::CommandMatch>()
                        .unwrap()
                        .args
                        .as_deref(),
                    Some("hello world")
                );
                Ok(())
            },
        );
        router.message(
            filters::Command::regex(r"^item_(\d+)$").unwrap(),
            |context| async move {
                let command = context.dependency::<filters::CommandMatch>().unwrap();
                assert_eq!(
                    command.regex_captures.as_ref().unwrap()[1].as_deref(),
                    Some("42")
                );
                Ok(())
            },
        );
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        assert!(
            dispatcher
                .feed_update(bot(), update("message", "/start aGVsbG8gd29ybGQ"))
                .await
                .unwrap()
        );
        assert!(
            dispatcher
                .feed_update(bot(), update("message", "/item_42"))
                .await
                .unwrap()
        );
        assert!(
            !dispatcher
                .feed_update(bot(), update("message", "/START aGVsbG8gd29ybGQ"))
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn callback_filter_injects_parsed_typed_data() {
        let mut router = Router::new();
        router.callback_query(
            filters::callback_data_filter(
                |value| {
                    value
                        .strip_prefix("item:")
                        .ok_or_else(|| Error::Utility("prefix".to_owned()))?
                        .parse::<u64>()
                        .map_err(|error| Error::Utility(error.to_string()))
                },
                |value| *value > 0,
            ),
            |context| async move {
                assert_eq!(*context.dependency::<u64>().unwrap(), 42);
                Ok(())
            },
        );
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);
        let update = serde_json::from_value(serde_json::json!({
            "update_id": 1,
            "callback_query": {
                "id": "query",
                "from": {"id": 1, "is_bot": false, "first_name": "Ada"},
                "chat_instance": "instance",
                "data": "item:42"
            }
        }))
        .unwrap();

        assert!(dispatcher.feed_update(bot(), update).await.unwrap());
    }

    #[tokio::test]
    async fn magic_fields_match_nested_values_and_capture_data() {
        let mut router = Router::new();
        router.message_filters(
            vec![
                filters::boxed(filters::field("text").regex(r"^hello-\d+$").unwrap()),
                filters::boxed(filters::field("chat.id").equals(4_i64)),
                filters::boxed(filters::update_field("message.text").capture::<String>()),
            ],
            |context| async move {
                assert_eq!(context.dependency::<String>().unwrap().as_str(), "hello-42");
                Ok(())
            },
        );
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        assert!(
            dispatcher
                .feed_update(bot(), update("message", "hello-42"))
                .await
                .unwrap()
        );
        assert!(
            !dispatcher
                .feed_update(bot(), update("message", "goodbye"))
                .await
                .unwrap()
        );
    }

    #[tokio::test]
    async fn fsm_context_is_available_to_state_filters_and_handlers() {
        let calls = Arc::new(AtomicUsize::new(0));
        let captured = calls.clone();
        let state = crate::fsm::State::new("Form", "name");
        let next_state = state.clone();
        let mut router = Router::new();
        router.message(filters::command("begin"), move |context| {
            let next_state = next_state.clone();
            async move {
                context
                    .dependency::<crate::fsm::FsmContext>()
                    .unwrap()
                    .set_state(next_state)
                    .await
            }
        });
        router.message(
            filters::StateFilter::new(state).and(filters::text("next")),
            move |_| {
                let captured = captured.clone();
                async move {
                    captured.fetch_add(1, Ordering::SeqCst);
                    Ok(())
                }
            },
        );
        let storage = Arc::new(crate::fsm::MemoryStorage::default());
        let mut dispatcher = Dispatcher::new();
        dispatcher
            .include_router(router)
            .fsm(crate::fsm::FsmMiddleware::new(storage));

        dispatcher
            .feed_update(bot(), update("message", "/begin"))
            .await
            .unwrap();
        dispatcher
            .feed_update(bot(), update("message", "next"))
            .await
            .unwrap();
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    struct TraceMiddleware(Arc<Mutex<Vec<&'static str>>>);

    #[async_trait]
    impl Middleware for TraceMiddleware {
        async fn handle(&self, context: UpdateContext, next: Next) -> Result<()> {
            self.0.lock().unwrap().push("before");
            next.run(context).await?;
            self.0.lock().unwrap().push("after");
            Ok(())
        }
    }

    struct TraceOuterMiddleware(Arc<Mutex<Vec<&'static str>>>);

    #[async_trait]
    impl OuterMiddleware for TraceOuterMiddleware {
        async fn handle(&self, context: UpdateContext, next: OuterNext<'_>) -> Result<bool> {
            self.0.lock().unwrap().push("outer-before");
            let handled = next.run(context).await?;
            self.0.lock().unwrap().push("outer-after");
            Ok(handled)
        }
    }

    #[tokio::test]
    async fn middleware_wraps_handler_and_skip_continues() {
        let trace = Arc::new(Mutex::new(Vec::new()));
        let first_trace = trace.clone();
        let second_trace = trace.clone();
        let mut router = Router::new();
        router.middleware(TraceMiddleware(trace.clone()));
        router.message(filters::any(), move |_| {
            let trace = first_trace.clone();
            async move {
                trace.lock().unwrap().push("skip");
                skip()
            }
        });
        router.message(filters::any(), move |_| {
            let trace = second_trace.clone();
            async move {
                trace.lock().unwrap().push("handler");
                Ok(())
            }
        });
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        assert!(
            dispatcher
                .feed_update(bot(), update("message", "hello"))
                .await
                .unwrap()
        );
        assert_eq!(
            *trace.lock().unwrap(),
            vec!["before", "skip", "before", "handler", "after"]
        );
    }

    #[tokio::test]
    async fn outer_middleware_runs_before_filters_even_when_no_route_matches() {
        let trace = Arc::new(Mutex::new(Vec::new()));
        let mut router = Router::new();
        router.outer_middleware(TraceOuterMiddleware(trace.clone()));
        router.message(filters::text("expected"), |_| async { Ok(()) });
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        assert!(
            !dispatcher
                .feed_update(bot(), update("message", "different"))
                .await
                .unwrap()
        );
        assert_eq!(*trace.lock().unwrap(), vec!["outer-before", "outer-after"]);
    }

    #[tokio::test]
    async fn observer_root_filter_and_middlewares_scope_to_event_and_gate_children() {
        let trace = Arc::new(Mutex::new(Vec::new()));
        let calls = Arc::new(AtomicUsize::new(0));
        let child_calls = calls.clone();
        let mut child = Router::new();
        child.message(filters::any(), move |_| {
            let child_calls = child_calls.clone();
            async move {
                child_calls.fetch_add(1, Ordering::SeqCst);
                Ok(())
            }
        });
        child.edited_message(filters::any(), |_| async { Ok(()) });

        let mut root = Router::new();
        root.message_filter(filters::text("allowed"));
        root.message_middleware(TraceMiddleware(trace.clone()));
        root.message_outer_middleware(TraceOuterMiddleware(trace.clone()));
        root.include_router(child);
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(root);

        assert!(
            !dispatcher
                .feed_update(bot(), update("message", "blocked"))
                .await
                .unwrap()
        );
        assert_eq!(calls.load(Ordering::SeqCst), 0);
        assert_eq!(*trace.lock().unwrap(), vec!["outer-before", "outer-after"]);
        trace.lock().unwrap().clear();

        assert!(
            dispatcher
                .feed_update(bot(), update("message", "allowed"))
                .await
                .unwrap()
        );
        assert_eq!(calls.load(Ordering::SeqCst), 1);
        assert_eq!(
            *trace.lock().unwrap(),
            vec!["outer-before", "before", "after", "outer-after"]
        );
        trace.lock().unwrap().clear();

        assert!(
            dispatcher
                .feed_update(bot(), update("edited_message", "blocked"))
                .await
                .unwrap()
        );
        assert!(trace.lock().unwrap().is_empty());
    }

    #[tokio::test]
    async fn error_observer_receives_handler_failure() {
        let calls = Arc::new(AtomicUsize::new(0));
        let captured = calls.clone();
        let mut router = Router::new();
        router.message(filters::any(), |_| async {
            Err(Error::Handler("boom".to_owned()))
        });
        router.error(filters::any(), move |context| {
            let captured = captured.clone();
            async move {
                assert_eq!(context.event_type(), Some("error"));
                assert_eq!(context.error().unwrap().to_string(), "handler error: boom");
                captured.fetch_add(1, Ordering::SeqCst);
                Ok(())
            }
        });
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        assert!(
            dispatcher
                .feed_update(bot(), update("message", "hello"))
                .await
                .unwrap()
        );
        assert_eq!(calls.load(Ordering::SeqCst), 1);
        assert_eq!(
            dispatcher.resolve_used_update_types(),
            Some(vec!["message".to_owned()])
        );
    }

    #[tokio::test]
    async fn unhandled_error_is_returned_to_caller() {
        let mut router = Router::new();
        router.message(filters::any(), |_| async {
            Err(Error::Handler("unhandled".to_owned()))
        });
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);

        assert!(matches!(
            dispatcher
                .feed_update(bot(), update("message", "hello"))
                .await,
            Err(Error::Handler(message)) if message == "unhandled"
        ));
    }

    struct CountingClassHandler(Arc<AtomicUsize>);

    #[async_trait::async_trait]
    impl ClassHandler for CountingClassHandler {
        async fn handle(&self, context: UpdateContext) -> Result<()> {
            assert_eq!(context.message().unwrap().text.as_deref(), Some("hello"));
            self.0.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
    }

    #[tokio::test]
    async fn class_handler_trait_registers_stateful_handlers() {
        let calls = Arc::new(AtomicUsize::new(0));
        let mut router = Router::new();
        router.message_handler(filters::any(), CountingClassHandler(calls.clone()));
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);
        assert!(
            dispatcher
                .feed_update(bot(), update("message", "hello"))
                .await
                .unwrap()
        );
        assert_eq!(calls.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn multi_bot_polling_has_one_lifecycle_and_programmatic_shutdown() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let api_base = format!("http://{}", listener.local_addr().unwrap());
        let startup_calls = Arc::new(AtomicUsize::new(0));
        let shutdown_calls = Arc::new(AtomicUsize::new(0));
        let mut dispatcher = Dispatcher::new();
        let calls = startup_calls.clone();
        dispatcher.startup(move |bot| {
            let calls = calls.clone();
            async move {
                assert_eq!(bot.id(), 654321);
                calls.fetch_add(1, Ordering::SeqCst);
                Ok(())
            }
        });
        let calls = shutdown_calls.clone();
        dispatcher.shutdown(move |bot| {
            let calls = calls.clone();
            async move {
                assert_eq!(bot.id(), 654321);
                calls.fetch_add(1, Ordering::SeqCst);
                Ok(())
            }
        });
        let dispatcher = Arc::new(dispatcher);
        let polling_dispatcher = dispatcher.clone();
        let first =
            Bot::with_api_base("123456:abcdefghijklmnopqrstuvwxyzABCDE", api_base.clone()).unwrap();
        let second =
            Bot::with_api_base("654321:abcdefghijklmnopqrstuvwxyzABCDE", api_base).unwrap();
        let polling =
            tokio::spawn(
                async move { polling_dispatcher.start_polling_many([first, second]).await },
            );

        tokio::time::timeout(Duration::from_secs(2), async {
            while !dispatcher.is_polling() {
                tokio::task::yield_now().await;
            }
        })
        .await
        .unwrap();
        assert!(matches!(
            dispatcher.start_polling(bot()).await.unwrap_err(),
            Error::PollingAlreadyStarted
        ));
        tokio::time::timeout(Duration::from_secs(2), dispatcher.stop_polling())
            .await
            .unwrap()
            .unwrap();
        tokio::time::timeout(Duration::from_secs(2), polling)
            .await
            .unwrap()
            .unwrap()
            .unwrap();
        assert!(!dispatcher.is_polling());
        assert_eq!(startup_calls.load(Ordering::SeqCst), 1);
        assert_eq!(shutdown_calls.load(Ordering::SeqCst), 1);
        assert!(matches!(
            dispatcher.stop_polling().await.unwrap_err(),
            Error::PollingNotStarted
        ));
        drop(listener);
    }

    #[tokio::test]
    async fn lifecycle_hooks_run_in_registration_order() {
        let trace = Arc::new(Mutex::new(Vec::new()));
        let startup_trace = trace.clone();
        let shutdown_trace = trace.clone();
        let router_startup_trace = trace.clone();
        let router_shutdown_trace = trace.clone();
        let child_startup_trace = trace.clone();
        let child_shutdown_trace = trace.clone();
        let mut child = Router::new();
        child.startup(move |_| {
            let trace = child_startup_trace.clone();
            async move {
                trace.lock().unwrap().push("child-startup");
                Ok(())
            }
        });
        child.shutdown(move |_| {
            let trace = child_shutdown_trace.clone();
            async move {
                trace.lock().unwrap().push("child-shutdown");
                Ok(())
            }
        });
        let mut router = Router::new();
        router.startup(move |_| {
            let trace = router_startup_trace.clone();
            async move {
                trace.lock().unwrap().push("router-startup");
                Ok(())
            }
        });
        router.shutdown(move |_| {
            let trace = router_shutdown_trace.clone();
            async move {
                trace.lock().unwrap().push("router-shutdown");
                Ok(())
            }
        });
        router.include_router(child);
        let mut dispatcher = Dispatcher::new();
        dispatcher.include_router(router);
        dispatcher.startup(move |_| {
            let trace = startup_trace.clone();
            async move {
                trace.lock().unwrap().push("startup");
                Ok(())
            }
        });
        dispatcher.shutdown(move |_| {
            let trace = shutdown_trace.clone();
            async move {
                trace.lock().unwrap().push("shutdown");
                Ok(())
            }
        });

        dispatcher.emit_startup(bot()).await.unwrap();
        dispatcher.emit_shutdown(bot()).await.unwrap();
        assert_eq!(
            *trace.lock().unwrap(),
            vec![
                "startup",
                "router-startup",
                "child-startup",
                "shutdown",
                "router-shutdown",
                "child-shutdown"
            ]
        );
    }
}