use serde::{Deserialize, Serialize};
use crate::{
api::{Form, Method, Payload, PayloadError, WriteForm},
types::{
ChatId,
InputFile,
InputTextCaption,
Integer,
Message,
ReplyMarkup,
ReplyParameters,
SuggestedPostParameters,
},
};
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct Voice {
pub duration: Integer,
pub file_id: String,
pub file_unique_id: String,
pub file_size: Option<Integer>,
pub mime_type: Option<String>,
}
#[derive(Debug)]
pub struct SendVoice {
voice: InputFile,
parameters: SendVoiceParameters,
}
impl SendVoice {
pub fn new<A, B>(chat_id: A, voice: B) -> Self
where
A: Into<ChatId>,
B: Into<InputFile>,
{
Self {
voice: voice.into(),
parameters: SendVoiceParameters {
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_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_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 SendVoiceParameters {
allow_paid_broadcast: Option<bool>,
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>,
protect_content: Option<bool>,
receiver_user_id: Option<Integer>,
reply_markup: Option<ReplyMarkup>,
reply_parameters: Option<ReplyParameters>,
suggested_post_parameters: Option<SuggestedPostParameters>,
voice: Option<String>,
}
impl Method for SendVoice {
type Response = Message;
fn into_payload(self) -> Result<Payload, PayloadError> {
let Self { voice, mut parameters } = self;
let mut form = Form::default();
parameters.voice = Some(voice.write(&mut form));
parameters.serialize(&mut form)?;
Payload::form("sendVoice", form)
}
}