telegram_bot_fork_raw/requests/
answer_callback_query.rs

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