use serde::{Deserialize, Serialize};
use crate::{
api::{Form, Method, Payload, PayloadError, WriteForm},
types::{
ChatId,
InputFile,
InputFileReader,
InputTextCaption,
Integer,
Message,
PhotoSize,
ReplyMarkup,
ReplyParameters,
SuggestedPostParameters,
},
};
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct Audio {
pub duration: Integer,
pub file_id: String,
pub file_unique_id: String,
pub file_name: Option<String>,
pub file_size: Option<Integer>,
pub mime_type: Option<String>,
pub performer: Option<String>,
pub title: Option<String>,
pub thumbnail: Option<PhotoSize>,
}
#[derive(Debug)]
pub struct SendAudio {
audio: InputFile,
thumbnail: Option<InputFileReader>,
parameters: SendAudioParameters,
}
impl SendAudio {
pub fn new<C, A>(chat_id: C, audio: A) -> Self
where
C: Into<ChatId>,
A: Into<InputFile>,
{
Self {
audio: audio.into(),
thumbnail: None,
parameters: SendAudioParameters {
chat_id: Some(chat_id.into()),
..Default::default()
},
}
}
pub fn with_allow_paid_broadcast(mut self, value: bool) -> Self {
self.parameters.allow_paid_broadcast = Some(value);
self
}
pub fn with_business_connection_id<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.parameters.business_connection_id = Some(value.into());
self
}
pub fn with_callback_query_id<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.parameters.callback_query_id = Some(value.into());
self
}
pub fn with_caption<T>(mut self, value: T) -> Self
where
T: Into<InputTextCaption>,
{
self.parameters.caption = Some(value.into());
self
}
pub fn with_direct_messages_topic_id(mut self, value: Integer) -> Self {
self.parameters.direct_messages_topic_id = Some(value);
self
}
pub fn with_disable_notification(mut self, value: bool) -> Self {
self.parameters.disable_notification = Some(value);
self
}
pub fn with_duration(mut self, value: Integer) -> Self {
self.parameters.duration = Some(value);
self
}
pub fn with_message_effect_id<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.parameters.message_effect_id = Some(value.into());
self
}
pub fn with_message_thread_id(mut self, value: Integer) -> Self {
self.parameters.message_thread_id = Some(value);
self
}
pub fn with_performer<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.parameters.performer = Some(value.into());
self
}
pub fn with_protect_content(mut self, value: bool) -> Self {
self.parameters.protect_content = Some(value);
self
}
pub fn with_receiver_user_id(mut self, value: Integer) -> Self {
self.parameters.receiver_user_id = Some(value);
self
}
pub fn with_title<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.parameters.title = Some(value.into());
self
}
pub fn with_thumbnail_file<T>(mut self, value: T) -> Self
where
T: Into<InputFileReader>,
{
self.thumbnail = Some(value.into());
self.parameters.thumbnail = None;
self
}
pub fn with_thumbnail_url<T>(mut self, value: T) -> Self
where
T: Into<String>,
{
self.parameters.thumbnail = Some(value.into());
self.thumbnail = None;
self
}
pub fn with_reply_markup<T>(mut self, value: T) -> Self
where
T: Into<ReplyMarkup>,
{
self.parameters.reply_markup = Some(value.into());
self
}
pub fn with_reply_parameters(mut self, value: ReplyParameters) -> Self {
self.parameters.reply_parameters = Some(value);
self
}
pub fn with_suggested_post_parameters(mut self, value: SuggestedPostParameters) -> Self {
self.parameters.suggested_post_parameters = Some(value);
self
}
}
#[serde_with::skip_serializing_none]
#[derive(Debug, Default, Serialize)]
struct SendAudioParameters {
allow_paid_broadcast: Option<bool>,
audio: Option<String>,
business_connection_id: Option<String>,
callback_query_id: Option<String>,
#[serde(flatten)]
caption: Option<InputTextCaption>,
chat_id: Option<ChatId>,
direct_messages_topic_id: Option<Integer>,
disable_notification: Option<bool>,
duration: Option<Integer>,
message_effect_id: Option<String>,
message_thread_id: Option<Integer>,
performer: Option<String>,
protect_content: Option<bool>,
receiver_user_id: Option<Integer>,
reply_markup: Option<ReplyMarkup>,
reply_parameters: Option<ReplyParameters>,
suggested_post_parameters: Option<SuggestedPostParameters>,
thumbnail: Option<String>,
title: Option<String>,
}
impl Method for SendAudio {
type Response = Message;
fn into_payload(self) -> Result<Payload, PayloadError> {
let Self {
audio,
thumbnail,
mut parameters,
} = self;
let mut form = Form::default();
parameters.audio = Some(audio.write(&mut form));
parameters.thumbnail = parameters.thumbnail.or_else(|| thumbnail.map(|x| x.write(&mut form)));
parameters.serialize(&mut form)?;
Payload::form("sendAudio", form)
}
}