telbot_types/
webhook.rs

1use std::collections::HashMap;
2
3use serde::{Deserialize, Serialize};
4
5use crate::file::InputFile;
6use crate::{FileMethod, JsonMethod, TelegramMethod};
7
8/// Contains information about the current status of a webhook.
9#[derive(Debug, Deserialize)]
10pub struct WebhookInfo {
11    /// Webhook URL, may be empty if webhook is not set up
12    pub url: String,
13    /// True, if a custom certificate was provided for webhook certificate checks
14    pub has_custom_certificate: bool,
15    /// Number of updates awaiting delivery
16    pub pending_update_count: u32,
17    /// Currently used webhook IP address
18    pub ip_address: Option<String>,
19    /// Unix time for the most recent error that happened when trying to deliver an update via webhook
20    pub last_error_date: u64,
21    /// Error message in human-readable format for the most recent error that happened when trying to deliver an update via webhook
22    pub last_error_message: Option<String>,
23    /// Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery
24    pub max_connections: Option<u32>,
25    /// A list of update types the bot is subscribed to.
26    /// Defaults to all update types except chat_member
27    pub allowed_updates: Option<Vec<String>>,
28}
29
30/// Use this method to specify a url and receive incoming updates via an outgoing webhook.
31/// Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized [Update](https://core.telegram.org/bots/api#update).
32/// In case of an unsuccessful request, we will give up after a reasonable amount of attempts.
33/// Returns True on success.
34///
35/// If you'd like to make sure that the Webhook request comes from Telegram,
36/// we recommend using a secret path in the URL, e.g. `https://www.example.com/<token>`.
37/// Since nobody else knows your bot's token, you can be pretty sure it's us.
38#[derive(Clone, Serialize)]
39pub struct SetWebhook {
40    /// HTTPS url to send updates to. Use an empty string to remove webhook integration
41    pub url: String,
42    /// Upload your public key certificate so that the root certificate in use can be checked.
43    /// See Telegram's [self-signed guide](https://core.telegram.org/bots/self-signed) for details.
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub certificate: Option<InputFile>,
46    /// The fixed IP address which will be used to send webhook requests instead of the IP address resolved through DNS
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub ip_address: Option<String>,
49    /// Maximum allowed number of simultaneous HTTPS connections to the webhook for update delivery, 1-100. Defaults to 40.
50    /// Use lower values to limit the load on your bot's server, and higher values to increase your bot's throughput.
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub max_connections: Option<u32>,
53    /// A JSON-serialized list of the update types you want your bot to receive.
54    /// For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types.
55    /// See [Update](https://core.telegram.org/bots/api#update) for a complete list of available update types.
56    /// Specify an empty list to receive all update types except chat_member (default).
57    /// If not specified, the previous setting will be used.
58    ///
59    /// Please note that this parameter doesn't affect updates created before the call to the getUpdates,
60    /// so unwanted updates may be received for a short period of time.
61    #[serde(skip_serializing_if = "Option::is_none")]
62    pub allowed_updates: Option<Vec<String>>,
63    /// Pass True to drop all pending updates
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub drop_pending_updates: Option<bool>,
66}
67
68impl SetWebhook {
69    /// Create a new setWebhook request that sets the webhook url.
70    pub fn new(url: String) -> Self {
71        Self {
72            url,
73            certificate: None,
74            ip_address: None,
75            max_connections: None,
76            allowed_updates: None,
77            drop_pending_updates: None,
78        }
79    }
80    /// Create a new setWebhook request that removes previous webhook.
81    pub fn remove_previous() -> Self {
82        Self {
83            url: "".to_string(),
84            certificate: None,
85            ip_address: None,
86            max_connections: None,
87            allowed_updates: None,
88            drop_pending_updates: None,
89        }
90    }
91    /// Set custom certificate for the webhook
92    pub fn with_certificate(self, cert: InputFile) -> Self {
93        Self {
94            certificate: Some(cert),
95            ..self
96        }
97    }
98    /// Set ip address to be used to send webhook request
99    pub fn with_ip_address(self, ip_address: impl Into<String>) -> Self {
100        Self {
101            ip_address: Some(ip_address.into()),
102            ..self
103        }
104    }
105    /// Set maximum simultaneous webhook request count
106    pub fn with_max_connections(self, max_connections: u32) -> Self {
107        Self {
108            max_connections: Some(max_connections),
109            ..self
110        }
111    }
112    /// Drop all pending updates
113    pub fn drop_pending_updates(self) -> Self {
114        Self {
115            drop_pending_updates: Some(true),
116            ..self
117        }
118    }
119}
120
121impl TelegramMethod for SetWebhook {
122    type Response = bool;
123
124    fn name() -> &'static str {
125        "setWebhook"
126    }
127}
128
129impl FileMethod for SetWebhook {
130    fn files(&self) -> Option<std::collections::HashMap<&str, &InputFile>> {
131        self.certificate.as_ref().map(|file| {
132            let mut map = HashMap::new();
133            map.insert("certificate", file);
134            map
135        })
136    }
137}
138
139/// Use this method to remove webhook integration if you decide to switch back to [getUpdates](https://core.telegram.org/bots/api#getupdates).
140/// Returns True on success.
141#[derive(Clone, Serialize)]
142pub struct DeleteWebhook {
143    /// Pass True to drop all pending updates
144    pub drop_pending_updates: Option<bool>,
145}
146
147impl DeleteWebhook {
148    pub fn new() -> Self {
149        Self {
150            drop_pending_updates: None,
151        }
152    }
153    /// Drop all pending updates
154    pub fn drop_pending_updates(self) -> Self {
155        Self {
156            drop_pending_updates: Some(true),
157            ..self
158        }
159    }
160}
161
162impl TelegramMethod for DeleteWebhook {
163    type Response = bool;
164
165    fn name() -> &'static str {
166        "deleteWebhook"
167    }
168}
169
170impl JsonMethod for DeleteWebhook {}
171
172/// Use this method to get current webhook status. Requires no parameters.
173/// On success, returns a WebhookInfo object.
174/// If the bot is using getUpdates, will return an object with the url field empty.
175#[derive(Clone, Serialize)]
176pub struct GetWebhookInfo;
177
178impl TelegramMethod for GetWebhookInfo {
179    type Response = WebhookInfo;
180
181    fn name() -> &'static str {
182        "getWebhookInfo"
183    }
184}
185
186impl JsonMethod for GetWebhookInfo {}