telegram_bot2/models/updates.rs
1use crate::models::{CallbackQuery, ChatJoinRequest, ChatMember, ChosenInlineResult, InlineQuery, Message, Poll, PollAnswer, PreCheckoutQuery, ShippingQuery};
2use crate::FileHolder;
3use serde::{Deserialize, Serialize};
4
5#[derive(Deserialize, Debug, Clone)]
6/// Incoming update json
7pub(crate) struct UpdateStruct {
8 /// The update's unique identifier. Update identifiers start from a certain positive number and increase sequentially. This ID becomes especially handy if you're using webhooks, since it allows you to ignore repeated updates or to restore the correct update sequence, should they get out of order. If there are no new updates for at least a week, then identifier of the next update will be chosen randomly instead of sequentially.
9 pub(crate) update_id: i64,
10 /// New incoming message of any kind - text, photo, sticker, etc.
11 message: Option<Message>,
12 /// New version of a message that is known to the bot and was edited
13 edited_message: Option<Message>,
14 /// New incoming channel post of any kind - text, photo, sticker, etc.
15 channel_post: Option<Message>,
16 /// New version of a channel post that is known to the bot and was edited
17 edited_channel_post: Option<Message>,
18 /// New incoming inline query
19 inline_query: Option<InlineQuery>,
20 /// The result of an inline query that was chosen by a user and sent to their chat partner. Please see our documentation on the feedback collecting for details on how to enable these updates for your bot.
21 chosen_inline_result: Option<ChosenInlineResult>,
22 /// New incoming callback query
23 callback_query: Option<CallbackQuery>,
24 /// New incoming shipping query. Only for invoices with flexible price
25 shipping_query: Option<ShippingQuery>,
26 /// New incoming pre-checkout query. Contains full information about checkout
27 pre_checkout_query: Option<PreCheckoutQuery>,
28 /// New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot
29 poll: Option<Poll>,
30 /// A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.
31 poll_answer: Option<PollAnswer>,
32 /// The bot's chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.
33 my_chat_member: Option<ChatMember>,
34 /// A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.
35 chat_member: Option<ChatMember>,
36 /// A request to join the chat has been sent. The bot must have the can_invite_users administrator right in the chat to receive these updates.
37 chat_join_request: Option<ChatJoinRequest>,
38}
39
40#[derive(Serialize, FileHolder)]
41pub(crate) struct GetUpdates {
42 /// Identifier of the first update to be returned. Must be greater by one than the highest among the identifiers of previously received updates. By default, updates starting with the earliest unconfirmed update are returned. An update is considered confirmed as soon as getUpdates is called with an offset higher than its update_id. The negative offset can be specified to retrieve updates starting from -offset update from the end of the updates queue. All previous updates will forgotten.
43 #[serde(default, skip_serializing_if = "Option::is_none")]
44 pub(crate) offset: Option<i64>,
45
46 /// Limits the number of updates to be retrieved. Values between 1-100 are accepted. Defaults to 100.
47 #[serde(default, skip_serializing_if = "Option::is_none")]
48 pub(crate) limit: Option<i64>,
49
50 /// Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling. Should be positive, short polling should be used for testing purposes only.
51 #[serde(default, skip_serializing_if = "Option::is_none")]
52 pub(crate) timeout: Option<i64>,
53
54 /// A JSON-serialized list of the update types you want your bot to receive. For example, specify \[“message”, “edited_channel_post”, “callback_query”] to only receive updates of these types. See Update for a complete list of available update types. Specify an empty list to receive all update types except chat_member (default). If not specified, the previous setting will be used.
55 /// Please note that this parameter doesn't affect updates created before the call to the getUpdates, so unwanted updates may be received for a short period of time.
56 #[serde(default, skip_serializing_if = "Vec::is_empty")]
57 pub(crate) allowed_updates: Vec<UpdateType>,
58}
59
60/// This object represents an incoming update
61#[allow(clippy::large_enum_variant)]
62#[derive(Debug, Clone)]
63pub enum Update {
64 /// New incoming message of any kind - text, photo, sticker, etc.
65 NewMessage(i64, Message),
66 /// New version of a message that is known to the bot and was edited
67 EditedMessage(i64, Message),
68
69 /// New incoming channel post of any kind - text, photo, sticker, etc.
70 NewChannelPost(i64, Message),
71 /// New version of a channel post that is known to the bot and was edited
72 EditedChannelPost(i64, Message),
73
74 /// New incoming inline query
75 InlineQuery(i64, InlineQuery),
76 /// The result of an inline query that was chosen by a user and sent to their chat partner. Please see our documentation on the feedback collecting for details on how to enable these updates for your bot
77 ChosenInlineResult(i64, ChosenInlineResult),
78 /// New incoming callback query
79 CallBackQuery(i64, CallbackQuery),
80
81 /// New incoming shipping query. Only for invoices with flexible price
82 ShippingQuery(i64, ShippingQuery),
83 /// New incoming pre-checkout query. Contains full information about checkout
84 PreCheckoutQuery(i64, PreCheckoutQuery),
85
86 /// New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot
87 Poll(i64, Poll),
88 /// A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.
89 PollAnswer(i64, PollAnswer),
90
91 /// The bot's chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.
92 /// The parameter will always be ChatMemberUpdated
93 BotStatus(i64, ChatMember),
94 /// A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.
95 /// The parameter will always be ChatMemberUpdated
96 MemberStatus(i64, ChatMember),
97
98 /// A request to join the chat has been sent. The bot must have the can_invite_users administrator right in the chat to receive these updates.
99 ChatJoinRequest(i64, ChatJoinRequest),
100}
101
102#[derive(Serialize, Copy, Clone, Debug, Eq, PartialEq)]
103#[serde(rename_all = "snake_case")]
104/// Type of an incoming update
105pub enum UpdateType {
106 /// New incoming message of any kind - text, photo, sticker, etc.
107 #[serde(rename = "message")]
108 NewMessage,
109 /// New version of a message that is known to the bot and was edited
110 EditedMessage,
111
112 /// New incoming channel post of any kind - text, photo, sticker, etc.
113 #[serde(rename = "channel_post")]
114 NewChannelPost,
115 /// New version of a channel post that is known to the bot and was edited
116 EditedChannelPost,
117
118 /// New incoming inline query
119 InlineQuery,
120 /// The result of an inline query that was chosen by a user and sent to their chat partner. Please see our documentation on the feedback collecting for details on how to enable these updates for your bot
121 ChosenInlineResult,
122 /// New incoming callback query
123 CallBackQuery,
124
125 /// New incoming shipping query. Only for invoices with flexible price
126 ShippingQuery,
127 /// New incoming pre-checkout query. Contains full information about checkout
128 PreCheckoutQuery,
129
130 /// New poll state. Bots receive only updates about stopped polls and polls, which are sent by the bot
131 Poll,
132 /// A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.
133 PollAnswer,
134
135 /// The bot's chat member status was updated in a chat. For private chats, this update is received only when the bot is blocked or unblocked by the user.
136 /// The parameter will always be ChatMemberUpdated
137 #[serde(rename = "my_chat_member")]
138 BotStatus,
139 /// A chat member's status was updated in a chat. The bot must be an administrator in the chat and must explicitly specify “chat_member” in the list of allowed_updates to receive these updates.
140 /// The parameter will always be ChatMemberUpdated
141 #[serde(rename = "chat_member")]
142 MemberStatus,
143
144 /// A request to join the chat has been sent. The bot must have the can_invite_users administrator right in the chat to receive these updates.
145 ChatJoinRequest,
146}
147
148impl Update {
149 #[allow(missing_docs)]
150 pub fn get_type(&self) -> UpdateType {
151 match self {
152 Update::NewMessage(_, _) => UpdateType::NewMessage,
153 Update::EditedMessage(_, _) => UpdateType::EditedMessage,
154 Update::NewChannelPost(_, _) => UpdateType::NewChannelPost,
155 Update::EditedChannelPost(_, _) => UpdateType::EditedChannelPost,
156 Update::InlineQuery(_, _) => UpdateType::InlineQuery,
157 Update::ChosenInlineResult(_, _) => UpdateType::ChosenInlineResult,
158 Update::CallBackQuery(_, _) => UpdateType::CallBackQuery,
159 Update::ShippingQuery(_, _) => UpdateType::ShippingQuery,
160 Update::PreCheckoutQuery(_, _) => UpdateType::PreCheckoutQuery,
161 Update::Poll(_, _) => UpdateType::Poll,
162 Update::PollAnswer(_, _) => UpdateType::PollAnswer,
163 Update::BotStatus(_, _) => UpdateType::BotStatus,
164 Update::MemberStatus(_, _) => UpdateType::MemberStatus,
165 Update::ChatJoinRequest(_, _) => UpdateType::ChatJoinRequest,
166 }
167 }
168
169 #[allow(missing_docs)]
170 pub fn get_id(&self) -> i64 {
171 *match self {
172 Update::NewMessage(id, _) => id,
173 Update::EditedMessage(id, _) => id,
174 Update::NewChannelPost(id, _) => id,
175 Update::EditedChannelPost(id, _) => id,
176 Update::InlineQuery(id, _) => id,
177 Update::ChosenInlineResult(id, _) => id,
178 Update::CallBackQuery(id, _) => id,
179 Update::ShippingQuery(id, _) => id,
180 Update::PreCheckoutQuery(id, _) => id,
181 Update::Poll(id, _) => id,
182 Update::PollAnswer(id, _) => id,
183 Update::BotStatus(id, _) => id,
184 Update::MemberStatus(id, _) => id,
185 Update::ChatJoinRequest(id, _) => id,
186 }
187 }
188}
189
190impl TryFrom<UpdateStruct> for Update {
191 type Error = ();
192
193 fn try_from(value: UpdateStruct) -> Result<Self, Self::Error> {
194 if let Some(message) = value.message {
195 Ok(Update::NewMessage(value.update_id, message))
196 } else if let Some(edited_message) = value.edited_message {
197 Ok(Update::EditedMessage(value.update_id, edited_message))
198 } else if let Some(channel_post) = value.channel_post {
199 Ok(Update::NewChannelPost(value.update_id, channel_post))
200 } else if let Some(edited_channel_post) = value.edited_channel_post {
201 Ok(Update::EditedChannelPost(value.update_id, edited_channel_post))
202 } else if let Some(callback_query) = value.callback_query {
203 Ok(Update::CallBackQuery(value.update_id, callback_query))
204 } else if let Some(poll) = value.poll {
205 Ok(Update::Poll(value.update_id, poll))
206 } else if let Some(poll_answer) = value.poll_answer {
207 Ok(Update::PollAnswer(value.update_id, poll_answer))
208 } else if let Some(my_chat_member) = value.my_chat_member {
209 Ok(Update::BotStatus(value.update_id, my_chat_member))
210 } else if let Some(chat_member) = value.chat_member {
211 Ok(Update::MemberStatus(value.update_id, chat_member))
212 } else if let Some(chat_join_request) = value.chat_join_request {
213 Ok(Update::ChatJoinRequest(value.update_id, chat_join_request))
214 } else if let Some(shipping_query) = value.shipping_query {
215 Ok(Update::ShippingQuery(value.update_id, shipping_query))
216 } else if let Some(pre_checkout_query) = value.pre_checkout_query {
217 Ok(Update::PreCheckoutQuery(value.update_id, pre_checkout_query))
218 } else if let Some(inline_query) = value.inline_query {
219 Ok(Update::InlineQuery(value.update_id, inline_query))
220 } else if let Some(chosen_inline_result) = value.chosen_inline_result {
221 Ok(Update::ChosenInlineResult(value.update_id, chosen_inline_result))
222 } else {
223 Err(())
224 }
225 }
226}