telegram_bot_raw/requests/
answer_callback_query.rs

1use std::borrow::Cow;
2use std::ops::Not;
3
4use crate::requests::*;
5use crate::types::*;
6
7/// Use this method to send answers to callback queries sent from inline keyboards.
8/// The answer will be displayed to the user as a notification at the top of
9/// the chat screen or as an alert.
10#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
11#[must_use = "requests do nothing unless sent"]
12pub struct AnswerCallbackQuery<'t> {
13    callback_query_id: CallbackQueryId,
14    #[serde(skip_serializing_if = "Option::is_none")]
15    text: Option<Cow<'t, str>>,
16    #[serde(skip_serializing_if = "Not::not")]
17    show_alert: bool,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    url: Option<Cow<'t, str>>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    cache_time: Option<i64>,
22}
23
24impl<'i, 't> Request for AnswerCallbackQuery<'t> {
25    type Type = JsonRequestType<Self>;
26    type Response = JsonTrueToUnitResponse;
27
28    fn serialize(&self) -> Result<HttpRequest, Error> {
29        Self::Type::serialize(RequestUrl::method("answerCallbackQuery"), self)
30    }
31}
32
33impl<'t> AnswerCallbackQuery<'t> {
34    fn new<Q, T>(query: Q, text: T) -> Self
35    where
36        Q: ToCallbackQueryId,
37        T: Into<Cow<'t, str>>,
38    {
39        Self {
40            callback_query_id: query.to_callback_query_id(),
41            text: Some(text.into()),
42            show_alert: false,
43            url: None,
44            cache_time: None,
45        }
46    }
47
48    fn acknowledge<Q>(query: Q) -> Self
49    where
50        Q: ToCallbackQueryId,
51    {
52        Self {
53            callback_query_id: query.to_callback_query_id(),
54            text: None,
55            show_alert: false,
56            url: None,
57            cache_time: None,
58        }
59    }
60
61    /// An alert will be shown by the client instead of a notification
62    /// at the top of the chat screen.
63    pub fn show_alert(&mut self) -> &mut Self {
64        self.show_alert = true;
65        self
66    }
67
68    /// URL that will be opened by the user's client. If you have created a
69    /// Game and accepted the conditions via @Botfather, specify the URL
70    /// that opens your game – note that this will only work if the query
71    /// comes from a callback_game button.
72    ///
73    /// Otherwise, you may use links like t.me/your_bot?start=XXXX that open your bot with a parameter.
74    pub fn url<T>(&mut self, url: T) -> &mut Self
75    where
76        T: Into<Cow<'t, str>>,
77    {
78        self.url = Some(url.into());
79        self
80    }
81
82    /// The maximum amount of time in seconds that the result of the callback query
83    /// may be cached client-side. Telegram apps will support caching starting in
84    /// version 3.14. Defaults to 0.
85    pub fn cache_time(&mut self, time: i64) -> &mut Self {
86        self.cache_time = Some(time);
87        self
88    }
89}
90
91/// Send answers to callback queries sent from inline keyboards.
92pub trait CanAnswerCallbackQuery {
93    fn answer<'t, T>(&self, text: T) -> AnswerCallbackQuery<'t>
94    where
95        T: Into<Cow<'t, str>>;
96    fn acknowledge<'t>(&self) -> AnswerCallbackQuery<'t>;
97}
98
99impl<Q> CanAnswerCallbackQuery for Q
100where
101    Q: ToCallbackQueryId,
102{
103    fn answer<'t, T>(&self, text: T) -> AnswerCallbackQuery<'t>
104    where
105        T: Into<Cow<'t, str>>,
106    {
107        AnswerCallbackQuery::new(&self, text)
108    }
109    fn acknowledge<'t>(&self) -> AnswerCallbackQuery<'t> {
110        AnswerCallbackQuery::acknowledge(&self)
111    }
112}