Skip to main content

rustigram_types/
update.rs

1use serde::{Deserialize, Serialize};
2
3use crate::chat::ChatJoinRequest;
4use crate::inline::{ChosenInlineResult, InlineQuery};
5use crate::message::Message;
6use crate::payments::{BotSubscriptionUpdated, PreCheckoutQuery, ShippingQuery};
7use crate::poll::{Poll, PollAnswer};
8use crate::user::User;
9
10/// An incoming update from Telegram.
11///
12/// Only one of the optional fields will be `Some` per update.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct Update {
15    /// Unique sequential identifier.
16    pub update_id: i64,
17    /// The kind of update and its payload.
18    #[serde(flatten)]
19    pub kind: UpdateKind,
20}
21
22/// The specific kind of an incoming update.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(rename_all = "snake_case")]
25pub enum UpdateKind {
26    /// New incoming message.
27    Message(Message),
28    /// New version of a message.
29    EditedMessage(Message),
30    /// New incoming channel post.
31    ChannelPost(Message),
32    /// New version of a channel post.
33    EditedChannelPost(Message),
34    /// A message from a business account.
35    BusinessMessage(Message),
36    /// Edited message from a business account.
37    EditedBusinessMessage(Message),
38    /// New incoming inline query.
39    InlineQuery(InlineQuery),
40    /// The result of an inline query that was chosen.
41    ChosenInlineResult(ChosenInlineResult),
42    /// New incoming callback query.
43    CallbackQuery(Box<CallbackQuery>),
44    /// New incoming shipping query (only for invoices with flexible price).
45    ShippingQuery(ShippingQuery),
46    /// New incoming pre-checkout query.
47    PreCheckoutQuery(PreCheckoutQuery),
48    /// New poll state.
49    Poll(Poll),
50    /// A user changed their answer in a non-anonymous poll.
51    PollAnswer(PollAnswer),
52    /// Bot's chat member status was updated in a chat.
53    MyChatMember(ChatMemberUpdated),
54    /// A chat member's status was updated in a chat.
55    ChatMember(ChatMemberUpdated),
56    /// A request to join the chat has been sent.
57    ChatJoinRequest(ChatJoinRequest),
58    /// A reaction to a message was changed by a user.
59    MessageReaction(MessageReactionUpdated),
60    /// Reactions to a message with anonymous reactions were changed.
61    MessageReactionCount(MessageReactionCountUpdated),
62    /// A chat boost was added or changed.
63    ChatBoost(ChatBoostUpdated),
64    /// A boost was removed from a chat.
65    RemovedChatBoost(ChatBoostRemoved),
66    /// A managed bot was connected or disconnected.
67    ManagedBot(ManagedBotUpdated),
68    /// A business connection was established or removed.
69    BusinessConnection(BusinessConnection),
70    /// Messages were deleted from a connected business account.
71    DeletedBusinessMessages(BusinessMessagesDeleted),
72    /// Purchased paid media.
73    PurchasedPaidMedia(PaidMediaPurchased),
74    /// New guest message — the bot can reply using [`answerGuestQuery`](https://core.telegram.org/bots/api#answerguestquery).
75    GuestMessage(Message),
76    /// User payment subscription toward the bot has changed.
77    Subscription(BotSubscriptionUpdated),
78}
79
80impl Update {
81    /// Returns the `chat_id` if the update contains a message or callback query.
82    #[must_use]
83    pub fn chat_id(&self) -> Option<i64> {
84        match &self.kind {
85            UpdateKind::Message(m)
86            | UpdateKind::EditedMessage(m)
87            | UpdateKind::ChannelPost(m)
88            | UpdateKind::EditedChannelPost(m)
89            | UpdateKind::BusinessMessage(m)
90            | UpdateKind::EditedBusinessMessage(m)
91            | UpdateKind::GuestMessage(m) => Some(m.chat.id),
92            UpdateKind::CallbackQuery(q) => q.message.as_ref().map(|m| m.chat.id),
93            _ => None,
94        }
95    }
96
97    /// Returns the `from` user if available.
98    #[must_use]
99    pub fn from(&self) -> Option<&User> {
100        match &self.kind {
101            UpdateKind::Message(m)
102            | UpdateKind::EditedMessage(m)
103            | UpdateKind::ChannelPost(m)
104            | UpdateKind::EditedChannelPost(m)
105            | UpdateKind::BusinessMessage(m)
106            | UpdateKind::EditedBusinessMessage(m)
107            | UpdateKind::GuestMessage(m) => m.from.as_ref(),
108            UpdateKind::CallbackQuery(q) => Some(&q.from),
109            UpdateKind::InlineQuery(q) => Some(&q.from),
110            UpdateKind::ShippingQuery(q) => Some(&q.from),
111            UpdateKind::PreCheckoutQuery(q) => Some(&q.from),
112            UpdateKind::PollAnswer(a) => a.user.as_ref(),
113            UpdateKind::Subscription(s) => Some(&s.user),
114            _ => None,
115        }
116    }
117}
118
119/// Incoming callback query from a callback button in an inline keyboard.
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct CallbackQuery {
122    /// Unique identifier for this query.
123    pub id: String,
124    /// User who sent the query.
125    pub from: User,
126    /// Message sent by the bot with the button that originated the query.
127    #[serde(skip_serializing_if = "Option::is_none")]
128    pub message: Option<Message>,
129    /// Identifier of the message sent via the bot in inline mode.
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub inline_message_id: Option<String>,
132    /// Global identifier, uniquely corresponding to the chat where the query originated.
133    pub chat_instance: String,
134    /// Data associated with the callback button.
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub data: Option<String>,
137    /// Short name of a Game to be returned.
138    #[serde(skip_serializing_if = "Option::is_none")]
139    pub game_short_name: Option<String>,
140}
141
142/// A chat member's status change.
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct ChatMemberUpdated {
145    /// The chat where the change occurred.
146    pub chat: crate::chat::Chat,
147    /// The user who triggered the change.
148    pub from: User,
149    /// Date of the change, as a Unix timestamp.
150    pub date: i64,
151    /// Previous chat member status.
152    pub old_chat_member: crate::chat_member::ChatMember,
153    /// New chat member status.
154    pub new_chat_member: crate::chat_member::ChatMember,
155    /// Invite link used to join the chat, if any.
156    #[serde(skip_serializing_if = "Option::is_none")]
157    pub invite_link: Option<crate::chat::ChatInviteLink>,
158    /// `true` if the user joined via a join request.
159    #[serde(skip_serializing_if = "Option::is_none")]
160    pub via_join_request: Option<bool>,
161    /// `true` if the user joined via a folder invite link.
162    #[serde(skip_serializing_if = "Option::is_none")]
163    pub via_chat_folder_invite_link: Option<bool>,
164}
165
166/// A reaction to a message changed by a user.
167#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct MessageReactionUpdated {
169    /// The chat containing the message.
170    pub chat: crate::chat::Chat,
171    /// Identifier of the message that was reacted to.
172    pub message_id: i64,
173    /// The user who changed the reaction (non-anonymous reactions only).
174    #[serde(skip_serializing_if = "Option::is_none")]
175    pub user: Option<User>,
176    /// The chat that changed the reaction (anonymous reactions in groups).
177    #[serde(skip_serializing_if = "Option::is_none")]
178    pub actor_chat: Option<crate::chat::Chat>,
179    /// Date of the change, as a Unix timestamp.
180    pub date: i64,
181    /// Previous list of reactions.
182    pub old_reaction: Vec<crate::message::ReactionType>,
183    /// New list of reactions.
184    pub new_reaction: Vec<crate::message::ReactionType>,
185}
186
187/// Anonymous reactions to a message were changed.
188#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct MessageReactionCountUpdated {
190    /// The chat containing the message.
191    pub chat: crate::chat::Chat,
192    /// Identifier of the message.
193    pub message_id: i64,
194    /// Date of the change, as a Unix timestamp.
195    pub date: i64,
196    /// Updated list of reactions with counts.
197    pub reactions: Vec<ReactionCount>,
198}
199
200/// Count of a specific reaction type.
201#[derive(Debug, Clone, Serialize, Deserialize)]
202pub struct ReactionCount {
203    /// The reaction type.
204    #[serde(rename = "type")]
205    pub kind: crate::message::ReactionType,
206    /// Total number of reactions of this type.
207    pub total_count: u32,
208}
209
210/// A boost was added or changed in a chat.
211#[derive(Debug, Clone, Serialize, Deserialize)]
212pub struct ChatBoostUpdated {
213    /// The chat that was boosted.
214    pub chat: crate::chat::Chat,
215    /// Information about the boost.
216    pub boost: ChatBoost,
217}
218
219/// Information about a chat boost.
220#[derive(Debug, Clone, Serialize, Deserialize)]
221pub struct ChatBoost {
222    /// Unique identifier of the boost.
223    pub boost_id: String,
224    /// Unix timestamp when the boost was added.
225    pub add_date: i64,
226    /// Unix timestamp when the boost expires.
227    pub expiration_date: i64,
228    /// Source of the boost.
229    pub source: serde_json::Value,
230}
231
232/// A boost was removed from a chat.
233#[derive(Debug, Clone, Serialize, Deserialize)]
234pub struct ChatBoostRemoved {
235    /// The chat that lost the boost.
236    pub chat: crate::chat::Chat,
237    /// Unique identifier of the boost.
238    pub boost_id: String,
239    /// Unix timestamp when the boost was removed.
240    pub remove_date: i64,
241    /// Source of the removed boost.
242    pub source: serde_json::Value,
243}
244
245/// A managed bot was connected or disconnected.
246#[derive(Debug, Clone, Serialize, Deserialize)]
247pub struct ManagedBotUpdated {
248    /// The user who connected or disconnected the managed bot.
249    pub user: User,
250    /// The managed bot.
251    pub bot: User,
252}
253
254/// Represents the rights of a business bot.
255///
256/// All fields are optional — a missing field means the right is not granted.
257#[derive(Debug, Clone, Default, Serialize, Deserialize)]
258pub struct BusinessBotRights {
259    /// `true` if the bot can send and edit messages in private chats that had
260    /// incoming messages in the last 24 hours.
261    #[serde(skip_serializing_if = "Option::is_none")]
262    pub can_reply: Option<bool>,
263    /// `true` if the bot can mark incoming private messages as read.
264    #[serde(skip_serializing_if = "Option::is_none")]
265    pub can_read_messages: Option<bool>,
266    /// `true` if the bot can delete messages sent by the bot.
267    #[serde(skip_serializing_if = "Option::is_none")]
268    pub can_delete_sent_messages: Option<bool>,
269    /// `true` if the bot can delete all private messages in managed chats.
270    #[serde(skip_serializing_if = "Option::is_none")]
271    pub can_delete_all_messages: Option<bool>,
272    /// `true` if the bot can edit the first and last name of the business account.
273    #[serde(skip_serializing_if = "Option::is_none")]
274    pub can_edit_name: Option<bool>,
275    /// `true` if the bot can edit the bio of the business account.
276    #[serde(skip_serializing_if = "Option::is_none")]
277    pub can_edit_bio: Option<bool>,
278    /// `true` if the bot can edit the profile photo of the business account.
279    #[serde(skip_serializing_if = "Option::is_none")]
280    pub can_edit_profile_photo: Option<bool>,
281    /// `true` if the bot can edit the username of the business account.
282    #[serde(skip_serializing_if = "Option::is_none")]
283    pub can_edit_username: Option<bool>,
284    /// `true` if the bot can change gift privacy settings for the business account.
285    #[serde(skip_serializing_if = "Option::is_none")]
286    pub can_change_gift_settings: Option<bool>,
287    /// `true` if the bot can view gifts and the Telegram Stars balance of the business account.
288    #[serde(skip_serializing_if = "Option::is_none")]
289    pub can_view_gifts_and_stars: Option<bool>,
290    /// `true` if the bot can convert regular gifts owned by the business account to Telegram Stars.
291    #[serde(skip_serializing_if = "Option::is_none")]
292    pub can_convert_gifts_to_stars: Option<bool>,
293    /// `true` if the bot can transfer and upgrade gifts owned by the business account.
294    #[serde(skip_serializing_if = "Option::is_none")]
295    pub can_transfer_and_upgrade_gifts: Option<bool>,
296    /// `true` if the bot can transfer Telegram Stars received by the business account.
297    #[serde(skip_serializing_if = "Option::is_none")]
298    pub can_transfer_stars: Option<bool>,
299    /// `true` if the bot can post, edit, and delete stories on behalf of the business account.
300    #[serde(skip_serializing_if = "Option::is_none")]
301    pub can_manage_stories: Option<bool>,
302}
303
304/// A business connection was established or removed.
305#[derive(Debug, Clone, Serialize, Deserialize)]
306pub struct BusinessConnection {
307    /// Unique identifier of the business connection.
308    pub id: String,
309    /// The business account user.
310    pub user: User,
311    /// Identifier of the private chat with the user.
312    pub user_chat_id: i64,
313    /// Date the connection was established as a Unix timestamp.
314    pub date: i64,
315    /// `true` if the connection is active.
316    pub is_enabled: bool,
317    /// Rights granted to the bot within this business connection.
318    #[serde(skip_serializing_if = "Option::is_none")]
319    pub rights: Option<BusinessBotRights>,
320}
321
322/// A list of boosts added to a chat by a user.
323///
324/// Returned by [`getUserChatBoosts`](https://core.telegram.org/bots/api#getuserchatboosts).
325#[derive(Debug, Clone, Serialize, Deserialize)]
326pub struct UserChatBoosts {
327    /// The list of boosts added to the chat by the user.
328    pub boosts: Vec<ChatBoost>,
329}
330
331/// Business messages that were deleted.
332#[derive(Debug, Clone, Serialize, Deserialize)]
333pub struct BusinessMessagesDeleted {
334    /// Unique identifier of the business connection.
335    pub business_connection_id: String,
336    /// The chat in which the messages were deleted.
337    pub chat: crate::chat::Chat,
338    /// Identifiers of the deleted messages.
339    pub message_ids: Vec<i64>,
340}
341
342/// Purchased paid media.
343#[derive(Debug, Clone, Serialize, Deserialize)]
344pub struct PaidMediaPurchased {
345    /// The user who purchased the media.
346    pub from: User,
347    /// Bot-specified paid media payload.
348    pub paid_media_payload: String,
349}
350
351/// A list of updates returned by `getUpdates`. Internal deserialization wrapper.
352#[derive(Debug, Clone, Serialize, Deserialize)]
353pub struct Updates {
354    /// `true` if the request was successful.
355    pub ok: bool,
356    /// The list of updates.
357    pub result: Vec<Update>,
358}