ferobot 0.1.2

A fully-featured, auto-generated Telegram Bot API library for Rust. All 285 types and 165 methods - strongly typed, fully async.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// ferobot: async Telegram Bot API framework written in Rust
// Repository: https://github.com/ankit-chaubey/ferobot
//
// Ferobot provides a fast and ergonomic framework for building Telegram bots
// using the official Telegram Bot API.
//
// Author: Ankit Chaubey
//
// If you use or modify this code, keep this notice at the top of your file
// and include the LICENSE-MIT or LICENSE-APACHE file from this repository.

use std::sync::Arc;

use serde::Deserialize;

use crate::{
    client::{BotClient, FormPart, ReqwestClient},
    input_file::{InputFile, InputFileOrString},
    types::User,
    BotError,
};

fn infer_mime(filename: &str) -> String {
    let ext = filename.rsplit('.').next().unwrap_or("").to_lowercase();
    match ext.as_str() {
        "jpg" | "jpeg" => "image/jpeg",
        "png" => "image/png",
        "gif" => "image/gif",
        "webp" => "image/webp",
        "mp4" => "video/mp4",
        "mp3" => "audio/mpeg",
        "ogg" => "audio/ogg",
        "pdf" => "application/pdf",
        "webm" => "video/webm",
        _ => "application/octet-stream",
    }
    .to_string()
}

const DEFAULT_API_URL: &str = "https://api.telegram.org";

/// The main Bot struct. Create one per bot token.
///
/// # Example
/// ```rust,no_run
/// # use ferobot::Bot;
/// # #[tokio::main]
/// # async fn main() {
/// let bot = Bot::new("YOUR_TOKEN").await.unwrap();
/// println!("Running as @{}", bot.me.username.as_deref().unwrap_or(""));
/// # }
/// ```
///
/// # Debug output
///
/// The `Debug` implementation **masks the token** to avoid accidental leaks
/// in logs: `token: "12345:****"`. Use [`Bot::token()`] if you genuinely
/// need the full string.
#[derive(Clone)]
pub struct Bot {
    token: String,
    /// Bot info populated via `getMe` on creation.
    pub me: User,
    /// API base URL (default: `https://api.telegram.org`).
    pub api_url: String,
    pub(crate) base: String,
    /// Pluggable HTTP back-end. Defaults to [`ReqwestClient`].
    pub(crate) client: Arc<dyn BotClient>,
}

impl std::fmt::Debug for Bot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let masked = match self.token.find(':') {
            Some(idx) => format!("{}:****", &self.token[..idx]),
            None => "****".to_string(),
        };
        f.debug_struct("Bot")
            .field("token", &masked)
            .field("me", &self.me)
            .field("api_url", &self.api_url)
            .finish()
    }
}

#[derive(Debug, Deserialize)]
struct TelegramResponse<T> {
    ok: bool,
    result: Option<T>,
    error_code: Option<i64>,
    description: Option<String>,
    parameters: Option<ResponseParameters>,
}

#[derive(Debug, Deserialize)]
struct ResponseParameters {
    migrate_to_chat_id: Option<i64>,
    retry_after: Option<i64>,
}

fn parse_bot_id(token: &str) -> Result<i64, BotError> {
    token
        .split(':')
        .next()
        .and_then(|s| s.parse::<i64>().ok())
        .ok_or(BotError::InvalidToken)
}

fn stub_user(id: i64) -> User {
    User {
        id,
        is_bot: true,
        first_name: String::new(),
        last_name: None,
        username: None,
        language_code: None,
        is_premium: None,
        added_to_attachment_menu: None,
        can_join_groups: None,
        can_read_all_group_messages: None,
        supports_guest_queries: None,
        supports_inline_queries: None,
        can_connect_to_business: None,
        has_main_web_app: None,
        has_topics_enabled: None,
        allows_users_to_create_topics: None,
        can_manage_bots: None,
    }
}

impl Bot {
    /// Returns the bot token.
    ///
    /// The token is kept private to avoid accidental `Debug` leaks. Access it
    /// through this method when you genuinely need the raw string (e.g. to
    /// build a webhook URL or call a raw HTTP endpoint).
    pub fn token(&self) -> &str {
        &self.token
    }

    /// Create a new Bot and verify the token by calling `getMe`.
    pub async fn new(token: impl Into<String>) -> Result<Self, BotError> {
        Self::with_timeout(token, DEFAULT_API_URL, std::time::Duration::from_secs(30)).await
    }

    /// Create a Bot pointing at a custom API server. Calls `getMe` on creation.
    pub async fn with_api_url(
        token: impl Into<String>,
        api_url: impl Into<String>,
    ) -> Result<Self, BotError> {
        Self::with_timeout(token, api_url, std::time::Duration::from_secs(30)).await
    }

