telbot_types/
update.rs

1use serde::{Deserialize, Serialize};
2
3use crate::chat::ChatMemberUpdated;
4use crate::message::{Message, Poll, PollAnswer};
5use crate::payment::{PreCheckoutQuery, ShippingQuery};
6use crate::query::{CallbackQuery, ChosenInlineResult, InlineQuery};
7use crate::{JsonMethod, TelegramMethod};
8
9/// This object represents an incoming update.
10/// At most **one** of the optional parameters can be present in any given update.
11#[derive(Debug, Deserialize)]
12pub struct Update {
13    /// The update's unique identifier.
14    /// Update identifiers start from a certain positive number and increase sequentially.
15    /// This ID becomes especially handy if you're using Webhooks,
16    /// since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order.
17    /// If there are no new updates for at least a week,
18    /// then identifier of the next update will be chosen randomly instead of sequentially.
19    pub update_id: u32,
20    #[serde(flatten)]
21    /// Update type
22    pub kind: UpdateKind,
23}
24
25/// Update type
26#[derive(Debug, Deserialize)]
27#[serde(untagged)]
28pub enum UpdateKind {
29    /// New incoming message of any kind — text, photo, sticker, etc.
30    Message { message: Message },
31    /// New version of a message that is known to the bot and was edited
32    EditedMessage { edited_message: Message },
33    /// New incoming channel post of any kind — text, photo, sticker, etc.
34    ChannelPost { channel_post: Message },
35    /// New version of a channel post that is known to the bot and was edited
36    EditedChannelPost { edited_channel_post: Message },
37    /// New incoming [inline](https://core.telegram.org/bots/api#inline-mode) query
38    InlineQuery { inline_query: InlineQuery },
39    /// The result of an [inline](https://core.telegram.org/bots/api#inline-mode)
40    /// query that was chosen by a user and sent to their chat partner.
41    /// Please see Telegram's documentation on the [feedback collecting](https://core.telegram.org/bots/inline#collecting-feedback) for details
42    /// on how to enable these updates for your bot.
43    ChosenInlineResult {
44        chosen_inline_result: ChosenInlineResult,
45    },
46    /// New incoming callback query
47    CallbackQuery { callback_query: CallbackQuery },
48    /// New incoming shipping query. Only for invoices with flexible price
49    ShippingQuery { shipping_query: ShippingQuery },
50    /// New incoming pre-checkout query. Contains full information about checkout
51    PreCheckoutQuery {
52        pre_checkout_query: PreCheckoutQuery,
53    },
54    /// New poll state.
55    /// Bots receive only updates about stopped polls and polls, which are sent by the bot
56    Poll { poll: Poll },
57    /// A user changed their answer in a non-anonymous poll.
58    /// Bots receive new votes only in polls that were sent by the bot itself.
59    PollAnswer { poll_answer: PollAnswer },
60    /// The bot's chat member status was updated in a chat. For private chats,
61    /// this update is received only when the bot is blocked or unblocked by the user.
62    MyChatMemberUpdated { my_chat_member: ChatMemberUpdated },
63    /// A chat member's status was updated in a chat.
64    /// The bot must be an administrator in the chat and must explicitly specify “chat_member”
65    /// in the list of *allowed_updates* to receive these updates.
66    ChatMemberUpdated { chat_member: ChatMemberUpdated },
67}
68
69impl UpdateKind {
70    pub fn message(&self) -> Option<&Message> {
71        match self {
72            Self::Message { message } => Some(message),
73            _ => None,
74        }
75    }
76
77    pub fn edited_message(&self) -> Option<&Message> {
78        match self {
79            Self::EditedMessage { edited_message } => Some(edited_message),
80            _ => None,
81        }
82    }
83
84    pub fn channel_post(&self) -> Option<&Message> {
85        match self {
86            Self::ChannelPost { channel_post } => Some(channel_post),
87            _ => None,
88        }
89    }
90
91    pub fn edited_channel_post(&self) -> Option<&Message> {
92        match self {
93            Self::EditedChannelPost {
94                edited_channel_post,
95            } => Some(edited_channel_post),
96            _ => None,
97        }
98    }
99
100    pub fn inline_query(&self) -> Option<&InlineQuery> {
101        match self {
102            Self::InlineQuery { inline_query } => Some(inline_query),
103            _ => None,
104        }
105    }
106
107    pub fn chosen_inline_result(&self) -> Option<&ChosenInlineResult> {
108        match self {
109            Self::ChosenInlineResult {
110                chosen_inline_result,
111            } => Some(chosen_inline_result),
112            _ => None,
113        }
114    }
115
116    pub fn callback_query(&self) -> Option<&CallbackQuery> {
117        match self {
118            Self::CallbackQuery { callback_query } => Some(callback_query),
119            _ => None,
120        }
121    }
122
123    pub fn shipping_query(&self) -> Option<&ShippingQuery> {
124        match self {
125            Self::ShippingQuery { shipping_query } => Some(shipping_query),
126            _ => None,
127        }
128    }
129
130    pub fn pre_checkout_query(&self) -> Option<&PreCheckoutQuery> {
131        match self {
132            Self::PreCheckoutQuery { pre_checkout_query } => Some(pre_checkout_query),
133            _ => None,
134        }
135    }
136
137    pub fn poll(&self) -> Option<&Poll> {
138        match self {
139            Self::Poll { poll } => Some(poll),
140            _ => None,
141        }
142    }
143
144    pub fn poll_answer(&self) -> Option<&PollAnswer> {
145        match self {
146            Self::PollAnswer { poll_answer } => Some(poll_answer),
147            _ => None,
148        }
149    }
150
151    pub fn my_chat_member(&self) -> Option<&ChatMemberUpdated> {
152        match self {
153            Self::MyChatMemberUpdated { my_chat_member } => Some(my_chat_member),
154            _ => None,
155        }
156    }
157
158    pub fn chat_member(&self) -> Option<&ChatMemberUpdated> {
159        match self {
160            Self::ChatMemberUpdated { chat_member } => Some(chat_member),
161            _ => None,
162        }
163    }
164
165    pub fn is_message(&self) -> bool {
166        matches!(self, Self::Message { .. })
167    }
168
169    pub fn is_edited_message(&self) -> bool {
170        matches!(self, Self::EditedMessage { .. })
171    }
172
173    pub fn is_channel_post(&self) -> bool {
174        matches!(self, Self::ChannelPost { .. })
175    }
176
177    pub fn is_edited_channel_post(&self) -> bool {
178        matches!(self, Self::EditedChannelPost { .. })
179    }
180
181    pub fn is_inline_query(&self) -> bool {
182        matches!(self, Self::InlineQuery { .. })
183    }
184
185    pub fn is_chosen_inline_result(&self) -> bool {
186        matches!(self, Self::ChosenInlineResult { .. })
187    }
188
189    pub fn is_callback_query(&self) -> bool {
190        matches!(self, Self::CallbackQuery { .. })
191    }
192
193    pub fn is_shipping_query(&self) -> bool {
194        matches!(self, Self::ShippingQuery { .. })
195    }
196
197    pub fn is_pre_checkout_query(&self) -> bool {
198        matches!(self, Self::PreCheckoutQuery { .. })
199    }
200
201    pub fn is_poll(&self) -> bool {
202        matches!(self, Self::Poll { .. })
203    }
204
205    pub fn is_poll_answer(&self) -> bool {
206        matches!(self, Self::PollAnswer { .. })
207    }
208
209    pub fn is_my_chat_member_updated(&self) -> bool {
210        matches!(self, Self::MyChatMemberUpdated { .. })
211    }
212
213    pub fn is_chat_member_updated(&self) -> bool {
214        matches!(self, Self::ChatMemberUpdated { .. })
215    }
216}
217
218/// Use this method to receive incoming updates using long polling ([wiki](https://en.wikipedia.org/wiki/Push_technology#Long_polling)).
219#[derive(Clone, Serialize)]
220pub struct GetUpdates {
221    /// Identifier of the first update to be returned.
222    /// Must be greater by one than the highest among the identifiers of previously received updates.
223    /// By default, updates starting with the earliest unconfirmed update are returned.
224    /// An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id.
225    /// The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue.
226    /// All previous updates will forgotten.
227    #[serde(skip_serializing_if = "Option::is_none")]
228    pub offset: Option<i32>,
229    /// Limits the number of updates to be retrieved.
230    /// Values between 1-100 are accepted. Defaults to 100.
231    #[serde(skip_serializing_if = "Option::is_none")]
232    pub limit: Option<u32>,
233    /// Timeout in seconds for long polling.
234    /// Defaults to 0, i.e. usual short polling.
235    /// Should be positive, short polling should be used for testing purposes only.
236    #[serde(skip_serializing_if = "Option::is_none")]
237    pub timeout: Option<u32>,
238    /// A JSON-serialized list of the update types you want your bot to receive.
239    /// For example, specify [“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types.
240    /// See [Update](https://core.telegram.org/bots/api#update) for a complete list of available update types.
241    /// Specify an empty list to receive all update types except chat_member (default).
242    /// If not specified, the previous setting will be used.
243    ///
244    /// Please note that this parameter doesn't affect updates created before the call to the getUpdates,
245    /// so unwanted updates may be received for a short period of time.
246    #[serde(skip_serializing_if = "Option::is_none")]
247    pub allowed_updates: Option<Vec<String>>,
248}
249
250impl GetUpdates {
251    /// Create a new getUpdates request
252    pub fn new() -> Self {
253        Self {
254            offset: None,
255            limit: None,
256            timeout: None,
257            allowed_updates: None,
258        }
259    }
260    /// Set offset
261    pub fn with_offset(self, offset: i32) -> Self {
262        Self {
263            offset: Some(offset),
264            ..self
265        }
266    }
267    /// Set limit
268    pub fn with_limit(self, limit: u32) -> Self {
269        Self {
270            limit: Some(limit),
271            ..self
272        }
273    }
274    /// Set timeout
275    pub fn with_timeout(self, timeout: u32) -> Self {
276        Self {
277            timeout: Some(timeout),
278            ..self
279        }
280    }
281    /// Set allowed updates
282    pub fn with_allowed_updates(self, updates: Vec<String>) -> Self {
283        Self {
284            allowed_updates: Some(updates),
285            ..self
286        }
287    }
288    /// Add one allowed update
289    pub fn with_allowed_update(mut self, update: impl Into<String>) -> Self {
290        let updates = self.allowed_updates.get_or_insert_with(Default::default);
291        updates.push(update.into());
292        Self {
293            allowed_updates: self.allowed_updates,
294            ..self
295        }
296    }
297}
298
299impl TelegramMethod for GetUpdates {
300    type Response = Vec<Update>;
301
302    fn name() -> &'static str {
303        "getUpdates"
304    }
305}
306
307impl JsonMethod for GetUpdates {}