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
use crate::types::ParseMode;
use reqwest::{
    header::{HeaderMap, CONNECTION},
    Client, ClientBuilder,
};
use std::{sync::Arc, time::Duration};

mod api;
mod download;

pub(crate) const TELOXIDE_TOKEN: &str = "TELOXIDE_TOKEN";
pub(crate) const TELOXIDE_PROXY: &str = "TELOXIDE_PROXY";

/// A requests sender.
///
/// No need to put it into [`Arc`], because it's already in.
///
/// [`Arc`]: std::sync::Arc
#[derive(Debug, Clone)]
pub struct Bot {
    token: Arc<str>,
    client: Client,
    parse_mode: Arc<Option<ParseMode>>,
}

impl Bot {
    /// Creates new [`BotBuilder`] see it's [docs] for more
    ///
    /// [docs]: BotBuilder
    #[must_use]
    pub fn builder() -> BotBuilder {
        BotBuilder::new()
    }

    /// Creates a new `Bot` with the `TELOXIDE_TOKEN` & `TELOXIDE_PROXY`
    /// environmental variables (a bot's token & a proxy) and the default
    /// [`reqwest::Client`].
    ///
    /// This function passes the value of `TELOXIDE_PROXY` into
    /// [`reqwest::Proxy::all`], if it exists, otherwise returns the default
    /// client.
    ///
    /// # Panics
    ///  - If cannot get the `TELOXIDE_TOKEN` and `TELOXIDE_PROXY` environmental
    ///    variables.
    ///  - If it cannot create [`reqwest::Client`].
    ///
    /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html
    /// [`reqwest::Proxy::all`]: https://docs.rs/reqwest/latest/reqwest/struct.Proxy.html#method.all
    #[must_use]
    pub fn from_env() -> Self {
        BotBuilder::new().build()
    }

    /// Creates a new `Bot` with the `TELOXIDE_TOKEN` environmental variable (a
    /// bot's token) and your [`reqwest::Client`].
    ///
    /// # Panics
    /// If cannot get the `TELOXIDE_TOKEN` environmental variable.
    ///
    /// # Caution
    /// Your custom client might not be configured correctly to be able to work
    /// in long time durations, see [issue 223].
    ///
    /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html
    /// [issue 223]: https://github.com/teloxide/teloxide/issues/223
    #[deprecated = "Deprecated in favour of BotBuilder because the later provides more options \
                    (notably default parse_mode)"]
    pub fn from_env_with_client(client: Client) -> Self {
        #[allow(deprecated)]
        Self::with_client(&get_env(TELOXIDE_TOKEN), client)
    }

    /// Creates a new `Bot` with the specified token and the default
    /// [`reqwest::Client`].
    ///
    /// # Panics
    /// If it cannot create [`reqwest::Client`].
    ///
    /// [`reqwest::Client`]: https://docs.rs/reqwest/latest/reqwest/struct.Client.html
    #[deprecated = "Deprecated in favour of BotBuilder because the later provides more options \
                    (notably default parse_mode)"]
    pub fn new<S>(token: S) -> Self
    where
        S: Into<String>,
    {
        #[allow(deprecated)]
        Self::with_client(token, build_sound_bot())
    }

    /// Creates a new `Bot` with the specified token and your
    /// [`reqwest::Client`].
    ///
    /// # Caution
    /// Your custom client might not be configured correctly to be able to work
    /// in long time durations, see [issue 223].
    ///
    /// [`reqwest::Client`]: https://docs.rs/reqwest/latest/reqwest/struct.Client.html
    /// [issue 223]: https://github.com/teloxide/teloxide/issues/223
    #[deprecated = "Deprecated in favour of BotBuilder because the later provides more options \
                    (notably default parse_mode)"]
    pub fn with_client<S>(token: S, client: Client) -> Self
    where
        S: Into<String>,
    {
        Self {
            token: Into::<Arc<str>>::into(Into::<String>::into(token)),
            client,
            parse_mode: Arc::new(None),
        }
    }
}

