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
// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
//
// ferogram: async Telegram MTProto client in Rust
// https://github.com/ankit-chaubey/ferogram
//
// Licensed under either the MIT License or the Apache License 2.0.
// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
// https://github.com/ankit-chaubey/ferogram
//
// Feel free to use, modify, and share this code.
// Please keep this notice when redistributing.
//! Async Rust client for the Telegram MTProto API.
//!
//! ferogram talks to Telegram directly over MTProto with no Bot API proxy. It works
//! for both bots and user accounts. Most things you'd want to do with Telegram
//! are already covered. If something isn't, you can always drop down to
//! [`client.invoke()`](Client::invoke) and call any TL function directly.
//!
//! Still in development but already covers major use cases for production.
//! Check the [CHANGELOG] before upgrading.
//!
//! [CHANGELOG]: https://github.com/ankit-chaubey/ferogram/blob/main/CHANGELOG.md
//!
//! # Quick start: bot
//!
//! ```rust,no_run
//! use ferogram::{Client, update::Update};
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let (client, _) = Client::builder()
//! .api_id(std::env::var("API_ID")?.parse()?)
//! .api_hash(std::env::var("API_HASH")?)
//! .session("bot.session")
//! .connect().await?;
//!
//! client.bot_sign_in(&std::env::var("BOT_TOKEN")?).await?;
//!
//! let mut stream = client.stream_updates();
//! while let Some(upd) = stream.next().await {
//! if let Update::NewMessage(msg) = upd {
//! if !msg.outgoing() {
//! msg.reply(msg.text().unwrap_or_default()).await.ok();
//! }
//! }
//! }
//! Ok(())
//! }
//! ```
//!
//! # Quick start: user account
//!
//! ```rust,no_run
//! use ferogram::{Client, SignInError};
//! # fn read_line() -> String { String::new() }
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//! let (client, _) = Client::builder()
//! .api_id(std::env::var("API_ID")?.parse()?)
//! .api_hash(std::env::var("API_HASH")?)
//! .session("my.session")
//! .connect().await?;
//!
//! if !client.is_authorized().await? {
//! let token = client.request_login_code("+1234567890").await?;
//! match client.sign_in(&token, &read_line()).await {
//! Ok(_) => {}
//! Err(SignInError::PasswordRequired(t)) => {
//! client.check_password(*t, &read_line()).await?;
//! }
//! Err(e) => return Err(e.into()),
//! }
//! client.save_session().await?;
//! }
//!
//! client.send_message("me", "Hello from ferogram!").await?;
//! Ok(())
//! }
//! ```
//!
//! # Dispatcher and filters
//!
//! ```rust,ignore
//! use ferogram::filters::{Dispatcher, command, private, text_contains};
//!
//! let mut dp = Dispatcher::new();
//!
//! dp.on_message(command("start"), |msg| async move {
//! msg.reply("Hello!").await.ok();
//! });
//!
//! dp.on_message(private() & text_contains("help"), |msg| async move {
//! msg.reply("Type /start to begin.").await.ok();
//! });
//!
//! while let Some(upd) = stream.next().await {
//! dp.dispatch(upd).await;
//! }
//! # }
//! ```
//!
//! Filters compose with `&`, `|`, `!`. Built-ins: `command`, `private`, `group`,
//! `channel`, `text`, `media`, `photo`, `forwarded`, `reply`, `album`, `regex`, and more.
//!
//! # FSM
//!
//! ```rust,ignore
//! use std::sync::Arc;
//!
//! #[derive(FsmState, Clone, Debug, PartialEq)]
//! enum Form { Name, Age }
//!
//! dp.with_state_storage(Arc::new(MemoryStorage::new()));
//!
//! dp.on_message_fsm(text(), Form::Name, |msg, state| async move {
//! state.set_data("name", msg.text().unwrap()).await.ok();
//! state.transition(Form::Age).await.ok();
//! msg.reply("How old are you?").await.ok();
//! });
//! ```
//!
//! # Raw API
//!
//! If something isn't wrapped yet, you can call any Layer 224 TL function directly:
//!
//! ```rust,ignore
//! use ferogram::tl;
//!
//! let req = tl::functions::messages::SendMessage {
//! peer: peer.into(),
//! message: "Hello!".into(),
//! random_id: ferogram::random_i64_pub(),
//! ..Default::default()
//! };
//! client.invoke(&req).await?;
//! ```
//!
//! # Session backends
//!
//! Binary file by default. Switch to SQLite, libSQL, or a base64 string with a
//! feature flag. Bring your own backend by implementing [`SessionBackend`].
//!
//! ```rust,ignore
//! // Portable string session, useful for serverless or env-var setups
//! let s = client.export_session_string().await?;
//! let (client, _) = Client::builder().session_string(s).connect().await?;
//! ```
//!
//! # Features
//!
//! Most common use cases are already covered. Full list in
//! [FEATURES.md](https://github.com/ankit-chaubey/ferogram/blob/main/FEATURES.md).
//!
//! If something's missing, feel free to open a feature request or PR.
//! Check the [contributing guidelines](https://github.com/ankit-chaubey/ferogram#contributing) first.
//!
//! # Community
//!
//! - Channel (releases, news): [t.me/Ferogram](https://t.me/Ferogram)
//! - Chat (questions, help): [t.me/FerogramChat](https://t.me/FerogramChat)
//! - Guide: [ferogram.ankitchaubey.in](https://ferogram.ankitchaubey.in)
//! - GitHub: [ankit-chaubey/ferogram](https://github.com/ankit-chaubey/ferogram)
// Re-export FsmState at the crate root for convenience.
pub use FsmState;
// Re-export the derive macro when the feature is enabled.
pub use FsmState;
pub use ;
pub use Client;
pub use ;
pub use ;
pub use ;
pub use TransportKind;
pub use random_i64 as random_i64_pub;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use PeerRef;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use LibSqlBackend;
pub use SqliteBackend;
pub use ;
pub use Socks5Config;
pub use ChannelKind;
pub use ;
pub use TypingGuard;
pub use ;
pub use ;
pub use ;
pub use ;
/// Re-export of `ferogram_tl_types`.
pub use ferogram_tl_types as tl;
/// Re-export of [`ferogram_mtproto`].
pub use ferogram_mtproto as mtproto;
/// Re-export of [`ferogram_crypto`].
pub use ferogram_crypto as crypto;
pub use ferogram_tl_parser as parser;
pub use ferogram_tl_gen as codegen;
pub use AuthKey;
pub use ;
pub use ;