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