Skip to main content

tbot/methods/
send_audio.rs

1use 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/// Sends an audio.
16///
17/// Reflects the [`sendAudio`][docs] method.
18///
19/// [docs]: https://core.telegram.org/bots/api#sendaudio
20#[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    /// Configures if the message will be sent silently.
51    /// Reflects the `disable_notification` parameter.
52    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    /// Configures which message this audio is sent in reply to.
68    /// Reflects the `reply_to_message_id` parameter.
69    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    /// Configures a keyboard for the message.
84    /// Reflects the `reply_markup` parameter.
85    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    /// Calls the method.
96    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}