Skip to main content

rustigram_bot/
bot.rs

1use crate::dispatcher::DispatcherBuilder;
2use crate::error::BotResult;
3use rustigram_api::{BotClient, ClientConfig};
4
5/// Entry point for building and running a Telegram bot.
6///
7/// `Bot` owns the [`BotClient`] and provides a convenient starting point
8/// for setting up the dispatcher.
9pub struct Bot {
10    /// The underlying API client.
11    pub client: BotClient,
12}
13
14impl Bot {
15    /// Creates a `Bot` from a bot token.
16    ///
17    /// # Errors
18    /// Returns `Err` if the token format is invalid or the HTTP client fails to build.
19    pub fn new(token: impl Into<String>) -> BotResult<Self> {
20        let client = BotClient::from_token(token).map_err(crate::error::BotError::Api)?;
21        Ok(Self { client })
22    }
23
24    /// Creates a `Bot` from a [`ClientConfig`] for advanced configuration.
25    ///
26    /// # Errors
27    /// Returns `Err` if the HTTP client fails to build.
28    pub fn from_config(config: ClientConfig) -> BotResult<Self> {
29        let client = BotClient::new(config).map_err(crate::error::BotError::Api)?;
30        Ok(Self { client })
31    }
32
33    /// Returns a [`DispatcherBuilder`] pre-configured with this bot's client.
34    pub fn dispatcher(&self) -> DispatcherBuilder {
35        crate::dispatcher::Dispatcher::builder(self.client.clone())
36    }
37}