use super::send_options::{SendOptions, caption_setters, send_builder};
use crate::{
Client,
enums::ParseMode,
errors::Result,
raw::{base, types},
types::{Message, PeerRef, Text},
};
#[must_use = "a request does nothing until it is awaited"]
pub struct SendWidget<'a> {
client: &'a Client,
chat_id: PeerRef,
html: String,
width: i32,
height: i32,
public_data: Option<String>,
chat_data: Option<String>,
allow_queries: bool,
shared_widget: bool,
caption: Text,
parse_mode: Option<ParseMode>,
options: SendOptions,
}
impl Client {
pub fn send_widget(&self, chat_id: impl Into<PeerRef>, html: impl Into<String>) -> SendWidget<'_> {
SendWidget {
client: self,
chat_id: chat_id.into(),
html: html.into(),
width: 0,
height: 0,
public_data: None,
chat_data: None,
allow_queries: false,
shared_widget: false,
caption: Text::new(),
parse_mode: None,
options: SendOptions::default(),
}
}
}
impl SendWidget<'_> {
pub fn width(mut self, width: i32) -> Self {
self.width = width;
self
}
pub fn height(mut self, height: i32) -> Self {
self.height = height;
self
}
pub fn public_data(mut self, data: impl Into<String>) -> Self {
self.public_data = Some(data.into());
self
}
pub fn chat_data(mut self, data: impl Into<String>) -> Self {
self.chat_data = Some(data.into());
self
}
pub fn allow_queries(mut self) -> Self {
self.allow_queries = true;
self
}
pub fn is_shared(mut self) -> Self {
self.shared_widget = true;
self
}
pub fn caption(mut self, caption: impl Into<Text>) -> Self {
self.caption = caption.into();
self
}
pub fn show_caption_above_media(mut self) -> Self {
self.options.invert_media = true;
self
}
async fn send(self) -> Result<Message> {
let media = types::InputMediaWidget {
allow_queries: self.allow_queries,
shared_widget: self.shared_widget,
width: self.width,
height: self.height,
html: self.html,
public_data: self.public_data,
chat_data: self.chat_data,
};
self.client
.send_media(
self.chat_id,
base::InputMedia::InputMediaWidget(Box::new(media)),
self.caption,
self.parse_mode,
self.options,
)
.await
}
}
caption_setters!(SendWidget);
send_builder!(SendWidget);
#[cfg(test)]
mod tests {
use crate::{
Client,
raw::{Serializable, base, types},
};
#[tokio::test]
async fn sends_widget_flags_and_automatic_width() {
let (client, mut sent) = Client::scripted(Vec::new());
let _ = client.send_widget(-5, "<p>x</p>").height(25).allow_queries().is_shared().await;
let body = sent.try_recv().unwrap();
let media = base::InputMedia::InputMediaWidget(Box::new(types::InputMediaWidget {
allow_queries: true,
shared_widget: true,
width: 0,
height: 25,
html: "<p>x</p>".into(),
public_data: None,
chat_data: None,
}))
.to_bytes();
assert!(body.windows(media.len()).any(|w| w == media));
}
}