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 Message,
10 ParseMode,
11 PhotoSize,
12 ReplyMarkup,
13 ReplyMarkupError,
14 ReplyParameters,
15 ReplyParametersError,
16 SuggestedPostParameters,
17 SuggestedPostParametersError,
18 TextEntities,
19 TextEntity,
20 TextEntityError,
21 User,
22 Video,
23 },
24};
25
26#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
28pub struct PaidMediaPurchased {
29 pub from: User,
31 #[serde(rename = "paid_media_payload")]
33 pub payload: String,
34}
35
36impl PaidMediaPurchased {
37 pub fn new<T>(from: User, payload: T) -> Self
44 where
45 T: Into<String>,
46 {
47 Self {
48 from,
49 payload: payload.into(),
50 }
51 }
52}
53
54#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
56pub struct PaidMediaInfo {
57 pub star_count: Integer,
59 pub paid_media: Vec<PaidMedia>,
61}
62
63impl PaidMediaInfo {
64 pub fn new<A, B>(star_count: Integer, paid_media: A) -> Self
71 where
72 A: IntoIterator<Item = B>,
73 B: Into<PaidMedia>,
74 {
75 Self {
76 star_count,
77 paid_media: paid_media.into_iter().map(Into::into).collect(),
78 }
79 }
80}
81
82#[derive(Clone, Debug, derive_more::From, Deserialize, PartialEq, PartialOrd, Serialize)]
84#[serde(from = "RawPaidMedia", into = "RawPaidMedia")]
85#[allow(clippy::large_enum_variant)]
86pub enum PaidMedia {
87 Photo(Vec<PhotoSize>),
89 Preview(PaidMediaPreview),
91 Video(Video),
93}
94
95#[serde_with::skip_serializing_none]
97#[derive(Clone, Debug, Default, Deserialize, PartialEq, PartialOrd, Serialize)]
98pub struct PaidMediaPreview {
99 pub duration: Option<Integer>,
101 pub height: Option<Integer>,
103 pub width: Option<Integer>,
105}
106
107impl PaidMediaPreview {
108 pub fn with_duration(mut self, value: Integer) -> Self {
114 self.duration = Some(value);
115 self
116 }
117
118 pub fn with_height(mut self, value: Integer) -> Self {
124 self.height = Some(value);
125 self
126 }
127
128 pub fn with_width(mut self, value: Integer) -> Self {
134 self.width = Some(value);
135 self
136 }
137}
138
139#[serde_with::skip_serializing_none]
140#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
141#[serde(rename_all = "snake_case", tag = "type")]
142#[allow(clippy::large_enum_variant)]
143enum RawPaidMedia {
144 Photo {
145 photo: Vec<PhotoSize>,
146 },
147 Preview {
148 duration: Option<Integer>,
149 height: Option<Integer>,
150 width: Option<Integer>,
151 },
152 Video {
153 video: Video,
154 },
155}
156
157impl From<RawPaidMedia> for PaidMedia {
158 fn from(value: RawPaidMedia) -> Self {
159 match value {
160 RawPaidMedia::Photo { photo } => Self::Photo(photo),
161 RawPaidMedia::Preview {
162 duration,
163 height,
164 width,
165 } => Self::Preview(PaidMediaPreview {
166 duration,
167 height,
168 width,
169 }),
170 RawPaidMedia::Video { video } => Self::Video(video),
171 }
172 }
173}
174
175impl From<PaidMedia> for RawPaidMedia {
176 fn from(value: PaidMedia) -> Self {
177 match value {
178 PaidMedia::Photo(photo) => Self::Photo { photo },
179 PaidMedia::Preview(PaidMediaPreview {
180 duration,
181 height,
182 width,
183 }) => Self::Preview {
184 duration,
185 height,
186 width,
187 },
188 PaidMedia::Video(video) => Self::Video { video },
189 }
190 }
191}
192
193#[derive(Debug)]
195pub struct SendPaidMedia {
196 form: Form,
197}
198
199impl SendPaidMedia {
200 pub fn new<T>(chat_id: T, media: InputPaidMediaGroup, star_count: Integer) -> Self
208 where
209 T: Into<ChatId>,
210 {
211 let mut form: Form = media.into();
212 form.insert_field("chat_id", chat_id.into());
213 form.insert_field("star_count", star_count);
214 Self { form }
215 }
216
217 pub fn with_allow_paid_broadcast(mut self, value: bool) -> Self {
225 self.form.insert_field("allow_paid_broadcast", value);
226 self
227 }
228
229 pub fn with_business_connection_id<T>(mut self, value: T) -> Self
236 where
237 T: Into<String>,
238 {
239 self.form.insert_field("business_connection_id", value.into());
240 self
241 }
242
243 pub fn with_caption<T>(mut self, value: T) -> Self
249 where
250 T: Into<String>,
251 {
252 self.form.insert_field("caption", value.into());
253 self
254 }
255
256 pub fn with_caption_entities<T>(mut self, value: T) -> Result<Self, TextEntityError>
262 where
263 T: IntoIterator<Item = TextEntity>,
264 {
265 let value = value.into_iter().collect::<TextEntities>().serialize()?;
266 self.form.insert_field("caption_entities", value);
267 self.form.remove_field("parse_mode");
268 Ok(self)
269 }
270
271 pub fn with_direct_messages_topic_id(mut self, value: Integer) -> Self {
277 self.form.insert_field("direct_messages_topic_id", value);
278 self
279 }
280
281 pub fn with_disable_notification(mut self, value: bool) -> Self {
289 self.form.insert_field("disable_notification", value);
290 self
291 }
292
293 pub fn with_message_thread_id(mut self, value: Integer) -> Self {
300 self.form.insert_field("message_thread_id", value);
301 self
302 }
303
304 pub fn with_parse_mode(mut self, value: ParseMode) -> Self {
310 self.form.insert_field("parse_mode", value);
311 self.form.remove_field("caption_entities");
312 self
313 }
314
315 pub fn with_payload<T>(mut self, value: T) -> Self
323 where
324 T: Into<String>,
325 {
326 self.form.insert_field("payload", value.into());
327 self
328 }
329
330 pub fn with_protect_content(mut self, value: bool) -> Self {
336 self.form.insert_field("protect_content", value);
337 self
338 }
339
340 pub fn with_reply_parameters(mut self, value: ReplyParameters) -> Result<Self, ReplyParametersError> {
346 let value = value.serialize()?;
347 self.form.insert_field("reply_parameters", value);
348 Ok(self)
349 }
350
351 pub fn with_reply_markup<T>(mut self, value: T) -> Result<Self, ReplyMarkupError>
357 where
358 T: Into<ReplyMarkup>,
359 {
360 let value = value.into().serialize()?;
361 self.form.insert_field("reply_markup", value);
362 Ok(self)
363 }
364
365 pub fn with_show_caption_above_media(mut self, value: bool) -> Self {
371 self.form.insert_field("show_caption_above_media", value);
372 self
373 }
374
375 pub fn with_suggested_post_parameters(
385 mut self,
386 value: &SuggestedPostParameters,
387 ) -> Result<Self, SuggestedPostParametersError> {
388 let value = serde_json::to_string(value).map_err(SuggestedPostParametersError::Serialize)?;
389 self.form.insert_field("suggested_post_parameters", value);
390 Ok(self)
391 }
392}
393
394impl Method for SendPaidMedia {
395 type Response = Message;
396
397 fn into_payload(self) -> Payload {
398 Payload::form("sendPaidMedia", self.form)
399 }
400}