Skip to main content

rustigram_api/methods/
updates.rs

1use std::future::{Future, IntoFuture};
2use std::pin::Pin;
3
4use serde::Serialize;
5
6use rustigram_types::update::Update;
7use rustigram_types::webhook::WebhookInfo;
8
9use crate::client::BotClient;
10use crate::error::Result;
11
12// ─── getUpdates ───────────────────────────────────────────────────────────────
13
14#[derive(Serialize, Default)]
15/// Parameters sent with a `getUpdates` request.
16pub struct GetUpdatesParams {
17    /// Identifier of the first update to return. Confirms all updates before this ID.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub offset: Option<i64>,
20    /// Maximum number of updates to return (1–100).
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub limit: Option<u8>,
23    /// Timeout in seconds for long polling. `0` for short polling.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub timeout: Option<u32>,
26    /// List of update types to receive. All types received if omitted.
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub allowed_updates: Option<Vec<String>>,
29}
30
31/// Builder for the [`getUpdates`](https://core.telegram.org/bots/api#getupdates) method.
32pub struct GetUpdates {
33    client: BotClient,
34    params: GetUpdatesParams,
35}
36
37impl GetUpdates {
38    pub(crate) fn new(client: BotClient) -> Self {
39        Self {
40            client,
41            params: GetUpdatesParams::default(),
42        }
43    }
44
45    /// Sets the update offset — all updates with `update_id < offset` are
46    /// acknowledged and will not be returned again.
47    pub fn offset(mut self, offset: i64) -> Self {
48        self.params.offset = Some(offset);
49        self
50    }
51
52    /// Limits the number of updates returned (1–100, default 100).
53    pub fn limit(mut self, limit: u8) -> Self {
54        self.params.limit = Some(limit.clamp(1, 100));
55        self
56    }
57
58    /// Sets the long-poll server-side timeout in seconds. Use 0 for short polling.
59    pub fn timeout(mut self, secs: u32) -> Self {
60        self.params.timeout = Some(secs);
61        self
62    }
63
64    /// Restricts which update types are returned (e.g. `["message", "callback_query"]`).
65    pub fn allowed_updates(mut self, types: Vec<impl Into<String>>) -> Self {
66        self.params.allowed_updates = Some(types.into_iter().map(Into::into).collect());
67        self
68    }
69}
70
71impl IntoFuture for GetUpdates {
72    type Output = Result<Vec<Update>>;
73    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
74
75    fn into_future(self) -> Self::IntoFuture {
76        Box::pin(async move { self.client.post_json("getUpdates", &self.params).await })
77    }
78}
79
80// ─── setWebhook ───────────────────────────────────────────────────────────────
81
82/// Parameters for a `setWebhook` request.
83#[derive(Serialize)]
84pub struct SetWebhookParams {
85    url: String,
86    #[serde(skip_serializing_if = "Option::is_none")]
87    ip_address: Option<String>,
88    #[serde(skip_serializing_if = "Option::is_none")]
89    max_connections: Option<u8>,
90    #[serde(skip_serializing_if = "Option::is_none")]
91    allowed_updates: Option<Vec<String>>,
92    #[serde(skip_serializing_if = "Option::is_none")]
93    drop_pending_updates: Option<bool>,
94    #[serde(skip_serializing_if = "Option::is_none")]
95    secret_token: Option<String>,
96}
97
98/// Builder for the [`setWebhook`](https://core.telegram.org/bots/api#setwebhook) method.
99pub struct SetWebhook {
100    client: BotClient,
101    params: SetWebhookParams,
102}
103
104impl SetWebhook {
105    pub(crate) fn new(client: BotClient, url: impl Into<String>) -> Self {
106        Self {
107            client,
108            params: SetWebhookParams {
109                url: url.into(),
110                ip_address: None,
111                max_connections: None,
112                allowed_updates: None,
113                drop_pending_updates: None,
114                secret_token: None,
115            },
116        }
117    }
118    /// Overrides the resolved IP address of the webhook server.
119    pub fn ip_address(mut self, ip: impl Into<String>) -> Self {
120        self.params.ip_address = Some(ip.into());
121        self
122    }
123    /// Sets the maximum number of concurrent HTTPS connections (1–100, default 40).
124    pub fn max_connections(mut self, n: u8) -> Self {
125        self.params.max_connections = Some(n.clamp(1, 100));
126        self
127    }
128    /// List of update types to receive. All types received if omitted.
129    pub fn allowed_updates(mut self, types: Vec<impl Into<String>>) -> Self {
130        self.params.allowed_updates = Some(types.into_iter().map(Into::into).collect());
131        self
132    }
133    /// If `true`, the webhook server will remove all pending updates.
134    pub fn drop_pending_updates(mut self, v: bool) -> Self {
135        self.params.drop_pending_updates = Some(v);
136        self
137    }
138    /// Sets the secret token Telegram sends in `X-Telegram-Bot-Api-Secret-Token`
139    /// on every webhook request.
140    pub fn secret_token(mut self, token: impl Into<String>) -> Self {
141        self.params.secret_token = Some(token.into());
142        self
143    }
144}
145
146impl IntoFuture for SetWebhook {
147    type Output = Result<bool>;
148    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
149    fn into_future(self) -> Self::IntoFuture {
150        Box::pin(async move { self.client.post_json("setWebhook", &self.params).await })
151    }
152}
153
154// ─── deleteWebhook ────────────────────────────────────────────────────────────
155
156#[derive(Serialize, Default)]
157struct DeleteWebhookParams {
158    #[serde(skip_serializing_if = "Option::is_none")]
159    drop_pending_updates: Option<bool>,
160}
161
162/// Builder for the [`deleteWebhook`](https://core.telegram.org/bots/api#deletewebhook) method.
163pub struct DeleteWebhook {
164    client: BotClient,
165    params: DeleteWebhookParams,
166}
167
168impl DeleteWebhook {
169    pub(crate) fn new(client: BotClient) -> Self {
170        Self {
171            client,
172            params: DeleteWebhookParams::default(),
173        }
174    }
175    /// If `true`, the webhook server will remove all pending updates.
176    pub fn drop_pending_updates(mut self, v: bool) -> Self {
177        self.params.drop_pending_updates = Some(v);
178        self
179    }
180}
181
182impl IntoFuture for DeleteWebhook {
183    type Output = Result<bool>;
184    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
185    fn into_future(self) -> Self::IntoFuture {
186        Box::pin(async move { self.client.post_json("deleteWebhook", &self.params).await })
187    }
188}
189
190// ─── getWebhookInfo ───────────────────────────────────────────────────────────
191
192/// Builder for the [`getWebhookInfo`](https://core.telegram.org/bots/api#getwebhookinfo) method.
193pub struct GetWebhookInfo {
194    client: BotClient,
195}
196
197impl GetWebhookInfo {
198    pub(crate) fn new(client: BotClient) -> Self {
199        Self { client }
200    }
201}
202
203impl IntoFuture for GetWebhookInfo {
204    type Output = Result<WebhookInfo>;
205    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
206    fn into_future(self) -> Self::IntoFuture {
207        Box::pin(async move {
208            self.client
209                .post_json("getWebhookInfo", &serde_json::json!({}))
210                .await
211        })
212    }
213}