tgbot/types/definitions/media/
paid.rs1use serde::{Deserialize, Serialize};
2
3use crate::{
4 api::{Form, Method, Payload},
5 types::{
6 ChatId,
7 InputPaidMediaGroup,
8 Integer,
9 LivePhoto,
10 Message,
11 ParseMode,
12 PhotoSize,
13 ReplyMarkup,
14 ReplyMarkupError,
15 ReplyParameters,
16 ReplyParametersError,
17 SuggestedPostParameters,
18 SuggestedPostParametersError,
19 TextEntities,
20 TextEntity,
21 TextEntityError,
22 User,
23 Video,
24 },
25};
26
27#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
29pub struct PaidMediaPurchased {
30 pub from: User,
32 #[serde(rename = "paid_media_payload")]
34 pub payload: String,
35}
36
37impl PaidMediaPurchased {
38 pub fn new<T>(from: User, payload: T) -> Self
45 where
46 T: Into<String>,
47 {
48 Self {
49 from,
50 payload: payload.into(),
51 }
52 }
53}
54
55#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
57pub struct PaidMediaInfo {
58 pub star_count: Integer,
60 pub paid_media: Vec<PaidMedia>,
62}
63
64impl PaidMediaInfo {
65 pub fn new<A, B>(star_count: Integer, paid_media: A) -> Self
72 where
73 A: IntoIterator<Item = B>,
74 B: Into<PaidMedia>,
75 {
76 Self {
77 star_count,
78 paid_media: paid_media.into_iter().map(Into::into).collect(),
79 }
80 }
81}
82
83#[derive(Clone, Debug, derive_more::From, Deserialize, PartialEq, PartialOrd, Serialize)]
85#[serde(from = "RawPaidMedia", into = "RawPaidMedia")]
86pub enum PaidMedia {
87 LivePhoto(LivePhoto),
89 Photo(Vec<PhotoSize>),
91 Preview(PaidMediaPreview),
93 #[from(Video)]
95 Video(Box<Video>),
96}
97
98#[serde_with::skip_serializing_none]
100#[derive(Clone, Debug, Default, Deserialize, PartialEq, PartialOrd, Serialize)]
101pub struct PaidMediaPreview {
102 pub duration: Option<Integer>,
104 pub height: Option<Integer>,
106 pub width: Option<Integer>,
108}
109
110impl PaidMediaPreview {
111 pub fn with_duration(mut self, value: Integer) -> Self {
117 self.duration = Some(value);
118 self
119 }
120
121 pub fn with_height(mut self, value: Integer) -> Self {
127 self.height = Some(value);
128 self
129 }
130
131 pub fn with_width(mut self, value: Integer) -> Self {
137 self.width = Some(value);
138 self
139 }
140}
141
142#[serde_with::skip_serializing_none]
143#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
144#[serde(rename_all = "snake_case", tag = "type")]
145enum RawPaidMedia {
146 LivePhoto {
147 live_photo: LivePhoto,
148 },
149 Photo {
150 photo: Vec<PhotoSize>,
151 },
152 Preview {
153 duration: Option<Integer>,
154 height: Option<Integer>,
155 width: Option<Integer>,
156 },
157 Video {
158 video: Box<Video>,
159 },
160}
161
162impl From<RawPaidMedia> for PaidMedia {
163 fn from(value: RawPaidMedia) -> Self {
164 match value {
165 RawPaidMedia::LivePhoto { live_photo } => Self::LivePhoto(live_photo),
166 RawPaidMedia::Photo { photo } => Self::Photo(photo),
167 RawPaidMedia::Preview {
168 duration,
169 height,
170 width,
171 } => Self::Preview(PaidMediaPreview {
172 duration,
173 height,
174 width,
175 }),
176 RawPaidMedia::Video { video } => Self::Video(video),
177 }
178 }
179}
180
181impl From<PaidMedia> for RawPaidMedia {
182 fn from(value: PaidMedia) -> Self {
183 match value {
184 PaidMedia::LivePhoto(live_photo) => Self::LivePhoto { live_photo },
185 PaidMedia::Photo(photo) => Self::Photo { photo },
186 PaidMedia::Preview(PaidMediaPreview {
187 duration,
188 height,
189 width,
190 }) => Self::Preview {
191 duration,
192 height,
193 width,
194 },
195 PaidMedia::Video(video) => Self::Video { video },
196 }
197 }
198}
199
200#[derive(Debug)]
202pub struct SendPaidMedia {
203 form: Form,
204}
205
206impl SendPaidMedia {
207 pub fn new<T>(chat_id: T, media: InputPaidMediaGroup, star_count: Integer) -> Self
215 where
216 T: Into<ChatId>,
217 {
218 let mut form: Form = media.into();
219 form.insert_field("chat_id", chat_id.into());
220 form.insert_field("star_count", star_count);
221 Self { form }
222 }
223
224 pub fn with_allow_paid_broadcast(mut self, value: bool) -> Self {
232 self.form.insert_field("allow_paid_broadcast", value);
233 self
234 }
235
236 pub fn with_business_connection_id<T>(mut self, value: T) -> Self
243 where
244 T: Into<String>,
245 {
246 self.form.insert_field("business_connection_id", value.into());
247 self
248 }
249
250 pub fn with_caption<T>(mut self, value: T) -> Self
256 where
257 T: Into<String>,
258 {
259 self.form.insert_field("caption", value.into());
260 self
261 }
262
263 pub fn with_caption_entities<T>(mut self, value: T) -> Result<Self, TextEntityError>
269 where
270 T: IntoIterator<Item = TextEntity>,
271 {
272 let value = value.into_iter().collect::<TextEntities>().serialize()?;
273 self.form.insert_field("caption_entities", value);
274 self.form.remove_field("parse_mode");
275 Ok(self)
276 }
277
278 pub fn with_direct_messages_topic_id(mut self, value: Integer) -> Self {
284 self.form.insert_field("direct_messages_topic_id", value);
285 self
286 }
287
288 pub fn with_disable_notification(mut self, value: bool) -> Self {
296 self.form.insert_field("disable_notification", value);
297 self
298 }
299
300 pub fn with_message_thread_id(mut self, value: Integer) -> Self {
307 self.form.insert_field("message_thread_id", value);
308 self
309 }
310
311 pub fn with_parse_mode(mut self, value: ParseMode) -> Self {
317 self.form.insert_field("parse_mode", value);
318 self.form.remove_field("caption_entities");
319 self
320 }
321
322 pub fn with_payload<T>(mut self, value: T) -> Self
330 where
331 T: Into<String>,
332 {
333 self.form.insert_field("payload", value.into());
334 self
335 }
336
337 pub fn with_protect_content(mut self, value: bool) -> Self {
343 self.form.insert_field("protect_content", value);
344 self
345 }
346
347 pub fn with_reply_parameters(mut self, value: ReplyParameters) -> Result<Self, ReplyParametersError> {
353 let value = value.serialize()?;
354 self.form.insert_field("reply_parameters", value);
355 Ok(self)
356 }
357
358 pub fn with_reply_markup<T>(mut self, value: T) -> Result<Self, ReplyMarkupError>
364 where
365 T: Into<ReplyMarkup>,
366 {
367 let value = value.into().serialize()?;
368 self.form.insert_field("reply_markup", value);
369 Ok(self)
370 }
371
372 pub fn with_show_caption_above_media(mut self, value: bool) -> Self {
378 self.form.insert_field("show_caption_above_media", value);
379 self
380 }
381
382 pub fn with_suggested_post_parameters(
392 mut self,
393 value: &SuggestedPostParameters,
394 ) -> Result<Self, SuggestedPostParametersError> {
395 self.form.insert_field("suggested_post_parameters", value.serialize()?);
396 Ok(self)
397 }
398}
399
400impl Method for SendPaidMedia {
401 type Response = Message;
402
403 fn into_payload(self) -> Payload {
404 Payload::form("sendPaidMedia", self.form)
405 }
406}