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
use std::fmt::Debug;
use std::future::IntoFuture;
use std::sync::atomic::AtomicI64;
use std::time::Duration;

use crate::client::ApiClient;
use crate::entities::misc::input_file::GetFiles;
use crate::entities::update::{AllowedUpdates, Update};
use crate::errors::{Error, TgApiError};
use crate::methods::edit_message_caption::EditMessageCaptionRequest;
use crate::methods::edit_message_text::EditMessageTextRequest;
use crate::methods::send_animation::SendAnimationRequest;
use crate::methods::send_audio::SendAudioRequest;
use crate::methods::send_contact::SendContactRequest;
use crate::methods::send_dice::SendDiceRequest;
use crate::methods::send_document::SendDocumentRequest;
use crate::methods::send_game::SendGameRequest;
use crate::methods::send_invoice::SendInvoiceRequest;
use crate::methods::send_location::SendLocationRequest;
use crate::methods::send_media_group::SendMediaGroupRequest;
use crate::methods::send_message::SendMessageRequest;
use crate::methods::send_photo::SendPhotoRequest;
use crate::methods::send_poll::SendPollRequest;
use crate::methods::send_sticker::SendStickerRequest;
use crate::methods::send_venue::SendVenueRequest;
use crate::methods::send_video::SendVideoRequest;
use crate::methods::send_video_note::SendVideoNoteRequest;
use crate::methods::send_voice::SendVoiceRequest;
use crate::request::RequestT;
use crate::server_config::ApiServerConfig;

use serde::de::DeserializeOwned;
use serde::Serialize;

macro_rules! set_default_param {
    ($api_client: expr, $param_name: literal, $value: ident, [$($request: ty),*]) => {
        $(
            $api_client.set_default_request_param(
                <$request>::get_name(),
                $param_name,
                $value.clone()
            )?;
        )*
    }
}

#[derive(Clone)]
pub struct APIConfig {
    pub token: String,
    pub server_config: ApiServerConfig,
}

impl APIConfig {
    pub fn new(bot_token: impl ToString, server_config: Option<ApiServerConfig>) -> Self {
        Self {
            token: bot_token.to_string(),
            server_config: server_config.unwrap_or(ApiServerConfig::remote(false)),
        }
    }

    pub fn remote(bot_token: impl ToString, use_test_env: bool) -> Self {
        Self::new(bot_token, Some(ApiServerConfig::remote(use_test_env)))
    }

    pub fn local(
        bot_token: impl ToString,
        server_url: impl Into<String>,
        port: impl Into<u16>,
        use_test_env: bool,
    ) -> Self {
        Self::new(
            bot_token,
            Some(ApiServerConfig::new(
                server_url.into(),
                port.into(),
                use_test_env,
            )),
        )
    }
}

pub struct API {
    config: APIConfig,
    api_client: ApiClient,

    allowed_updates: Vec<String>,
    get_updates_offset: AtomicI64,
}

impl Debug for API {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut token_splits = self.config.token.split(':');
        f.debug_struct("API")
            .field("bot_id", &token_splits.next().unwrap_or("Unknown"))
            .field(
                "token",
                &format!("{}...", &token_splits.next().unwrap_or("Unknown")[..10]),
            )
            .finish()
    }
}

impl API {
    pub fn new(config: APIConfig) -> Self {
        Self {
            config: config.clone(),
            api_client: ApiClient::new(config),

            allowed_updates: vec![],
            get_updates_offset: AtomicI64::new(0),
        }
    }

    /// Sets `allow_sending_without_reply` to `true` for all requests
    pub fn set_essential_request_defaults(&mut self) -> Result<(), Error> {
        set_default_param!(
            self.api_client,
            "allow_sending_without_reply",
            true,
            [
                SendMessageRequest,
                SendPhotoRequest,
                SendAnimationRequest,
                SendAudioRequest,
                SendDocumentRequest,
                SendVoiceRequest,
                SendContactRequest,
                SendDiceRequest,
                SendGameRequest,
                SendInvoiceRequest,
                SendStickerRequest,
                SendVenueRequest,
                SendVideoRequest,
                SendLocationRequest,
                SendVideoNoteRequest,
                SendMediaGroupRequest
            ]
        );

        Ok(())
    }

