vaegram 0.3.0

Telegram MTProto client library for users and bots
Documentation
use crate::Client;
use crate::errors::Result;
use crate::methods::accepted;
use crate::raw::{base, functions, types};
use crate::types::{InlineQueryResult, InlineQueryResultsButton};

/// Request built by [`Client::answer_inline_query`].
#[must_use = "a request does nothing until it is awaited"]
pub struct AnswerInlineQuery<'a> {
    client: &'a Client,
    query_id: i64,
    results: Vec<InlineQueryResult>,
    cache_time: i32,
    personal: bool,
    gallery: bool,
    next_offset: Option<String>,
    button: Option<InlineQueryResultsButton>,
}

impl Client {
    /// Answers an inline query with a list of results.
    ///
    /// Telegram caches the results for 300 seconds unless [`cache_time`](AnswerInlineQuery::cache_time) says
    /// otherwise.
    pub fn answer_inline_query<R: Into<InlineQueryResult>>(
        &self,
        query_id: i64,
        results: impl IntoIterator<Item = R>,
    ) -> AnswerInlineQuery<'_> {
        AnswerInlineQuery {
            client: self,
            query_id,
            results: results.into_iter().map(Into::into).collect(),
            cache_time: 300,
            personal: false,
            gallery: false,
            next_offset: None,
            button: None,
        }
    }
}

impl AnswerInlineQuery<'_> {
    /// How long, in seconds, Telegram may cache the results. Defaults to 300.
    pub fn cache_time(mut self, seconds: i32) -> Self {
        self.cache_time = seconds;
        self
    }

    /// Caches the results only for the user who sent the query, not for everyone.
    pub fn personal(mut self) -> Self {
        self.personal = true;
        self
    }

    /// Shows the results as a gallery instead of a list.
    pub fn gallery(mut self) -> Self {
        self.gallery = true;
        self
    }

    /// Sets the offset the client sends back in the next query to load more results.
    pub fn next_offset(mut self, offset: impl Into<String>) -> Self {
        self.next_offset = Some(offset.into());
        self
    }

    /// Shows a button above the results that opens the bot's chat or a Mini App.
    pub fn button(mut self, button: InlineQueryResultsButton) -> Self {
        self.button = Some(button);
        self
    }

    async fn send(self) -> Result<()> {
        let mut results = Vec::with_capacity(self.results.len());
        for r in self.results {
            results.push(r.write(self.client).await?);
        }
        let (switch_pm, switch_webview) = match self.button {
            Some(InlineQueryResultsButton::Start { text, start_parameter }) => {
                let pm = types::InlineBotSwitchPM { text, start_param: start_parameter };
                (Some(base::InlineBotSwitchPM::InlineBotSwitchPM(Box::new(pm))), None)
            }
            Some(InlineQueryResultsButton::WebApp { text, url }) => {
                let web_view = types::InlineBotWebView { text, url };
                (None, Some(base::InlineBotWebView::InlineBotWebView(Box::new(web_view))))
            }
            None => (None, None),
        };
        let request = functions::messages::SetInlineBotResults {
            gallery: self.gallery,
            private: self.personal,
            query_id: self.query_id,
            results,
            cache_time: self.cache_time,
            next_offset: self.next_offset,
            switch_pm,
            switch_webview,
        };
        accepted(self.client.invoke(&request).await?)
    }
}

into_future!(AnswerInlineQuery, ());