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#[derive(Debug, Deserialize)]
12pub struct Update {
13 pub update_id: u32,
20 #[serde(flatten)]
21 pub kind: UpdateKind,
23}
24
25#[derive(Debug, Deserialize)]
27#[serde(untagged)]
28pub enum UpdateKind {
29 Message { message: Message },
31 EditedMessage { edited_message: Message },
33 ChannelPost { channel_post: Message },
35 EditedChannelPost { edited_channel_post: Message },
37 InlineQuery { inline_query: InlineQuery },
39 ChosenInlineResult {
44 chosen_inline_result: ChosenInlineResult,
45 },
46 CallbackQuery { callback_query: CallbackQuery },
48 ShippingQuery { shipping_query: ShippingQuery },
50 PreCheckoutQuery {
52 pre_checkout_query: PreCheckoutQuery,
53 },
54 Poll { poll: Poll },
57 PollAnswer { poll_answer: PollAnswer },
60 MyChatMemberUpdated { my_chat_member: ChatMemberUpdated },
63 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#[derive(Clone, Serialize)]
220pub struct GetUpdates {
221 #[serde(skip_serializing_if = "Option::is_none")]
228 pub offset: Option<i32>,
229 #[serde(skip_serializing_if = "Option::is_none")]
232 pub limit: Option<u32>,
233 #[serde(skip_serializing_if = "Option::is_none")]
237 pub timeout: Option<u32>,
238 #[serde(skip_serializing_if = "Option::is_none")]
247 pub allowed_updates: Option<Vec<String>>,
248}
249
250impl GetUpdates {
251 pub fn new() -> Self {
253 Self {
254 offset: None,
255 limit: None,
256 timeout: None,
257 allowed_updates: None,
258 }
259 }
260 pub fn with_offset(self, offset: i32) -> Self {
262 Self {
263 offset: Some(offset),
264 ..self
265 }
266 }
267 pub fn with_limit(self, limit: u32) -> Self {
269 Self {
270 limit: Some(limit),
271 ..self
272 }
273 }
274 pub fn with_timeout(self, timeout: u32) -> Self {
276 Self {
277 timeout: Some(timeout),
278 ..self
279 }
280 }
281 pub fn with_allowed_updates(self, updates: Vec<String>) -> Self {
283 Self {
284 allowed_updates: Some(updates),
285 ..self
286 }
287 }
288 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 {}