tbot/methods/
send_audio.rs1use super::call_method;
2#[allow(deprecated)]
3use crate::{
4 connectors::Client,
5 errors, token,
6 types::{
7 input_file::{Audio, InputFile, Thumb},
8 keyboard,
9 message::{self, Message},
10 parameters::{ChatId, ImplicitChatId, NotificationState},
11 },
12 Multipart,
13};
14
15#[derive(Debug, Clone)]
21#[must_use = "methods do nothing unless turned into a future"]
22pub struct SendAudio<'a> {
23 client: &'a Client,
24 token: token::Ref<'a>,
25 chat_id: ChatId<'a>,
26 audio: Audio<'a>,
27 disable_notification: Option<bool>,
28 reply_to_message_id: Option<message::Id>,
29 reply_markup: Option<keyboard::Any<'a>>,
30}
31
32impl<'a> SendAudio<'a> {
33 pub(crate) fn new(
34 client: &'a Client,
35 token: token::Ref<'a>,
36 chat_id: impl ImplicitChatId<'a>,
37 audio: Audio<'a>,
38 ) -> Self {
39 Self {
40 client,
41 token,
42 chat_id: chat_id.into(),
43 audio,
44 disable_notification: None,
45 reply_to_message_id: None,
46 reply_markup: None,
47 }
48 }
49
50 pub fn is_notification_disabled(mut self, is_disabled: bool) -> Self {
53 self.disable_notification = Some(is_disabled);
54 self
55 }
56
57 #[doc(hidden)]
58 #[deprecated(
59 since = "0.6.6",
60 note = "use `is_notification_disabled` which takes a `bool`"
61 )]
62 #[allow(deprecated)]
63 pub fn notification(self, state: NotificationState) -> Self {
64 self.is_notification_disabled(state.is_disabled())
65 }
66
67 pub fn in_reply_to(mut self, id: message::Id) -> Self {
70 self.reply_to_message_id = Some(id);
71 self
72 }
73
74 #[doc(hidden)]
75 #[deprecated(
76 since = "0.6.6",
77 note = "this method is renamed to `in_reply_to`"
78 )]
79 pub fn reply_to_message_id(self, id: message::Id) -> Self {
80 self.in_reply_to(id)
81 }
82
83 pub fn reply_markup(
86 mut self,
87 markup: impl Into<keyboard::Any<'a>>,
88 ) -> Self {
89 self.reply_markup = Some(markup.into());
90 self
91 }
92}
93
94impl SendAudio<'_> {
95 pub async fn call(self) -> Result<Message, errors::MethodCall> {
97 let mut multipart = Multipart::new(11)
98 .chat_id("chat_id", self.chat_id)
99 .maybe_string("duration", self.audio.duration)
100 .maybe_str("caption", self.audio.caption)
101 .maybe_str("performer", self.audio.performer)
102 .maybe_str("title", self.audio.title)
103 .maybe_json("parse_mode", self.audio.parse_mode)
104 .maybe_string("disable_notification", self.disable_notification)
105 .maybe_string("reply_to_message_id", self.reply_to_message_id)
106 .maybe_json("reply_markup", self.reply_markup);
107
108 match self.audio.media {
109 InputFile::File {
110 filename, bytes, ..
111 } => multipart = multipart.file("audio", filename, bytes),
112 InputFile::Id(audio) | InputFile::Url(audio) => {
113 multipart = multipart.str("audio", audio);
114 }
115 }
116
117 if let Some(Thumb(InputFile::File {
118 filename, bytes, ..
119 })) = self.audio.thumb
120 {
121 multipart = multipart.file("thumb", filename, bytes);
122 }
123
124 let (boundary, body) = multipart.finish();
125
126 call_method(self.client, self.token, "sendAudio", Some(boundary), body)
127 .await
128 }
129}