tgbot 0.47.0

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

use crate::{
    api::{Method, Payload, PayloadError},
    types::{Integer, MaybeInaccessibleMessage, User},
};

/// Represents an incoming callback query from a callback button in an inline keyboard.
///
/// If the button that originated the query was attached to a message sent by the bot,
/// the field message will be present.
///
/// If the button was attached to a message sent via the bot (in inline mode),
/// the field `inline_message_id` will be present.
///
/// Exactly one of the fields data or `game_short_name` will be present.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CallbackQuery {
    /// Sender of the query.
    pub from: User,
    /// Unique identifier of the query.
    pub id: String,
    /// Global identifier, uniquely corresponding
    /// to the chat to which the message with the
    /// callback button was sent.
    ///
    /// Useful for high scores in games.
    pub chat_instance: Option<String>,
    /// Data associated with the callback button.
    ///
    /// Be aware that a bad client can send arbitrary data in this field.
    pub data: Option<String>,
    /// Short name of a Game to be returned,
    /// serves as the unique identifier for the game.
    pub game_short_name: Option<String>,
    /// Identifier of the message sent via the bot
    /// in inline mode, that originated the query.
    pub inline_message_id: Option<String>,
    /// Message with the callback button that originated the query.
    ///
    /// Note that message content and message date
    /// will not be available if the message is too old.
    pub message: Option<MaybeInaccessibleMessage>,
}

/// Sends an answer to a callback query sent from an inline keyboard.
///
/// The answer will be displayed to the user as a notification at the top of the chat screen or as an alert.
///
/// Alternatively, the user can be redirected to the specified Game URL.
///
/// For this option to work, you must first create a game for your bot via Bot Father and accept the terms.
///
/// Otherwise, you may use links like `t.me/your_bot?start=XXX` that open your bot with a parameter.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct AnswerCallbackQuery {
    callback_query_id: String,
    cache_time: Option<Integer>,
    show_alert: Option<bool>,
    text: Option<String>,
    url: Option<String>,
}

impl AnswerCallbackQuery {
    /// Creates a new `AnswerCallbackQuery`.
    ///
    /// # Arguments
    ///
    /// * `callback_query_id` - Unique identifier of the query to be answered.
    pub fn new<T>(callback_query_id: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            callback_query_id: callback_query_id.into(),
            cache_time: None,
            show_alert: None,
            text: None,
            url: None,
        }
    }

    /// Sets a new cache time.
    ///
    /// # Arguments
    ///
    /// * `value` - The maximum amount of time in seconds that the result
    ///   of the callback query may be cached client-side;
    ///   telegram apps will support caching starting in version 3.14;
    ///   default - 0.
    pub fn with_cache_time(mut self, value: Integer) -> Self {
        self.cache_time = Some(value);
        self
    }

    /// Sets a new value for the `short_alert` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - An alert will be shown by the client instead
    ///   of a notification at the top of the chat screen;
    ///   default - `false`.
    pub fn with_show_alert(mut self, value: bool) -> Self {
        self.show_alert = Some(value);
        self
    }

    /// Sets a new text.
    ///
    /// # Arguments
    ///
    /// * `value` - Text of the notification;
    ///   if not specified, nothing will be shown to the user;
    ///   0-200 characters.
    pub fn with_text<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.text = Some(value.into());
        self
    }

    /// Sets a new URL.
    ///
    /// # Arguments
    ///
    /// * `value` - URL that will be opened by the user's client.
    ///
    /// If you have created a game and accepted the conditions via Bot Father,
    /// specify the URL that opens your game – note that this will only work
    /// if the query comes from a callback game button.
    ///
    /// Otherwise, you may use links like `t.me/your_bot?start=XXX`
    /// that open your bot with a parameter.
    pub fn with_url<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.url = Some(value.into());
        self
    }
}

impl Method for AnswerCallbackQuery {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("answerCallbackQuery", self)
    }
}