    /// Set default value for particular request
    pub fn set_default_request_param(
        &mut self,
        method: impl Into<String>,
        param_name: impl Into<String>,
        value: impl Serialize,
    ) -> Result<(), Error> {
        self.api_client
            .set_default_request_param(method.into(), param_name, value)
    }

    /// Sets default `parse_mode` for applicable requests
    pub fn set_parse_mode(&mut self, value: impl Into<String>) -> Result<(), Error> {
        let value = value.into();

        set_default_param!(
            self.api_client,
            "parse_mode",
            value,
            [
                SendMessageRequest,
                SendPhotoRequest,
                SendAnimationRequest,
                SendAudioRequest,
                SendDocumentRequest,
                SendVoiceRequest,
                EditMessageTextRequest,
                EditMessageCaptionRequest
            ]
        );

        self.api_client.set_default_request_param(
            SendPollRequest::get_name(),
            "explanation_parse_mode",
            value,
        )?;

        Ok(())
    }

    pub async fn method_json<
        ReturnType: DeserializeOwned + std::fmt::Debug,
        Params: Serialize + std::fmt::Debug,
    >(
        &self,
        method: &str,
        params: Option<&Params>,
    ) -> Result<ReturnType, Error> {
        self.api_client.method_json(method, params).await
    }

    pub async fn method_multipart_form<
        ReturnType: DeserializeOwned + std::fmt::Debug,
        Params: Serialize + GetFiles + std::fmt::Debug,
    >(
        &self,
        method: &str,
        params: Option<&Params>,
    ) -> Result<ReturnType, Error> {
        self.api_client.method_multipart_form(method, params).await
    }

    /// Set allowed update kinds list which will be later used in polling
    pub fn set_allowed_updates(&mut self, allowed_updates: Vec<AllowedUpdates>) {
        self.allowed_updates = allowed_updates.iter().map(|x| x.to_string()).collect();
    }

    /// Poll the server for pending updates
    pub async fn poll_once(&self) -> Result<Vec<Update>, Error> {
        let offset = self
            .get_updates_offset
            .load(std::sync::atomic::Ordering::Relaxed);
        let r = self
            .get_updates()
            .allowed_updates(self.allowed_updates.clone())
            .offset(offset)
            .timeout(600);

        let updates = r.await?;
        if let Some(last_update) = updates.iter().max_by_key(|update| update.update_id) {
            self.get_updates_offset.store(
                last_update.update_id + 1,
                std::sync::atomic::Ordering::Relaxed,
            );
        }
        Ok(updates)
    }

    /// Same as [`API::request_ref`] but will consume the request
    pub async fn request<Request, ReturnType>(request: Request) -> Result<ReturnType, Error>
    where
        for<'a> &'a Request: IntoFuture<Output = Result<ReturnType, Error>>,
    {
        Self::request_ref(&request).await
    }

    /// This will make API request and automativally handle some common errors like RetryAfter or BadGateway
    pub async fn request_ref<Request, ReturnType>(request: &Request) -> Result<ReturnType, Error>
    where
        for<'a> &'a Request: IntoFuture<Output = Result<ReturnType, Error>>,
    {
        let mut result = request.await;

        let mut wait_for = 1;

        while !match &result {
            Err(Error::ApiError(TgApiError::RetryAfter(wait_for))) => {
                tokio::time::sleep(Duration::from_secs(*wait_for as u64)).await;
                result = request.await;
                false
            }
            Err(Error::ApiError(TgApiError::BadGateway))
            | Err(Error::ApiError(TgApiError::GatewayTimeout)) => {
                wait_for = std::cmp::min(wait_for * 2, 60);
                tokio::time::sleep(Duration::from_secs(wait_for)).await;
                result = request.await;
                false
            }
            _ => true,
        } {}

        result
    }
}