/// Returns a builder with safe settings.
///
/// By "safe settings" I mean that a client will be able to work in long time
/// durations, see the [issue 223].
///
/// [issue 223]: https://github.com/teloxide/teloxide/issues/223
pub(crate) fn sound_bot() -> ClientBuilder {
    let mut headers = HeaderMap::new();
    headers.insert(CONNECTION, "keep-alive".parse().unwrap());

    let connect_timeout = Duration::from_secs(5);
    let timeout = 10;

    ClientBuilder::new()
        .connect_timeout(connect_timeout)
        .timeout(Duration::from_secs(connect_timeout.as_secs() + timeout + 2))
        .tcp_nodelay_(true)
        .default_headers(headers)
}

pub(crate) fn build_sound_bot() -> Client {
    sound_bot().build().expect("creating reqwest::Client")
}

fn get_env(env: &'static str) -> String {
    std::env::var(env).unwrap_or_else(|_| panic!("Cannot get the {} env variable", env))
}

impl Bot {
    // TODO: const fn
    pub fn token(&self) -> &str {
        &self.token
    }

    // TODO: const fn
    pub fn client(&self) -> &Client {
        &self.client
    }
}

/// A builder of [`Bot`], supporting some extra settings.
///
/// [`Bot`]: crate::Bot
#[derive(Debug, Default)]
pub struct BotBuilder {
    token: Option<String>,
    client: Option<Client>,
    parse_mode: Option<ParseMode>,
}

impl BotBuilder {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Specifies a custom HTTPS client. Otherwise, the default will be used.
    ///
    /// # Caution
    ///  - Your custom client might not be configured correctly to be able to
    ///    work
    /// in long time durations, see [issue 223].
    ///
    ///  - If this method is used, the `TELOXIDE_PROXY` environmental variable
    ///    won't be extracted in [`BotBuilder::build`].
    ///
    /// [issue 223]: https://github.com/teloxide/teloxide/issues/223
    /// [`BotBuilder::build`]: crate::BotBuilder::build
    #[must_use]
    pub fn client(mut self, client: Client) -> Self {
        self.client = Some(client);
        self
    }

    /// Specified a custom token.
    ///
    /// Otherwise, a token will be extracted from the `TELOXIDE_TOKEN`
    /// environmental variable.
    #[must_use]
    pub fn token<S>(mut self, token: S) -> Self
    where
        S: Into<String>,
    {
        self.token = Some(token.into());
        self
    }

    /// Specifies [`ParseMode`], which will be used during all calls to:
    ///
    ///  - [`send_message`]
    ///  - [`send_photo`]
    ///  - [`send_video`]
    ///  - [`send_audio`]
    ///  - [`send_document`]
    ///  - [`send_animation`]
    ///  - [`send_voice`]
    ///  - [`send_poll`]
    ///  - [`edit_message_text`]
    ///  - [`edit_message_caption`]
    ///
    /// [`send_message`]: crate::Bot::send_message
    /// [`send_photo`]: crate::Bot::send_photo
    /// [`send_video`]: crate::Bot::send_video
    /// [`send_audio`]: crate::Bot::send_audio
    /// [`send_document`]: crate::Bot::send_document
    /// [`send_animation`]: crate::Bot::send_animation
    /// [`send_voice`]: crate::Bot::send_voice
    /// [`send_poll`]: crate::Bot::send_poll
    /// [`edit_message_text`]: crate::Bot::edit_message_text
    /// [`edit_message_caption`]: crate::Bot::edit_message_caption
    #[must_use]
    pub fn parse_mode(mut self, parse_mode: ParseMode) -> Self {
        self.parse_mode = Some(parse_mode);
        self
    }

    /// Builds [`Bot`].
    ///
    /// This method will attempt to build a new client with a proxy, specified
    /// in the `TELOXIDE_PROXY` (passed into [`reqwest::Proxy::all`])
    /// environmental variable, if a client haven't been specified.
    ///
    /// # Panics
    ///  - If cannot get the `TELOXIDE_TOKEN` and `TELOXIDE_PROXY` environmental
    ///    variables.
    ///  - If it cannot create [`reqwest::Client`].
    ///
    /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html
    ///
    /// [`Bot`]: crate::Bot
    /// [`reqwest::Proxy::all`]: https://docs.rs/reqwest/latest/reqwest/struct.Proxy.html#method.all
    #[must_use]
    pub fn build(self) -> Bot {
        Bot {
            client: self.client.unwrap_or_else(crate::utils::client_from_env),
            token: self.token.unwrap_or_else(|| get_env(TELOXIDE_TOKEN)).into(),
            parse_mode: Arc::new(self.parse_mode),
        }
    }
}