tgbot/types/definitions/webhook.rs
1use std::collections::HashSet;
2
3use serde::{Deserialize, Serialize};
4
5use crate::{
6 api::{Method, Payload},
7 types::{AllowedUpdate, Integer},
8};
9
10/// Represents a current status of a webhook.
11#[serde_with::skip_serializing_none]
12#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
13pub struct WebhookInfo {
14 /// Indicates whether a custom certificate was provided for webhook certificate checks.
15 pub has_custom_certificate: bool,
16 /// Number of updates awaiting delivery.
17 pub pending_update_count: Integer,
18 /// Webhook URL, may be empty if webhook is not set up.
19 pub url: String,
20 /// A list of update types the bot is subscribed to.
21 ///
22 /// Defaults to all update types.
23 pub allowed_updates: Option<Vec<AllowedUpdate>>,
24 /// Currently used webhook IP address.
25 pub ip_address: Option<String>,
26 /// Unix time for the most recent error that happened
27 /// when trying to deliver an update via webhook.
28 pub last_error_date: Option<Integer>,
29 /// Error message in human-readable format for the most recent error
30 /// that happened when trying to deliver an update via webhook.
31 pub last_error_message: Option<String>,
32 /// Unix time of the most recent error that happened
33 /// when trying to synchronize available updates with Telegram datacenters.
34 pub last_synchronization_error_date: Option<Integer>,
35 /// Maximum allowed number of simultaneous HTTPS connections
36 /// to the webhook for update delivery.
37 pub max_connections: Option<Integer>,
38}
39
40impl WebhookInfo {
41 /// Creates a new `WebhookInfo`.
42 ///
43 /// # Arguments
44 ///
45 /// * `url` - Webhook URL.
46 /// * `has_custom_certificate` - Indicates whether a custom certificate was provided.
47 /// * `pending_update_count` - Number of updates awaiting delivery.
48 pub fn new<T>(url: T, has_custom_certificate: bool, pending_update_count: Integer) -> Self
49 where
50 T: Into<String>,
51 {
52 Self {
53 has_custom_certificate,
54 pending_update_count,
55 url: url.into(),
56 allowed_updates: None,
57 ip_address: None,
58 last_error_date: None,
59 last_error_message: None,
60 last_synchronization_error_date: None,
61 max_connections: None,
62 }
63 }
64
65 /// Sets a new list of allowed updates.
66 ///
67 /// # Arguments
68 ///
69 /// * `value` - List of allowed updates.
70 pub fn with_allowed_updates<T>(mut self, value: T) -> Self
71 where
72 T: IntoIterator<Item = AllowedUpdate>,
73 {
74 self.allowed_updates = Some(value.into_iter().collect());
75 self
76 }
77
78 /// Sets a new IP address.
79 ///
80 /// # Arguments
81 ///
82 /// * `value` - IP address.
83 pub fn with_ip_address<T>(mut self, value: T) -> Self
84 where
85 T: Into<String>,
86 {
87 self.ip_address = Some(value.into());
88 self
89 }
90
91 /// Sets a new last error date.
92 ///
93 /// # Arguments
94 ///
95 /// * `value` - Last error date.
96 pub fn with_last_error_date(mut self, value: Integer) -> Self {
97 self.last_error_date = Some(value);
98 self
99 }
100
101 /// Sets a new last error message.
102 ///
103 /// # Arguments
104 ///
105 /// * `value` - Last error message.
106 pub fn with_last_error_message<T>(mut self, value: T) -> Self
107 where
108 T: Into<String>,
109 {
110 self.last_error_message = Some(value.into());
111 self
112 }
113
114 /// Sets a new last synchronization error date.
115 ///
116 /// # Arguments
117 ///
118 /// * `value` - Last synchronization error date.
119 pub fn with_last_synchronization_error_date(mut self, value: Integer) -> Self {
120 self.last_synchronization_error_date = Some(value);
121 self
122 }
123
124 /// Sets a new number of max connections.
125 ///
126 /// # Arguments
127 ///
128 /// * `value` - Number of max connections.
129 pub fn with_max_connections(mut self, value: Integer) -> Self {
130 self.max_connections = Some(value);
131 self
132 }
133}
134
135/// Removes a webhook integration if you decide to switch back to [`crate::types::GetUpdates`].
136#[derive(Clone, Copy, Debug, Default, Serialize)]
137pub struct DeleteWebhook {
138 drop_pending_updates: Option<bool>,
139}
140
141impl DeleteWebhook {
142 /// Sets a new value for the `drop_pending_updates` flag.
143 ///
144 /// # Arguments
145 ///
146 /// * `value` - Indicates whether to drop all pending updates.
147 pub fn with_drop_pending_updates(mut self, value: bool) -> Self {
148 self.drop_pending_updates = Some(value);
149 self
150 }
151}
152
153impl Method for DeleteWebhook {
154 type Response = bool;
155
156 fn into_payload(self) -> Payload {
157 if self.drop_pending_updates.is_some() {
158 Payload::json("deleteWebhook", self)
159 } else {
160 Payload::empty("deleteWebhook")
161 }
162 }
163}
164
165/// Returns current webhook status.
166#[derive(Clone, Copy, Debug)]
167pub struct GetWebhookInfo;
168
169impl Method for GetWebhookInfo {
170 type Response = WebhookInfo;
171
172 fn into_payload(self) -> Payload {
173 Payload::empty("getWebhookInfo")
174 }
175}
176
177/// Specifies an url and returns incoming updates via an outgoing webhook.
178///
179/// Whenever there is an update for the bot, we will send an HTTPS POST request
180/// to the specified url, containing a JSON-serialized Update.
181/// In case of an unsuccessful request, we will give up after a reasonable amount of attempts.
182///
183/// If you'd like to make sure that the Webhook request comes from Telegram,
184/// we recommend using a secret path in the URL, e.g. `https://www.example.com/<token>`
185/// Since nobody else knows your bot‘s token, you can be pretty sure it’s us.
186#[serde_with::skip_serializing_none]
187#[derive(Clone, Debug, Serialize)]
188pub struct SetWebhook {
189 url: String,
190 allowed_updates: Option<HashSet<AllowedUpdate>>,
191 certificate: Option<String>,
192 drop_pending_updates: Option<bool>,
193 ip_address: Option<String>,
194 max_connections: Option<Integer>,
195 secret_token: Option<String>,
196}
197
198impl SetWebhook {
199 /// Creates a new `SetWebhook`.
200 ///
201 /// # Arguments
202 ///
203 /// * `url` - HTTPS url to send updates to; use an empty string to remove webhook integration.
204 pub fn new<T>(url: T) -> Self
205 where
206 T: Into<String>,
207 {
208 Self {
209 url: url.into(),
210 allowed_updates: None,
211 certificate: None,
212 drop_pending_updates: None,
213 ip_address: None,
214 max_connections: None,
215 secret_token: None,
216 }
217 }
218
219 /// Adds a type of update you want your bot to receive.
220 ///
221 /// # Arguments
222 ///
223 /// * `value` - A type to add.
224 pub fn add_allowed_update(mut self, value: AllowedUpdate) -> Self {
225 match self.allowed_updates {
226 Some(ref mut updates) => {
227 updates.insert(value);
228 }
229 None => {
230 let mut updates = HashSet::new();
231 updates.insert(value);
232 self.allowed_updates = Some(updates);
233 }
234 };
235 self
236 }
237
238 /// Sets a new list of allowed update types.
239 ///
240 /// # Arguments
241 ///
242 /// * `value` - List of types you want your bot to receive.
243 ///
244 /// For example, specify `[AllowedUpdate::Message]`
245 /// to only receive updates of these types.
246 /// See [`AllowedUpdate`] for a complete list of available update types.
247 /// Specify an empty list to receive all updates regardless of type (default).
248 /// If not specified, the previous setting will be used.
249 /// Please note that this parameter doesn't affect
250 /// updates created before the call to the [`SetWebhook`],
251 /// so unwanted updates may be received for a short period of time.
252 pub fn with_allowed_updates(mut self, value: HashSet<AllowedUpdate>) -> Self {
253 self.allowed_updates = Some(value);
254 self
255 }
256
257 /// Sets a new certificate.
258 ///
259 /// # Arguments
260 ///
261 /// * `value` - Public key certificate; so that the root certificate in use can be checked.
262 pub fn with_certificate<T>(mut self, value: T) -> Self
263 where
264 T: Into<String>,
265 {
266 self.certificate = Some(value.into());
267 self
268 }
269
270 /// Sets a new value for the `drop_pending_updates` flag.
271 ///
272 /// # Arguments
273 ///
274 /// * `value` - Indicates whether to drop all pending updates.
275 pub fn with_drop_pending_updates(mut self, value: bool) -> Self {
276 self.drop_pending_updates = Some(value);
277 self
278 }
279
280 /// Sets a new IP address.
281 ///
282 /// # Arguments
283 ///
284 /// * `value` - The fixed IP address which will be used to send webhook requests
285 /// instead of the IP address resolved through DNS.
286 pub fn with_ip_address<T>(mut self, value: T) -> Self
287 where
288 T: Into<String>,
289 {
290 self.ip_address = Some(value.into());
291 self
292 }
293
294 /// Sets a new number of max connections.
295 ///
296 /// # Arguments
297 ///
298 /// * `value` - Maximum allowed number of simultaneous HTTPS connections
299 /// to the webhook for update delivery; 1-100; default - 40.
300 ///
301 /// Use lower values to limit the load on your bot‘s server,
302 /// and higher values to increase your bot’s throughput.
303 pub fn with_max_connections(mut self, value: Integer) -> Self {
304 self.max_connections = Some(value);
305 self
306 }
307
308 /// Sets a new secret token.
309 ///
310 /// # Arguments
311 ///
312 /// * `value` - A secret token to be sent in a header.
313 ///
314 /// “X-Telegram-Bot-Api-Secret-Token” in every webhook request; 1-256 characters
315 ///
316 /// Only characters A-Z, a-z, 0-9, _ and - are allowed.
317 /// The header is useful to ensure that the request comes from a webhook set by you.
318 pub fn with_secret_token<T>(mut self, value: T) -> Self
319 where
320 T: Into<String>,
321 {
322 self.secret_token = Some(value.into());
323 self
324 }
325}
326
327impl Method for SetWebhook {
328 type Response = bool;
329
330 fn into_payload(self) -> Payload {
331 Payload::json("setWebhook", self)
332 }
333}