tgbot 0.47.0

A Telegram Bot library
Documentation
use serde::{Deserialize, Serialize};

/// Represents an HTTP link.
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct Link {
    /// URL of the link.
    pub url: String,
}

/// Link preview media size
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum LinkPreviewMediaSize {
    /// The media in the link preview is suppposed to be enlarged.
    Large,
    /// The media in the link preview is suppposed to be shrunk.
    Small,
}

/// Represents the options used for link preview generation.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Default, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct LinkPreviewOptions {
    /// Whether the link preview is disabled.
    pub is_disabled: Option<bool>,
    /// Whether the media in the link preview is suppposed to be enlarged.
    pub prefer_large_media: Option<bool>,
    /// Whether the media in the link preview is suppposed to be shrunk.
    pub prefer_small_media: Option<bool>,
    /// Whether the link preview must be shown above the message text.
    pub show_above_text: Option<bool>,
    /// URL to use for the link preview.
    pub url: Option<String>,
}

impl LinkPreviewOptions {
    /// Creates a new `LinkPreviewOptions` with `is_disabled` flag set to `true`.
    pub fn disabled() -> Self {
        Self {
            is_disabled: Some(true),
            ..Default::default()
        }
    }

    /// Sets the media size.
    ///
    /// # Arguments
    ///
    /// * `value` - Size of the media in the link preview;
    ///   ignored if the URL isn't explicitly specified or media size change isn't supported for the preview.
    pub fn with_media_size(mut self, value: LinkPreviewMediaSize) -> Self {
        match value {
            LinkPreviewMediaSize::Large => {
                self.prefer_large_media = Some(true);
                self.prefer_small_media = None;
            }
            LinkPreviewMediaSize::Small => {
                self.prefer_large_media = None;
                self.prefer_small_media = Some(true);
            }
        }
        self
    }

    /// Sets a new value for the `show_above_text` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the link preview must be shown above the message text;
    ///   otherwise, the link preview will be shown below the message text.
    pub fn with_show_above_text(mut self, value: bool) -> Self {
        self.show_above_text = Some(value);
        self
    }

    /// Sets a new URL.
    ///
    /// # Arguments
    ///
    /// * `value` - URL to use for the link preview.
    ///
    /// If empty, then the first URL found in the message text will be used.
    pub fn with_url<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.url = Some(value.into());
        self
    }
}