    /// Create a Bot with a custom HTTP timeout and API URL. Calls `getMe`.
    pub async fn with_timeout(
        token: impl Into<String>,
        api_url: impl Into<String>,
        timeout: std::time::Duration,
    ) -> Result<Self, BotError> {
        let token = token.into();
        let api_url = api_url.into();

        if !token.contains(':') {
            return Err(BotError::InvalidToken);
        }

        let bot_id = parse_bot_id(&token)?;
        let client = ReqwestClient::with_timeout(timeout)?;

        let base = format!("{}/bot{}/", api_url, token);
        let mut bot = Bot {
            token,
            me: stub_user(bot_id),
            api_url,
            base,
            client: Arc::new(client),
        };

        bot.me = bot.call_api("getMe", serde_json::json!({})).await?;
        Ok(bot)
    }

    /// Create a Bot with a **custom HTTP client** implementing [`BotClient`].
    ///
    /// The bot ID is parsed from the token so `bot.me.id` is valid.
    /// Call `getMe` yourself if you need the rest of the `me` fields.
    pub fn with_client(
        token: impl Into<String>,
        api_url: impl Into<String>,
        client: impl BotClient + 'static,
    ) -> Result<Self, BotError> {
        let token = token.into();
        if !token.contains(':') {
            return Err(BotError::InvalidToken);
        }
        let bot_id = parse_bot_id(&token)?;
        let api_url = api_url.into();
        let base = format!("{}/bot{}/", api_url, token);
        Ok(Bot {
            token,
            me: stub_user(bot_id),
            api_url,
            base,
            client: Arc::new(client),
        })
    }

    /// Create a Bot **without** calling `getMe` (no network on startup).
    ///
    /// The bot ID is parsed from the token so `bot.me.id` is always valid.
    /// All other `me` fields are left as zero-values until you call `getMe`.
    pub fn new_unverified(token: impl Into<String>) -> Result<Self, BotError> {
        let token = token.into();
        let bot_id = parse_bot_id(&token)?;
        let client = ReqwestClient::with_timeout(std::time::Duration::from_secs(30))
            .expect("default client should build");
        let api_url = DEFAULT_API_URL.to_string();
        let base = format!("{}/bot{}/", api_url, token);
        Ok(Bot {
            token,
            me: stub_user(bot_id),
            api_url,
            base,
            client: Arc::new(client),
        })
    }

