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