Skip to main content

tgbot/types/definitions/message/
reply.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::types::{
5    Animation,
6    Audio,
7    Chat,
8    Checklist,
9    Contact,
10    Dice,
11    Document,
12    Game,
13    Giveaway,
14    GiveawayWinners,
15    Integer,
16    Invoice,
17    LinkPreviewOptions,
18    LivePhoto,
19    Location,
20    Message,
21    MessageOrigin,
22    PaidMediaInfo,
23    PhotoSize,
24    Poll,
25    Sticker,
26    Story,
27    Venue,
28    Video,
29    VideoNote,
30    Voice,
31};
32
33/// Contains information about a message or a story that is being replied to.
34#[derive(Clone, Debug, Deserialize, Serialize)]
35pub enum ReplyTo {
36    /// The original message.
37    ///
38    /// Note that the Message object in this field will not contain further
39    /// `reply_to` fields even if it itself is a reply.
40    #[serde(rename = "reply_to_message")]
41    Message(Box<Message>),
42    /// The original story.
43    #[serde(rename = "reply_to_story")]
44    Story(Story),
45}
46
47impl From<Message> for ReplyTo {
48    fn from(value: Message) -> Self {
49        Self::Message(Box::new(value))
50    }
51}
52
53impl From<Story> for ReplyTo {
54    fn from(value: Story) -> Self {
55        Self::Story(value)
56    }
57}
58
59/// Contains information about a message that is being replied to, which may come from another chat or forum topic.
60#[serde_with::skip_serializing_none]
61#[derive(Clone, Debug, Deserialize, Serialize)]
62pub struct ExternalReplyInfo {
63    /// Origin of the message replied to by the given message.
64    pub origin: MessageOrigin,
65    /// Chat the original message belongs to.
66    ///
67    /// Available only if the chat is a supergroup or a channel.
68    pub chat: Option<Chat>,
69    /// Whether the message media is covered by a spoiler animation.
70    pub has_media_spoiler: Option<bool>,
71    /// Options used for link preview generation for the original message, if it is a text message.
72    pub link_preview_options: Option<LinkPreviewOptions>,
73    /// Unique message identifier inside the original chat.
74    ///
75    /// Available only if the original chat is a supergroup or a channel.
76    pub message_id: Option<Integer>,
77
78    /// Contains data of the message.
79    #[serde(flatten)]
80    pub data: ExternalReplyData,
81}
82
83impl ExternalReplyInfo {
84    /// Creates a new `ExternalReplyInfo`.
85    ///
86    /// # Arguments
87    ///
88    /// * `data` - Data of the message.
89    /// * `origin` - Origin of the message.
90    pub fn new<A, B>(data: A, origin: B) -> Self
91    where
92        A: Into<ExternalReplyData>,
93        B: Into<MessageOrigin>,
94    {
95        Self {
96            origin: origin.into(),
97            chat: None,
98            has_media_spoiler: None,
99            link_preview_options: None,
100            message_id: None,
101            data: data.into(),
102        }
103    }
104
105    /// Sets a new chat.
106    ///
107    /// # Arguments
108    ///
109    /// * `value` - Chat the original message belongs to.
110    pub fn with_chat<T>(mut self, value: T) -> Self
111    where
112        T: Into<Chat>,
113    {
114        self.chat = Some(value.into());
115        self
116    }
117
118    /// Sets a new value for the `has_media_spoiler` flag.
119    ///
120    /// # Arguments
121    ///
122    /// * `value` - Whether the message media is covered by a spoiler animation.
123    pub fn with_has_media_spoiler(mut self, value: bool) -> Self {
124        self.has_media_spoiler = Some(value);
125        self
126    }
127
128    /// Sets new link preview options.
129    ///
130    /// # Arguments
131    ///
132    /// * `value` - Options used for link preview generation for the original message, if it is a text message.
133    pub fn with_link_preview_options(mut self, value: LinkPreviewOptions) -> Self {
134        self.link_preview_options = Some(value);
135        self
136    }
137
138    /// Sets a new message ID.
139    ///
140    /// # Arguments
141    ///
142    /// * `value` - Unique message identifier inside the original chat.
143    pub fn with_message_id(mut self, value: Integer) -> Self {
144        self.message_id = Some(value);
145        self
146    }
147}
148
149/// Contains data of an external reply info.
150#[derive(Clone, Debug, Deserialize, derive_more::From, Serialize)]
151#[serde(rename_all = "snake_case")]
152pub enum ExternalReplyData {
153    /// Message is an animation, information about the animation.
154    Animation(Animation),
155    /// Message is an audio file, information about the file.
156    Audio(Audio),
157    /// Message is a checklist
158    Checklist(Checklist),
159    /// Message is a shared contact, information about the contact.
160    Contact(Contact),
161    /// Message is a dice with random value.
162    Dice(Dice),
163    /// Message is a general file, information about the file.
164    Document(Document),
165    /// Message is a game, information about the game.
166    Game(Game),
167    /// Message is a scheduled giveaway, information about the giveaway.
168    Giveaway(Giveaway),
169    /// A giveaway with public winners was completed.
170    GiveawayWinners(GiveawayWinners),
171    /// Message is an invoice for a payment, information about the invoice.
172    Invoice(Invoice),
173    /// Message is a live photo, information about live photo.
174    LivePhoto(LivePhoto),
175    /// Message is a shared location, information about the location.
176    Location(Location),
177    /// Message contains paid media, information about the paid media.
178    PaidMedia(PaidMediaInfo),
179    /// Message is a photo, available sizes of the photo.
180    Photo(Vec<PhotoSize>),
181    /// Message is a native poll, information about the poll.
182    Poll(Box<Poll>),
183    /// Message is a sticker, information about the sticker.
184    Sticker(Sticker),
185    /// Message is a forwarded story.
186    Story(Story),
187    /// Message is a venue, information about the venue.
188    Venue(Venue),
189    /// Message is a video, information about the video.
190    Video(Video),
191    /// Message is a video note, information about the video message.
192    VideoNote(VideoNote),
193    /// Message is a voice message, information about the file.
194    Voice(Voice),
195    /// Contains arbitrary data for future variants.
196    #[serde(untagged)]
197    Unknown(Value),
198}
199
200impl From<Poll> for ExternalReplyData {
201    fn from(value: Poll) -> Self {
202        Self::Poll(Box::new(value))
203    }
204}