    /// Build a `Bot` configured for outbound API calls (sendMessage, etc.).
    ///
    /// Call [`Bot::new`] first to verify the token, then swap the transport:
    ///
    /// ```rust,no_run
    /// # async fn example() -> Result<(), ferobot::BotError> {
    /// let verified = ferobot::Bot::new("TOKEN").await?;
    /// let api_bot = verified.into_api_bot()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn into_api_bot(self) -> Result<Self, BotError> {
        Ok(Bot {
            client: Arc::new(ReqwestClient::for_api()?),
            ..self
        })
    }

    /// Build a `Bot` configured for long-polling (`getUpdates`).
    ///
    /// Pass this to [`Poller::new`] and a separate `into_api_bot()` to
    /// [`Poller::api_bot`] so handler calls use a different connection pool.
    ///
    /// ```rust,no_run
    /// # async fn example() -> Result<(), ferobot::BotError> {
    /// let token = "TOKEN";
    /// let verified  = ferobot::Bot::new(token).await?;
    /// let poll_bot  = verified.clone().into_polling_bot()?;
    /// let api_bot   = verified.into_api_bot()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn into_polling_bot(self) -> Result<Self, BotError> {
        Ok(Bot {
            client: Arc::new(ReqwestClient::for_polling()?),
            ..self
        })
    }

    /// Build the full endpoint URL for a Telegram method name.
    pub fn endpoint(&self, method: &str) -> String {
        format!("{}{}", self.base, method)
    }

    /// Make a JSON API call and deserialize the result.
    pub async fn call_api<T>(&self, method: &str, body: serde_json::Value) -> Result<T, BotError>
    where
        T: for<'de> Deserialize<'de>,
    {
        let url = self.endpoint(method);
        let bytes = self.client.post_json(&url, body).await?;
        let tg: TelegramResponse<T> = serde_json::from_slice(&bytes)?;
        self.unwrap_response(tg)
    }

    /// Make an API call using multipart when a `Memory` file is present,
    /// JSON otherwise.
    pub async fn call_api_with_file<T>(
        &self,
        method: &str,
        body: serde_json::Map<String, serde_json::Value>,
        file_field: &str,
        file: InputFileOrString,
    ) -> Result<T, BotError>
    where
        T: for<'de> Deserialize<'de>,
    {
        match file {
            InputFileOrString::File(InputFile::Memory { filename, data }) => {
                let mime = infer_mime(&filename);

                let mut parts: Vec<FormPart> = body
                    .into_iter()
                    .filter(|(_, v)| !v.is_null())
                    .map(|(k, v)| {
                        let text = match &v {
                            serde_json::Value::String(s) => s.clone(),
                            other => other.to_string(),
                        };
                        FormPart::text(k, text)
                    })
                    .collect();

                parts.push(FormPart::bytes(file_field, filename, mime, data));
                self.call_api_multipart(method, parts).await
            }
            other => {
                let mut req = body;
                req.insert(
                    file_field.into(),
                    serde_json::to_value(other).unwrap_or_default(),
                );
                self.call_api(method, serde_json::Value::Object(req)).await
            }
        }
    }

    pub async fn call_api_with_two_files<T>(
        &self,
        method: &str,
        body: serde_json::Map<String, serde_json::Value>,
        field_a: &str,
        file_a: InputFileOrString,
        field_b: &str,
        file_b: InputFileOrString,
    ) -> Result<T, BotError>
    where
        T: for<'de> Deserialize<'de>,
    {
        let a_is_upload = matches!(file_a, InputFileOrString::File(InputFile::Memory { .. }));
        let b_is_upload = matches!(file_b, InputFileOrString::File(InputFile::Memory { .. }));

        if a_is_upload || b_is_upload {
            let mut parts: Vec<FormPart> = body
                .into_iter()
                .filter(|(_, v)| !v.is_null())
                .map(|(k, v)| {
                    let text = match &v {
                        serde_json::Value::String(s) => s.clone(),
                        other => other.to_string(),
                    };
                    FormPart::text(k, text)
                })
                .collect();

            match file_a {
                InputFileOrString::File(InputFile::Memory { filename, data }) => {
                    let mime = infer_mime(&filename);
                    parts.push(FormPart::bytes(field_a, filename, mime, data));
                }
                other => {
                    parts.push(FormPart::text(
                        field_a,
                        serde_json::to_value(other).unwrap_or_default().to_string(),
                    ));
                }
            }

            match file_b {
                InputFileOrString::File(InputFile::Memory { filename, data }) => {
                    let mime = infer_mime(&filename);
                    parts.push(FormPart::bytes(field_b, filename, mime, data));
                }
                other => {
                    parts.push(FormPart::text(
                        field_b,
                        serde_json::to_value(other).unwrap_or_default().to_string(),
                    ));
                }
            }

            self.call_api_multipart(method, parts).await
        } else {
            let mut req = body;
            req.insert(
                field_a.into(),
                serde_json::to_value(file_a).unwrap_or_default(),
            );
            req.insert(
                field_b.into(),
                serde_json::to_value(file_b).unwrap_or_default(),
            );
            self.call_api(method, serde_json::Value::Object(req)).await
        }
    }

    /// Make a `multipart/form-data` API call directly from [`FormPart`]s.
    pub async fn call_api_multipart<T>(
        &self,
        method: &str,
        parts: Vec<FormPart>,
    ) -> Result<T, BotError>
    where
        T: for<'de> Deserialize<'de>,
    {
        let url = self.endpoint(method);
        let bytes = self.client.post_form(&url, parts).await?;
        let tg: TelegramResponse<T> = serde_json::from_slice(&bytes)?;
        self.unwrap_response(tg)
    }

    fn unwrap_response<T>(&self, tg: TelegramResponse<T>) -> Result<T, BotError> {
        if tg.ok {
            tg.result
                .ok_or_else(|| BotError::Other("ok=true but result is null".into()))
        } else {
            Err(BotError::Api {
                code: tg.error_code.unwrap_or(0),
                description: tg.description.unwrap_or_else(|| "Unknown error".into()),
                retry_after: tg.parameters.as_ref().and_then(|p| p.retry_after),
                migrate_to_chat_id: tg.parameters.as_ref().and_then(|p| p.migrate_to_chat_id),
            })
        }
    }

    /// Call any Telegram Bot API method by name.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use ferobot::Bot;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), ferobot::BotError> {
    /// let bot = Bot::new("TOKEN").await?;
    ///
    /// // sendMessage with any params you like
    /// let msg: serde_json::Value = bot
    ///     .raw("sendMessage")
    ///     .param("chat_id", 123456789_i64)
    ///     .param("text", "Hello from raw! 🦀")
    ///     .param("parse_mode", "HTML")
    ///     .call()
    ///     .await?;
    ///
    /// println!("sent message_id = {}", msg["message_id"]);
    ///
    /// // Fire-and-forget (no result needed)
    /// bot.raw("deleteMessage")
    ///     .param("chat_id", 123456789_i64)
    ///     .param("message_id", msg["message_id"].as_i64().unwrap())
    ///     .send()
    ///     .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn raw(&self, method: impl Into<String>) -> crate::raw::RawRequest<'_> {
        crate::raw::RawRequest::new(self, method)
    }
}