layer 0.4.6

Ergonomic Telegram MTProto library โ€” auto-generated TL types, raw API access, session management
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
<div align="center">

<img src="https://raw.githubusercontent.com/ankit-chaubey/layer/main/docs/images/layer-banner-dark.png" alt="layer โ€” Async Rust MTProto" width="100%" />

<br/>

# โšก layer

**A modular, production-grade async Rust library for the Telegram MTProto protocol.**

*Developed By* **[Ankit Chaubey](https://github.com/ankit-chaubey)**

*Built with curiosity, caffeine, and a lot of Rust compiler errors ๐Ÿฆ€*

<br/>

[![GitHub](https://img.shields.io/badge/GitHub-ankit--chaubey-181717?style=for-the-badge&logo=github)](https://github.com/ankit-chaubey)
[![Website](https://img.shields.io/badge/Website-ankitchaubey.in-10b981?style=for-the-badge&logo=safari)](https://ankitchaubey.in)

<br/>

[![Crates.io](https://img.shields.io/crates/v/layer-client?style=for-the-badge&color=fc8d62&label=layer-client&logo=rust)](https://crates.io/crates/layer-client)
[![Downloads](https://img.shields.io/crates/d/layer-client?style=for-the-badge&color=f59e0b&logo=rust&label=downloads)](https://crates.io/crates/layer-client)
[![docs.rs](https://img.shields.io/badge/docs.rs-layer--client-5865F2?style=for-the-badge&logo=docs.rs)](https://docs.rs/layer-client)
[![Guide](https://img.shields.io/badge/book-online%20guide-10b981?style=for-the-badge&logo=mdbook)](https://layer.ankitchaubey.in/)

<br/>

[![License](https://img.shields.io/badge/license-MIT%20%7C%20Apache--2.0-blue?style=flat-square)](LICENSE-MIT)
[![Rust 2024](https://img.shields.io/badge/rust-2024%20edition-f74c00?style=flat-square&logo=rust)](https://www.rust-lang.org/)
[![TL Layer](https://img.shields.io/badge/TL%20Layer-224-8b5cf6?style=flat-square)](https://core.telegram.org/schema)
[![Tokio](https://img.shields.io/badge/async-tokio-6366f1?style=flat-square)](https://tokio.rs)
[![Build](https://img.shields.io/badge/build-passing-22c55e?style=flat-square)](https://github.com/ankit-chaubey/layer/actions)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen?style=flat-square)](CONTRIBUTING.md)

<br/>

[![Telegram Channel](https://img.shields.io/badge/channel-%40layer__rs-2CA5E0?style=for-the-badge&logo=telegram)](https://t.me/layer_rs)
[![Telegram Chat](https://img.shields.io/badge/chat-%40layer__chat-2CA5E0?style=for-the-badge&logo=telegram)](https://t.me/layer_chat)

</div>

<br/>

> **Pre-production (`0.x.x`)** โ€” APIs may change between minor versions. Review the [CHANGELOG]CHANGELOG.md before upgrading.

<br/>

---

## Table of Contents

- [What is layer?]#-what-is-layer
- [What makes layer unique?]#-what-makes-layer-unique
- [Crate Overview]#-crate-overview
- [Installation]#-installation
- [The Minimal Bot โ€” 15 Lines]#-the-minimal-bot--15-lines
- [Quick Start โ€” User Account]#-quick-start--user-account
- [Quick Start โ€” Bot]#-quick-start--bot
  - [Spawning per-update tasks]#spawning-per-update-tasks
- [ClientBuilder]#-clientbuilder
- [String Sessions โ€” Portable Auth]#-string-sessions--portable-auth
- [Update Stream]#-update-stream
  - [Update variants]#update-variants
  - [IncomingMessage API]#incomingmessage-api
- [Messaging]#-messaging
  - [Send text]#send-text
  - [InputMessage builder]#inputmessage-builder
  - [Edit, forward, delete]#edit-forward-delete
  - [Pin and unpin]#pin-and-unpin
  - [Scheduled messages]#scheduled-messages
  - [Chat actions and typing]#chat-actions-and-typing
- [Media]#-media
  - [Upload]#upload
  - [Download]#download
- [Keyboards and Reply Markup]#-keyboards-and-reply-markup
  - [Inline keyboards]#inline-keyboards
  - [Reply keyboards]#reply-keyboards
  - [Answer callback queries]#answer-callback-queries
  - [Inline mode]#inline-mode
- [Text Formatting]#-text-formatting
  - [Markdown]#markdown
  - [HTML]#html
- [Reactions]#-reactions
- [Typing Guard (RAII)]#-typing-guard-raii
- [Participants and Chat Management]#-participants-and-chat-management
  - [Fetch participants]#fetch-participants
  - [Ban, kick, promote]#ban-kick-promote
  - [Profile photos]#profile-photos
- [Search]#-search
  - [In-chat search]#in-chat-search
  - [Global search]#global-search
- [Dialogs and Iterators]#-dialogs-and-iterators
- [Peer Resolution]#-peer-resolution
- [Session Backends]#-session-backends
- [Feature Flags]#-feature-flags
- [Raw API Escape Hatch]#-raw-api-escape-hatch
- [Transports]#-transports
- [Networking โ€” SOCKS5 and DC Pool]#-networking--socks5-and-dc-pool
- [Error Handling]#-error-handling
- [Shutdown]#-shutdown
- [Updating the TL Layer]#-updating-the-tl-layer
- [Running Tests]#-running-tests
- [Unsupported Features]#-unsupported-features
- [Community]#-community
- [Contributing]#-contributing
- [Security]#-security
- [Author]#-author
- [Acknowledgements]#-acknowledgements
- [License]#-license
- [Telegram Terms of Service]#%EF%B8%8F-telegram-terms-of-service

<br/>

---

## ๐Ÿงฉ What is layer?

**layer** is a hand-crafted, bottom-up async Rust implementation of the [Telegram MTProto](https://core.telegram.org/mtproto) protocol.

Every core piece โ€” the `.tl` schema parser, the AES-IGE cipher, the Diffie-Hellman key exchange, the MTProto session, the async typed update stream โ€” is written from scratch, owned by this project, and fully understood. The async runtime and a handful of well-known utilities (`tokio`, `flate2`, `getrandom`) come from the ecosystem, because that's good engineering.

The goal was never *"yet another Telegram SDK."* It was: **what happens if you sit down and build every piece yourself, and truly understand why it works?**

<br/>

---

## ๐Ÿ’ก What makes layer unique?

Most Telegram libraries are thin wrappers around generated code or ports from other languages. layer is different.

**Built from first principles.** The `.tl` schema parser, the AES-IGE cipher, the Diffie-Hellman key exchange, and the MTProto framing are all implemented from scratch โ€” not borrowed from a C++ library or wrapped behind FFI. Every algorithm is understood and owned by this project.

**Modular workspace architecture.** layer is not a monolith. Each concern lives in its own focused crate: schema parsing, code generation, cryptographic primitives, the protocol session, and the high-level client are all separate, versioned, independently usable pieces.

**A full escape hatch.** Every one of Telegram's 2,329 Layer 224 API methods is accessible via `client.invoke()` with the fully-typed TL schema โ€” even if no high-level wrapper exists yet. You never hit a wall.

**Unique session flexibility.** layer ships with binary file, in-memory, string (base64), SQLite, and libsql/Turso session backends out of the box โ€” and supports custom `SessionBackend` implementations for any other storage (Redis, Postgres, S3, etc.).

**Android / Termux tested.** The reconnect logic, backoff parameters, and socket handling are tuned for mobile conditions. layer is actively developed and tested on Android via Termux.

**No `unsafe`, pure async Rust.** The entire stack from cryptographic primitives to the high-level client is safe Rust, running on Tokio.

<br/>

<div align="center">
<img src="https://raw.githubusercontent.com/ankit-chaubey/layer/main/docs/images/arch-stack.svg" alt="layer crate architecture" width="100%"/>
</div>

<br/>

---

## ๐Ÿ—๏ธ Crate Overview

<div align="center">
<img src="https://raw.githubusercontent.com/ankit-chaubey/layer/main/docs/images/feature-flags.svg" alt="Feature flags" width="100%"/>
</div>

<br/>

layer is a workspace of focused crates. Most users only ever need **`layer-client`**.

| Crate | Version | Description |
|---|:---:|---|
| [`layer-client`]./layer-client | [![crates.io]https://img.shields.io/crates/v/layer-client?style=flat-square&color=fc8d62]https://crates.io/crates/layer-client | High-level async client: auth, send, receive, media, bots |
| [`layer-tl-types`]./layer-tl-types | [![crates.io]https://img.shields.io/crates/v/layer-tl-types?style=flat-square&color=f59e0b]https://crates.io/crates/layer-tl-types | All Layer **224** constructors, functions, and enums (2,329 definitions) |
| [`layer-mtproto`]./layer-mtproto | [![crates.io]https://img.shields.io/crates/v/layer-mtproto?style=flat-square&color=6366f1]https://crates.io/crates/layer-mtproto | MTProto session, DH exchange, message framing, transports |
| [`layer-crypto`]./layer-crypto | [![crates.io]https://img.shields.io/crates/v/layer-crypto?style=flat-square&color=8b5cf6]https://crates.io/crates/layer-crypto | AES-IGE, RSA, SHA, Diffie-Hellman, auth key derivation |
| [`layer-tl-gen`]./layer-tl-gen | [![crates.io]https://img.shields.io/crates/v/layer-tl-gen?style=flat-square&color=10b981]https://crates.io/crates/layer-tl-gen | Build-time Rust code generator from the TL AST |
| [`layer-tl-parser`]./layer-tl-parser | [![crates.io]https://img.shields.io/crates/v/layer-tl-parser?style=flat-square&color=22c55e]https://crates.io/crates/layer-tl-parser | Parses `.tl` schema text into an AST |
| `layer-app` | โŒ | Interactive demo binary (not published) |
| `layer-connect` | โŒ | Raw DH connection demo (not published) |

```
layer/
โ”œโ”€โ”€ layer-tl-parser/      .tl schema text โ†’ AST
โ”œโ”€โ”€ layer-tl-gen/         AST โ†’ Rust source (build-time codegen)
โ”œโ”€โ”€ layer-tl-types/       Auto-generated types, functions & enums (Layer 224)
โ”œโ”€โ”€ layer-crypto/         AES-IGE, RSA, SHA, auth key derivation, PQ factorization
โ”œโ”€โ”€ layer-mtproto/        MTProto session, DH handshake, framing, transport
โ”œโ”€โ”€ layer-client/         High-level async Client API  โ† you are here
โ”œโ”€โ”€ layer-connect/        Demo: raw DH + getConfig
โ””โ”€โ”€ layer-app/            Demo: interactive login + update stream
```

> The full API reference lives at **[docs.rs/layer-client]https://docs.rs/layer-client**.
> The narrative guide lives at **[layer.ankitchaubey.in]https://layer.ankitchaubey.in/**.

<br/>

---

## ๐Ÿ“ฆ Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
layer-client = "0.4.6"
tokio        = { version = "1", features = ["full"] }
```

Get your `api_id` and `api_hash` from **[my.telegram.org](https://my.telegram.org)** โ€” every Telegram client needs them.

**Optional feature flags:**

```toml
# SQLite session persistence (stores auth key in a local .db file)
layer-client = { version = "0.4.6", features = ["sqlite-session"] }

# libsql / Turso remote or embedded database session
layer-client = { version = "0.4.6", features = ["libsql-session"] }

# Hand-rolled HTML entity parser (parse_html / generate_html)
layer-client = { version = "0.4.6", features = ["html"] }

# Spec-compliant html5ever tokenizer โ€” replaces the built-in html parser
layer-client = { version = "0.4.6", features = ["html5ever"] }
```

> **Note:** `layer-client` re-exports `layer_tl_types` as `layer_client::tl`, so you usually do not need to add `layer-tl-types` as a direct dependency.

<br/>

---

## โšก The Minimal Bot โ€” 15 Lines

This is the least code you need to have a working, update-receiving Telegram bot running with layer.

```rust
use layer_client::{Client, Config, update::Update};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let (client, _shutdown) = Client::connect(Config {
        session_path: "bot.session".into(),
        api_id:   std::env::var("API_ID")?.parse()?,
        api_hash: std::env::var("API_HASH")?,
        ..Default::default()
    }).await?;

    client.bot_sign_in(&std::env::var("BOT_TOKEN")?).await?;
    client.save_session().await?;

    let mut stream = client.stream_updates();
    while let Some(Update::NewMessage(msg)) = stream.next().await {
        if let (false, Some(text), Some(peer)) = (msg.outgoing(), msg.text(), msg.peer_id()) {
            client.send_message_to_peer(peer.clone(), &format!("Echo: {text}")).await?;
        }
    }
    Ok(())
}
```

No trait objects, no callbacks, no `dyn Handler`. Just an async loop and pattern matching. That's the whole bot.

> [๐Ÿ“– Read more in the Bot Quick Start guide โ†’]https://layer.ankitchaubey.in/quickstart-bot.html

<br/>

---

## ๐Ÿ‘ค Quick Start โ€” User Account

```rust
use layer_client::{Client, Config, SignInError};
use std::io::{self, BufRead};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (client, _shutdown) = Client::connect(Config {
        session_path: "my.session".into(),
        api_id:       12345,
        api_hash:     "your_api_hash".into(),
        ..Default::default()
    })
    .await?;

    if !client.is_authorized().await? {
        let phone = "+1234567890";
        let token = client.request_login_code(phone).await?;

        print!("Enter code: ");
        let stdin = io::stdin();
        let code  = stdin.lock().lines().next().unwrap()?;

        match client.sign_in(&token, &code).await {
            Ok(name) => println!("Welcome, {name}!"),
            Err(SignInError::PasswordRequired(t)) => {
                // 2FA โ€” read password and call check_password
                client.check_password(*t, "my_2fa_password").await?;
            }
            Err(e) => return Err(e.into()),
        }
        client.save_session().await?;
    }

    let me = client.get_me().await?;
    println!("Logged in as: {}", me.first_name.unwrap_or_default());

    // Send a message to Saved Messages
    client.send_message("me", "Hello from layer! ๐Ÿ‘‹").await?;

    // Or send to any peer
    client.send_message_to_peer("@username", "Hello!").await?;

    Ok(())
}
```

> After the first successful login the session is persisted to `my.session`. Subsequent runs skip the phone/code flow entirely.

> [๐Ÿ“– Full user account guide โ†’]https://layer.ankitchaubey.in/quickstart-user.html

<br/>

---

## ๐Ÿค– Quick Start โ€” Bot

```rust
use layer_client::{Client, Config, update::Update};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let (client, _shutdown) = Client::connect(Config {
        session_path: "bot.session".into(),
        api_id:       12345,
        api_hash:     "your_api_hash".into(),
        ..Default::default()
    })
    .await?;

    if !client.is_authorized().await? {
        client.bot_sign_in("1234567890:ABCdef...").await?;
        client.save_session().await?;
    }

    let me = client.get_me().await?;
    println!("@{} is online", me.username.as_deref().unwrap_or("bot"));

    let mut stream = client.stream_updates();
    while let Some(update) = stream.next().await {
        match update {
            Update::NewMessage(msg) if !msg.outgoing() => {
                if let Some(peer) = msg.peer_id() {
                    client
                        .send_message_to_peer(
                            peer.clone(),
                            &format!("You said: {}", msg.text().unwrap_or("")),
                        )
                        .await?;
                }
            }
            Update::CallbackQuery(cb) => {
                client
                    .answer_callback_query(cb.query_id, Some("โœ… Done!"), false)
                    .await?;
            }
            _ => {}
        }
    }
    Ok(())
}
```

### Spawning per-update tasks

For production bots the update loop should never block. Spawn each update into its own task:

```rust
use layer_client::{Client, update::Update};
use std::sync::Arc;

// Wrap in Arc so it can be moved into spawned tasks
let client = Arc::new(client);
let mut stream = client.stream_updates();

while let Some(update) = stream.next().await {
    let c = client.clone();
    tokio::spawn(async move {
        if let Err(e) = handle_update(update, &c).await {
            eprintln!("handler error: {e}");
        }
    });
}

async fn handle_update(
    update: Update,
    client: &Client,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    match update {
        Update::NewMessage(msg) if !msg.outgoing() => {
            if let Some(peer) = msg.peer_id() {
                client.send_message_to_peer(peer.clone(), "๐Ÿ‘‹").await?;
            }
        }
        _ => {}
    }
    Ok(())
}
```

> [๐Ÿ“– Full production bot guide โ†’]https://layer.ankitchaubey.in/quickstart-bot.html

<br/>

---

## ๐Ÿ”จ ClientBuilder

The fluent [`ClientBuilder`](./layer-client/src/builder.rs) is the cleanest way to configure a connection when you need more than defaults:

```rust
use layer_client::Client;

let (client, _shutdown) = Client::builder()
    .api_id(12345)
    .api_hash("your_api_hash")
    .session("my.session")          // BinaryFileBackend at this path
    .catch_up(true)                 // replay missed updates on reconnect
    .connect()
    .await?;
```

Use `.session_string(s)` for portable base64 sessions (no file on disk):

```rust
let session = std::env::var("SESSION").unwrap_or_default();

let (client, _shutdown) = Client::builder()
    .api_id(12345)
    .api_hash("your_api_hash")
    .session_string(session)
    .connect()
    .await?;
```

Use `.socks5(host, port)` for a proxy:

```rust
let (client, _shutdown) = Client::builder()
    .api_id(12345)
    .api_hash("your_api_hash")
    .session("proxy.session")
    .socks5("127.0.0.1", 1080)
    .connect()
    .await?;
```

> [๐Ÿ“– ClientBuilder reference โ†’]https://docs.rs/layer-client/latest/layer_client/builder/struct.ClientBuilder.html

<br/>

---

## ๐Ÿ”‘ String Sessions โ€” Portable Auth

A string session encodes the entire auth state (auth key, DC, peer cache) into a single printable base64 string. Store it in an environment variable, a database column, a secret manager โ€” anywhere.

```rust
// โ”€โ”€ Export from any running client โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
let session_string = client.export_session_string().await?;
println!("{session_string}");  // save this somewhere safe

// โ”€โ”€ Restore later โ€” no phone/code needed โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
let (client, _shutdown) = Client::with_string_session(&session_string).await?;

// Or via builder
let (client, _shutdown) = Client::builder()
    .api_id(12345)
    .api_hash("your_api_hash")
    .session_string(session_string)
    .connect()
    .await?;
```

String sessions are ideal for serverless deployments, CI/CD bots, and any environment where writing files is inconvenient.

> [๐Ÿ“– Session backends guide โ†’]https://layer.ankitchaubey.in/authentication/session-backends.html

<br/>

---

## ๐Ÿ“ก Update Stream

[`client.stream_updates()`](https://docs.rs/layer-client/latest/layer_client/struct.Client.html#method.stream_updates) returns an [`UpdateStream`](https://docs.rs/layer-client/latest/layer_client/struct.UpdateStream.html) that yields typed updates:

```rust
let mut stream = client.stream_updates();
while let Some(update) = stream.next().await {
    // ...
}
```

`stream_updates()` is cheap and can be called multiple times. Each call returns an independent receiver. Use `Arc<Client>` and clone it into spawned tasks.

### Update variants

```rust
use layer_client::update::Update;

match update {
    // โ”€โ”€ Messages โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    Update::NewMessage(msg)     => { /* new incoming message */ }
    Update::MessageEdited(msg)  => { /* existing message was edited */ }
    Update::MessageDeleted(del) => { /* one or more messages were deleted */ }

    // โ”€โ”€ Bot interactions โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    Update::CallbackQuery(cb)   => { /* inline button was pressed */ }
    Update::InlineQuery(iq)     => { /* @bot query in inline mode */ }
    Update::InlineSend(is)      => { /* user selected an inline result */ }

    // โ”€โ”€ Presence โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    Update::UserTyping(action)  => { /* typing / uploading / recording */ }
    Update::UserStatus(status)  => { /* contact went online / offline */ }

    // โ”€โ”€ Raw passthrough โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    Update::Raw(raw)            => { /* any unmapped TL update */ }

    _ => {}  // Update is #[non_exhaustive] โ€” always add a fallback
}
```

> **Important:** `Update` is `#[non_exhaustive]`. Always include `_ => {}` to stay forward-compatible as new variants are added.

### IncomingMessage API

[`IncomingMessage`](https://docs.rs/layer-client/latest/layer_client/update/struct.IncomingMessage.html) is the type of `NewMessage` and `MessageEdited`:

```rust
Update::NewMessage(msg) => {
    msg.id()          // i32 โ€” unique message ID in the chat
    msg.text()        // Option<&str> โ€” text or caption
    msg.peer_id()     // Option<&tl::enums::Peer> โ€” the chat this message is in
    msg.sender_id()   // Option<&tl::enums::Peer> โ€” who sent it
    msg.outgoing()    // bool โ€” was this sent by us?
    msg.date()        // i32 โ€” Unix timestamp
    msg.edit_date()   // Option<i32> โ€” last edit timestamp
    msg.mentioned()   // bool โ€” are we mentioned?
    msg.silent()      // bool โ€” no notification?
    msg.pinned()      // bool โ€” is the message currently pinned?
    msg.post()        // bool โ€” is this a channel post (no sender)?
    msg.raw           // tl::enums::Message โ€” full TL object for everything else
}
```

> [๐Ÿ“– Incoming message reference โ†’]https://layer.ankitchaubey.in/updates/incoming-message.html

<br/>

---

## ๐Ÿ’ฌ Messaging

### Send text

The simplest send methods accept any `impl Into<PeerRef>` โ€” a `&str` username, `"me"` for Saved Messages, a `tl::enums::Peer` clone, or a numeric ID:

```rust
// By username
client.send_message("@username", "Hello!").await?;

// To Saved Messages
client.send_message("me", "Note to self").await?;

// By TL Peer (from an incoming message)
if let Some(peer) = msg.peer_id() {
    client.send_message_to_peer(peer.clone(), "Reply!").await?;
}

// To self โ€” shorthand for "me"
client.send_to_self("Reminder: buy milk ๐Ÿฅ›").await?;
```

### InputMessage builder

[`InputMessage`](https://docs.rs/layer-client/latest/layer_client/struct.InputMessage.html) gives you full control over every send option:

```rust
use layer_client::{InputMessage, parsers::parse_markdown};
use layer_client::keyboard::InlineKeyboard;

let (text, entities) = parse_markdown("**Bold** and `code`");

let kb = InlineKeyboard::new()
    .row()
    .callback("โœ… Confirm", b"confirm")
    .url("๐Ÿ”— Docs", "https://docs.rs/layer-client")
    .build();

client
    .send_message_to_peer_ex(
        peer.clone(),
        &InputMessage::text(text)
            .entities(entities)         // formatted text
            .reply_to(Some(msg_id))     // reply to a specific message
            .silent(true)               // no notification
            .no_webpage(true)           // suppress link preview
            .keyboard(kb),              // attach inline keyboard
    )
    .await?;
```

### Edit, forward, delete

```rust
// Edit
client.edit_message(peer.clone(), message_id, "Updated text").await?;

// Forward messages between peers
client.forward_messages(
    from_peer.clone(),
    to_peer.clone(),
    &[message_id_1, message_id_2],
).await?;

// Delete (also removes from the other side if you have permission)
client.delete_messages(peer.clone(), &[message_id]).await?;
```

### Pin and unpin

```rust
// Pin a message (notify: true sends a "pinned message" service message)
client.pin_message(peer.clone(), message_id, true).await?;

// Get the current pinned message
let pinned = client.get_pinned_message(peer.clone()).await?;

// Unpin a specific message
client.unpin_message(peer.clone(), message_id).await?;

// Unpin all at once
client.unpin_all_messages(peer.clone()).await?;
```

### Scheduled messages

```rust
use std::time::{SystemTime, UNIX_EPOCH};

// Schedule for 1 hour from now
let schedule_ts = (SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .unwrap()
    .as_secs() + 3600) as i32;

client
    .send_message_to_peer_ex(
        peer.clone(),
        &InputMessage::text("Reminder! โฐ").schedule_date(Some(schedule_ts)),
    )
    .await?;

// List all scheduled messages in a chat
let scheduled = client.get_scheduled_messages(peer.clone()).await?;

// Cancel a scheduled message
client.delete_scheduled_messages(peer.clone(), &[scheduled_msg_id]).await?;
```

### Chat actions and typing

```rust
use layer_tl_types as tl;

// Start a "typing..." indicator
client.send_chat_action(
    peer.clone(),
    tl::enums::SendMessageAction::SendMessageTypingAction,
    None,  // top_msg_id โ€” None for normal chats, Some(id) for forum topics
).await?;

// Mark all messages as read
client.mark_as_read(peer.clone()).await?;

// Clear all @mention badges
client.clear_mentions(peer.clone()).await?;
```

> [๐Ÿ“– Full messaging reference โ†’]https://layer.ankitchaubey.in/messaging/sending.html

<br/>

---

## ๐Ÿ“Ž Media

### Upload

```rust
use layer_client::media::UploadedFile;

// Upload from bytes โ€” small files sequentially
let uploaded: UploadedFile = client
    .upload_file("photo.jpg", file_bytes.as_ref())
    .await?;

// Upload from bytes โ€” parallel chunks (faster for large files)
let uploaded = client
    .upload_file_concurrent("video.mp4", video_bytes.as_ref())
    .await?;

// Upload from an async reader (e.g. a file on disk)
use tokio::fs::File;
let f = File::open("document.pdf").await?;
let uploaded = client
    .upload_stream("document.pdf", f)
    .await?;

// Send the uploaded file to a peer
client.send_file(peer.clone(), uploaded, /* as_photo */ false).await?;

// Send multiple files as an album in one call
client.send_album(peer.clone(), vec![uploaded_a, uploaded_b]).await?;
```

### Download

```rust
// Download directly to a file path (streaming, no full memory buffer)
client
    .download_media_to_file(&message_media, "output.jpg")
    .await?;

// Download to Vec<u8> โ€” sequential
let bytes: Vec<u8> = client.download_media(&message_media).await?;

// Download to Vec<u8> โ€” parallel chunks
let bytes: Vec<u8> = client.download_media_concurrent(&message_media).await?;

// Use the Downloadable trait for Photos, Documents, Stickers
use layer_client::media::{Photo, Downloadable};
let photo = Photo::from_message(&msg.raw)?;
let bytes = client.download(&photo).await?;
```

> [๐Ÿ“– Media guide โ†’]https://layer.ankitchaubey.in/messaging/media.html

<br/>

---

## โŒจ๏ธ Keyboards and Reply Markup

### Inline keyboards

```rust
use layer_client::keyboard::InlineKeyboard;

let kb = InlineKeyboard::new()
    .row()
        .callback("๐Ÿ‘ Like",    b"like")
        .callback("๐Ÿ‘Ž Dislike", b"dislike")
    .row()
        .url("๐Ÿ”— Open docs", "https://docs.rs/layer-client")
        .switch_inline("๐Ÿ” Search", "query")
    .build();

client
    .send_message_to_peer_ex(peer.clone(), &InputMessage::text("Vote!").keyboard(kb))
    .await?;
```

Available button types: `callback`, `url`, `url_auth`, `switch_inline`, `switch_elsewhere`, `webview`, `simple_webview`, `request_phone`, `request_geo`, `request_poll`, `request_quiz`, `game`, `buy`, `copy_text`.

### Reply keyboards

```rust
use layer_client::keyboard::ReplyKeyboard;

let kb = ReplyKeyboard::new()
    .row()
        .text("๐Ÿ“ธ Photo")
        .text("๐Ÿ“„ Document")
    .row()
        .text("โŒ Cancel")
    .resize(true)
    .single_use(true)
    .build();

client
    .send_message_to_peer_ex(peer.clone(), &InputMessage::text("Choose:").keyboard(kb))
    .await?;
```

### Answer callback queries

```rust
Update::CallbackQuery(cb) => {
    let data = cb.data().unwrap_or("");
    match data {
        b"like"    => client.answer_callback_query(cb.query_id, Some("โค๏ธ Liked!"), false).await?,
        b"dislike" => client.answer_callback_query(cb.query_id, Some("๐Ÿ‘Ž Noted"),   false).await?,
        _          => client.answer_callback_query(cb.query_id, None,              false).await?,
    }
}
```

Pass `alert: true` as the third argument to show a popup alert instead of a toast.

### Inline mode

```rust
use layer_tl_types as tl;

Update::InlineQuery(iq) => {
    let q   = iq.query().to_string();
    let qid = iq.query_id;

    let results = vec![
        tl::enums::InputBotInlineResult::InputBotInlineResult(
            tl::types::InputBotInlineResult {
                id: "1".into(), r#type: "article".into(),
                title: Some("Result title".into()),
                description: Some(q.clone()),
                url: None, thumb: None, content: None,
                send_message: tl::enums::InputBotInlineMessage::Text(
                    tl::types::InputBotInlineMessageText {
                        no_webpage: false, invert_media: false,
                        message: q, entities: None, reply_markup: None,
                    },
                ),
            },
        ),
    ];

    // cache_time: 30s, is_personal: false, next_offset: None
    client.answer_inline_query(qid, results, 30, false, None).await?;
}
```

> [๐Ÿ“– Keyboards guide โ†’]https://layer.ankitchaubey.in/messaging/keyboards.html

<br/>

---

## ๐Ÿ–Š๏ธ Text Formatting

### Markdown

```rust
use layer_client::parsers::{parse_markdown, generate_markdown};

// Parse markdown โ†’ plain text + message entities
let (text, entities) = parse_markdown("**Bold**, `code`, _italic_, [link](https://example.com)");

// Send with formatting
client
    .send_message_to_peer_ex(
        peer.clone(),
        &InputMessage::text(text).entities(entities),
    )
    .await?;

// Go the other way: entities + plain text โ†’ markdown string
let md = generate_markdown(&plain_text, &entities);
```

### HTML

Enable the `html` or `html5ever` feature flag:

```toml
layer-client = { version = "0.4.6", features = ["html"] }
```

```rust
use layer_client::parsers::{parse_html, generate_html};

let (text, entities) = parse_html("<b>Bold</b> and <code>monospace</code>");

client
    .send_message_to_peer_ex(peer.clone(), &InputMessage::text(text).entities(entities))
    .await?;

// Always available, no feature flag needed
let html_str = generate_html(&plain_text, &entities);
```

> [๐Ÿ“– Formatting reference โ†’]https://layer.ankitchaubey.in/messaging/formatting.html

<br/>

---

## ๐Ÿ’ฅ Reactions

[`InputReactions`](https://docs.rs/layer-client/latest/layer_client/reactions/struct.InputReactions.html) is the typed builder for reactions:

```rust
use layer_client::reactions::InputReactions;

// Single emoji reaction
client.send_reaction(peer.clone(), message_id, InputReactions::emoticon("๐Ÿ‘")).await?;

// Custom premium emoji
client.send_reaction(peer.clone(), message_id, InputReactions::custom_emoji(1234567890)).await?;

// Big animated reaction
client.send_reaction(peer.clone(), message_id, InputReactions::emoticon("๐Ÿ”ฅ").big()).await?;

// Remove all reactions
client.send_reaction(peer.clone(), message_id, InputReactions::remove()).await?;
```

> [๐Ÿ“– Reactions guide โ†’]https://layer.ankitchaubey.in/messaging/reactions.html

<br/>

---

## โŒ› Typing Guard (RAII)

[`TypingGuard`](https://docs.rs/layer-client/latest/layer_client/struct.TypingGuard.html) is a RAII wrapper that automatically starts and stops typing/uploading indicators:

```rust
use layer_client::TypingGuard;
use layer_tl_types as tl;

async fn handle_long_task(client: &Client, peer: tl::enums::Peer) -> anyhow::Result<()> {
    // Typing indicator starts immediately and is renewed every ~4 seconds
    let _typing = TypingGuard::start(
        client,
        peer.clone(),
        tl::enums::SendMessageAction::SendMessageTypingAction,
    )
    .await?;

    // Do expensive work โ€” user sees "typing..."
    do_expensive_work().await;

    // _typing is dropped here โ†’ Telegram sees the indicator stop
    Ok(())
}
```

Convenience constructors for common actions:

```rust
// Typing
let _t = client.typing(peer.clone()).await?;

// Uploading document
let _t = client.uploading_document(peer.clone()).await?;

// Recording video
let _t = client.recording_video(peer.clone()).await?;

// Typing in a specific forum topic
let _t = client.typing_in_topic(peer.clone(), topic_id).await?;
```

> [๐Ÿ“– Typing guard reference โ†’]https://layer.ankitchaubey.in/api/typing-guard.html

<br/>

---

## ๐Ÿ‘ฅ Participants and Chat Management

### Fetch participants

```rust
use layer_client::participants::Participant;

// Fetch up to N participants at once
let participants: Vec<Participant> = client.get_participants(peer.clone(), 100).await?;

// Paginated lazy iterator โ€” works for very large groups
let mut iter = client.iter_participants(peer.clone());
while let Some(p) = iter.next(&client).await? {
    println!("{}", p.user.first_name.as_deref().unwrap_or(""));
}

// Search within a group
let results = client.search_peer(peer.clone(), "John").await?;
```

### Ban, kick, promote

```rust
use layer_client::participants::{BanRights, AdminRightsBuilder};

// Kick (ban + immediate unban)
client.kick_participant(peer.clone(), user_id).await?;

// Ban with custom rights and optional expiry
client
    .ban_participant(
        peer.clone(),
        user_id,
        BanRights::new()
            .no_messages(true)
            .no_media(true)
            .until(expiry_unix_timestamp),
    )
    .await?;

// Promote to admin with specific rights
client
    .promote_participant(
        peer.clone(),
        user_id,
        AdminRightsBuilder::new()
            .post_messages(true)
            .delete_messages(true)
            .ban_users(true)
            .title("Moderator"),
    )
    .await?;

// Get a user's current permissions in a channel
let perms = client.get_permissions(peer.clone(), user_id).await?;
```

### Profile photos

```rust
// Fetch the first page of profile photos
let photos = client.get_profile_photos(user_id, 0, 10).await?;

// Lazy iterator across all pages
let mut iter = client.iter_profile_photos(user_id);
while let Some(photo) = iter.next(&client).await? {
    let bytes = client.download(&photo).await?;
}
```

### Join and leave

```rust
// Join a public group or channel by username
client.join_chat("@somegroup").await?;

// Accept a private invite link
client.accept_invite_link("https://t.me/joinchat/AbCdEfG...").await?;

// Leave and delete a dialog from the dialog list
client.delete_dialog(peer.clone()).await?;
```

> [๐Ÿ“– Participants guide โ†’]https://layer.ankitchaubey.in/api/participants.html

<br/>

---

## ๐Ÿ” Search

### In-chat search

[`SearchBuilder`](https://docs.rs/layer-client/latest/layer_client/search/struct.SearchBuilder.html) is a chainable builder for `messages.search`:

```rust
use layer_tl_types::enums::MessagesFilter;

let results = client
    .search(peer.clone(), "hello world")
    .min_date(1_700_000_000)
    .max_date(1_720_000_000)
    .filter(MessagesFilter::InputMessagesFilterPhotos)
    .limit(50)
    .fetch(&client)
    .await?;

for msg in results {
    println!("[{}] {}", msg.id, msg.message);
}
```

### Global search

[`GlobalSearchBuilder`](https://docs.rs/layer-client/latest/layer_client/search/struct.GlobalSearchBuilder.html) searches across all chats:

```rust
let results = client
    .search_global_builder("rust async")
    .broadcasts_only(true)       // channels only
    .min_date(1_700_000_000)
    .limit(30)
    .fetch(&client)
    .await?;
```

> [๐Ÿ“– Search guide โ†’]https://layer.ankitchaubey.in/api/search.html

<br/>

---

## ๐Ÿ“œ Dialogs and Iterators

```rust
// Fetch the first N dialogs
let dialogs = client.get_dialogs(50).await?;
for d in &dialogs {
    println!("{} โ€” {} unread", d.title(), d.unread_count());
}

// Lazy dialog iterator (all dialogs, paginated)
let mut iter = client.iter_dialogs();
while let Some(dialog) = iter.next(&client).await? {
    println!("{}", dialog.title());
}

// Lazy message iterator for a specific peer
let mut iter = client.iter_messages(peer.clone());
while let Some(msg) = iter.next(&client).await? {
    println!("{}", msg.message);
}

// Fetch messages by ID
let messages = client.get_messages_by_id(peer.clone(), &[100, 101, 102]).await?;

// Fetch the latest N messages from a peer
let messages = client.get_messages(peer.clone(), 20).await?;
```

> [๐Ÿ“– Dialogs guide โ†’]https://layer.ankitchaubey.in/api/dialogs.html

<br/>

---

## ๐Ÿ”— Peer Resolution

```rust
// Resolve any string (username, phone number, "me") to a TL Peer
let peer = client.resolve_peer("@telegram").await?;
let peer = client.resolve_peer("+1234567890").await?;
let peer = client.resolve_peer("me").await?;

// Resolve just the username part (without @)
let peer = client.resolve_username("telegram").await?;
```

Access hash caching is handled automatically. Once a peer is resolved its access hash is stored in the session and reused on all subsequent calls โ€” no need to manage it yourself.

<br/>

---

## ๐Ÿ’พ Session Backends

layer ships with multiple session backends. They all implement the [`SessionBackend`](https://docs.rs/layer-client/latest/layer_client/session_backend/trait.SessionBackend.html) trait and are hot-swappable.

| Backend | Feature flag | Best for |
|---|---|---|
| [`BinaryFileBackend`]https://docs.rs/layer-client/latest/layer_client/session_backend/struct.BinaryFileBackend.html | *(default)* | Single-process bots, scripts |
| [`InMemoryBackend`]https://docs.rs/layer-client/latest/layer_client/session_backend/struct.InMemoryBackend.html | *(default)* | Tests, ephemeral tasks |
| [`StringSessionBackend`]https://docs.rs/layer-client/latest/layer_client/session_backend/struct.StringSessionBackend.html | *(default)* | Serverless, env-var storage, CI bots |
| [`SqliteBackend`]https://docs.rs/layer-client/latest/layer_client/session_backend/struct.SqliteBackend.html | `sqlite-session` | Multi-session local apps |
| [`LibSqlBackend`]https://docs.rs/layer-client/latest/layer_client/session_backend/struct.LibSqlBackend.html | `libsql-session` | Distributed / Turso-backed storage |
| Custom | โ€” | Implement `SessionBackend` for anything |

```rust
use layer_client::session_backend::{SqliteBackend, SessionBackend};

// SQLite backend
let backend = SqliteBackend::new("sessions.db").await?;

let (client, _shutdown) = Client::connect(Config {
    session_backend: Box::new(backend),
    api_id:  12345,
    api_hash: "your_api_hash".into(),
    ..Default::default()
}).await?;
```

```rust
// Implement your own โ€” Redis, Postgres, S3, anything
use layer_client::session_backend::SessionBackend;

struct RedisBackend { /* ... */ }

#[async_trait::async_trait]
impl SessionBackend for RedisBackend {
    async fn load(&self) -> anyhow::Result<Option<Vec<u8>>> { /* ... */ }
    async fn save(&self, data: &[u8]) -> anyhow::Result<()> { /* ... */ }
}
```

> [๐Ÿ“– Session backends guide โ†’]https://layer.ankitchaubey.in/authentication/session-backends.html

<br/>

---

## ๐Ÿ”ง Feature Flags

### `layer-tl-types`

| Flag | Default | Description |
|---|:---:|---|
| `tl-api` | โœ… | High-level Telegram API schema (`api.tl`) |
| `tl-mtproto` | โŒ | Low-level MTProto schema (`mtproto.tl`) |
| `impl-debug` | โœ… | `#[derive(Debug)]` on all generated types |
| `impl-from-type` | โœ… | `From<types::T> for enums::E` on all constructors |
| `impl-from-enum` | โœ… | `TryFrom<enums::E> for types::T` on all constructors |
| `name-for-id` | โŒ | `name_for_id(u32) -> Option<&'static str>` lookup table |
| `impl-serde` | โŒ | `serde::Serialize` + `Deserialize` on all types |

### `layer-client`

| Flag | Default | Description |
|---|:---:|---|
| `html` | โŒ | Hand-rolled HTML parser (`parse_html`, `generate_html`) |
| `html5ever` | โŒ | Spec-compliant `html5ever` tokenizer, replaces the built-in parser |
| `sqlite-session` | โŒ | SQLite session backend (`SqliteBackend`) |
| `libsql-session` | โŒ | libsql / Turso session backend (`LibSqlBackend`) |

<br/>

---

## ๐Ÿ”ฉ Raw API Escape Hatch

Every Telegram method in **Layer 224** is available via the raw [`invoke`](https://docs.rs/layer-client/latest/layer_client/struct.Client.html#method.invoke) API, even if it has no high-level wrapper yet. The full type-safe schema is available as `layer_client::tl` (re-exported from `layer-tl-types`).

```rust
use layer_client::tl;

// Set the bot's command list โ€” no wrapper yet, use raw invoke
let req = tl::functions::bots::SetBotCommands {
    scope: tl::enums::BotCommandScope::Default(tl::types::BotCommandScopeDefault {}),
    lang_code: "en".into(),
    commands: vec![
        tl::enums::BotCommand::BotCommand(tl::types::BotCommand {
            command:     "start".into(),
            description: "Start the bot".into(),
        }),
    ],
};
client.invoke(&req).await?;
```

```rust
// Update profile info
let req = tl::functions::account::UpdateProfile {
    first_name: Some("Alice".into()),
    last_name:  None,
    about:      Some("layer user ๐Ÿฆ€".into()),
};
client.invoke(&req).await?;
```

```rust
// Send to a specific DC (useful for cross-DC file downloads)
client.invoke_on_dc(&req, 2).await?;
```

Any method listed in the [Telegram API documentation](https://core.telegram.org/method) can be invoked this way. Layer 224 includes **2,329** TL constructors and all RPC functions.

> [๐Ÿ“– Raw API guide โ†’]https://layer.ankitchaubey.in/advanced/raw-api.html

<br/>

---

## ๐Ÿš‚ Transports

Three MTProto transport encodings are supported:

| Transport | Description | When to use |
|---|---|---|
| **Abridged** | Single-byte length prefix, lowest overhead | Default โ€” best for most setups |
| **Intermediate** | 4-byte LE length prefix | Better compatibility with some proxies |
| **Obfuscated2** | XOR stream cipher over Abridged | DPI bypass, MTProxy, restricted networks |

```rust
use layer_client::{Client, TransportKind};

// Switch to Obfuscated2 (DPI bypass)
let (client, _) = Client::builder()
    .api_id(12345)
    .api_hash("your_api_hash")
    .session("obfuscated.session")
    .transport(TransportKind::Obfuscated)
    .connect()
    .await?;
```

> [๐Ÿ“– Transport reference โ†’]https://layer.ankitchaubey.in/advanced/proxy.html

<br/>

---

## ๐ŸŒ Networking โ€” SOCKS5 and DC Pool

### SOCKS5 proxy

```rust
use layer_client::{Client, Socks5Config};

// Without auth
let (client, _) = Client::builder()
    .api_id(12345)
    .api_hash("your_api_hash")
    .session("proxy.session")
    .socks5("127.0.0.1", 1080)
    .connect()
    .await?;

// With username/password
let proxy = Socks5Config::with_auth("proxy.host", 1080, "user", "pass");
let (client, _) = Client::builder()
    .api_id(12345)
    .api_hash("your_api_hash")
    .socks5_config(proxy)
    .connect()
    .await?;
```

### DC pool and multi-DC

Auth keys are stored per datacenter and connections are created on demand. When Telegram responds with `PHONE_MIGRATE_*`, `USER_MIGRATE_*`, or `NETWORK_MIGRATE_*`, the client migrates automatically. You can also target a specific DC directly:

```rust
// Force a request to DC 2
client.invoke_on_dc(&req, 2).await?;
```

### Reconnect and keepalive

The client reconnects automatically after network failures using exponential backoff with 20% jitter, capped at 5 seconds (tuned for mobile / Android conditions). Pings are sent every 60 seconds. To skip the backoff after a known-good network event:

```rust
// Call this when your app detects the network is back
client.signal_network_restored();
```

<br/>

---

## โš ๏ธ Error Handling

```rust
use layer_client::{InvocationError, RpcError};

match client.send_message("@badpeer", "Hello").await {
    Ok(()) => {}

    // Telegram RPC error โ€” has a numeric code and a string message
    Err(InvocationError::Rpc(RpcError { code, message, .. })) => {
        eprintln!("Telegram error {code}: {message}");
    }

    // Network / I/O error
    Err(InvocationError::Io(e)) => {
        eprintln!("I/O error: {e}");
    }

    // Other
    Err(e) => eprintln!("Error: {e}"),
}
```

`FLOOD_WAIT` errors are handled automatically by the default [`AutoSleep`](https://docs.rs/layer-client/latest/layer_client/retry/struct.AutoSleep.html) retry policy. You can replace this with your own policy:

```rust
use layer_client::retry::NoRetries;

// Disable all automatic retries
let (client, _) = Client::builder()
    .api_id(12345)
    .api_hash("your_api_hash")
    .retry_policy(NoRetries)
    .connect()
    .await?;
```

> [๐Ÿ“– Error handling guide โ†’]https://layer.ankitchaubey.in/errors.html

<br/>

---

## ๐Ÿ›‘ Shutdown

```rust
// Client::connect returns (Client, ShutdownToken)
let (client, shutdown) = Client::connect(config).await?;

// Graceful shutdown from any task
shutdown.cancel();

// Immediate disconnect (no drain)
client.disconnect();
```

The [`ShutdownToken`](https://docs.rs/layer-client/latest/layer_client/struct.ShutdownToken.html) is a `CancellationToken` wrapper. You can clone it and pass it to multiple tasks.

<br/>

---

## ๐Ÿ“ Updating the TL Layer

When Telegram publishes a new TL schema, updating layer is a two-step process:

```bash
# 1. Replace the schema file
cp new-api.tl layer-tl-types/tl/api.tl

# 2. Build โ€” layer-tl-gen regenerates all types at compile time
cargo build
```

The codegen (`layer-tl-gen`) runs as a build script. No manual code changes are required for pure schema updates โ€” the 2,329 type definitions are entirely auto-generated.

> [๐Ÿ“– Layer upgrade guide โ†’]https://layer.ankitchaubey.in/advanced/layer-upgrade.html

<br/>

---

## ๐Ÿงช Running Tests

```bash
# Run all tests in the workspace
cargo test --workspace

# Run only layer-client tests
cargo test -p layer-client

# Run with all features enabled
cargo test --workspace --all-features
```

Integration tests live in [`layer-client/tests/integration.rs`](./layer-client/tests/integration.rs). They use `InMemoryBackend` and do not require real Telegram credentials.

<br/>

---

## โŒ Unsupported Features

The following are gaps in the current high-level API. Every single one can be accessed today via `client.invoke::<R>()` with the raw TL types โ€” see the [Raw API Escape Hatch](#-raw-api-escape-hatch) section.

| Feature | Workaround |
|---|---|
| **Secret chats (E2E)** | Not implemented at the MTProto layer-2 level |
| **Voice and video calls** | No call signalling or media transport |
| **Payments** | `SentCode::PaymentRequired` returns an error |
| **Channel creation** | Use `invoke` with `channels::CreateChannel` |
| **Sticker set management** | Use `invoke` with `messages::GetStickerSet` etc. |
| **Account settings** | Use `invoke` with `account::UpdateProfile` etc. |
| **Contact management** | Use `invoke` with `contacts::ImportContacts` etc. |
| **Poll / quiz creation** | Use `invoke` with `InputMediaPoll` |
| **Live location** | Not wrapped |
| **Bot command registration** | Use `invoke` with `bots::SetBotCommands` |
| **IPv6** | Config flag exists but address formatting for IPv6 DCs is untested |

<br/>

---

## ๐Ÿ’ฌ Community

Questions, ideas, bug reports โ€” come talk to us:

| | Link |
|---|---|
| ๐Ÿ“ข **Channel** โ€” releases and announcements | [t.me/layer_rs]https://t.me/layer_rs |
| ๐Ÿ’ฌ **Chat** โ€” questions and discussion | [t.me/layer_chat]https://t.me/layer_chat |
| ๐Ÿ“– **Online Book** โ€” narrative guide | [layer.ankitchaubey.in]https://layer.ankitchaubey.in/ |
| ๐Ÿ“ฆ **Crates.io** | [crates.io/crates/layer-client]https://crates.io/crates/layer-client |
| ๐Ÿ“„ **API Docs** | [docs.rs/layer-client]https://docs.rs/layer-client |
| ๐Ÿ› **Issue Tracker** | [github.com/ankit-chaubey/layer/issues]https://github.com/ankit-chaubey/layer/issues |

<br/>

---

## ๐Ÿค Contributing

Contributions are welcome โ€” bug fixes, new wrappers, better docs, more tests. All pull requests are appreciated.

Please read [CONTRIBUTING.md](CONTRIBUTING.md) before opening a PR. In brief:

- Run `cargo test --workspace` and `cargo clippy --workspace` locally before pushing.
- For new wrappers, add a doc-test in the `///` comment block.
- For security issues, follow the responsible disclosure process in [SECURITY.md]SECURITY.md โ€” **do not** open a public issue.

[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen?style=flat-square)](CONTRIBUTING.md)
[![Good First Issues](https://img.shields.io/github/issues/ankit-chaubey/layer/good%20first%20issue?style=flat-square&color=5865F2&label=good%20first%20issues)](https://github.com/ankit-chaubey/layer/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22)

<br/>

---

## ๐Ÿ”’ Security

Found a vulnerability? Please report it **privately**. See [SECURITY.md](SECURITY.md) for the responsible disclosure process. Do not open a public GitHub issue for security bugs.

<br/>

---

## ๐Ÿ‘ค Author

<div align="center">

<br/>

<a href="https://github.com/ankit-chaubey">
  <img src="https://github.com/ankit-chaubey.png" width="96" style="border-radius:50%" alt="Ankit Chaubey" />
</a>

<br/><br/>

**Ankit Chaubey**

*Built with curiosity, caffeine, and a lot of Rust compiler errors ๐Ÿฆ€*

<br/>

[![GitHub](https://img.shields.io/badge/GitHub-ankit--chaubey-181717?style=for-the-badge&logo=github)](https://github.com/ankit-chaubey)
[![Website](https://img.shields.io/badge/Website-ankitchaubey.in-10b981?style=for-the-badge&logo=safari)](https://ankitchaubey.in)
[![Email](https://img.shields.io/badge/Email-ankitchaubey.dev%40gmail.com-ea4335?style=for-the-badge&logo=gmail)](mailto:ankitchaubey.dev@gmail.com)
[![Telegram](https://img.shields.io/badge/Telegram-%40layer__rs-2CA5E0?style=for-the-badge&logo=telegram)](https://t.me/layer_rs)

<br/>

</div>

---

## ๐Ÿ™ Acknowledgements

- [**Lonami**]https://codeberg.org/Lonami for [**grammers**]https://codeberg.org/Lonami/grammers โ€” the architecture, DH session design, SRP 2FA math, and session handling in layer are deeply inspired by this excellent library. Portions of this project include code derived from grammers, which is dual-licensed MIT or Apache-2.0.

- [**Telegram**]https://core.telegram.org/mtproto for the detailed MTProto specification and the publicly available TL schema.

- The Rust async ecosystem โ€” [`tokio`]https://tokio.rs, [`flate2`]https://crates.io/crates/flate2, [`getrandom`]https://crates.io/crates/getrandom, [`sha2`]https://crates.io/crates/sha2, [`socket2`]https://crates.io/crates/socket2, and friends.

<br/>

---

## ๐Ÿ“„ License

Licensed under either of, at your option:

- **MIT License** โ€” see [LICENSE-MIT]LICENSE-MIT
- **Apache License, Version 2.0** โ€” see [LICENSE-APACHE]LICENSE-APACHE

Unless you explicitly state otherwise, any contribution you submit for inclusion shall be dual-licensed as above, without any additional terms or conditions.

<br/>

---

## โš ๏ธ Telegram Terms of Service

As with any third-party Telegram library, ensure your usage complies with [Telegram's Terms of Service](https://core.telegram.org/api/terms) and [API Terms of Service](https://core.telegram.org/api/terms). Misuse of the Telegram API โ€” including but not limited to spam, mass scraping, or automation of normal user accounts โ€” may result in account limitations or permanent bans.

<br/>

---

<div align="center">

*layer โ€” because sometimes you have to build it yourself to truly understand it.*

[![Star on GitHub](https://img.shields.io/github/stars/ankit-chaubey/layer?style=social)](https://github.com/ankit-chaubey/layer)

</div>