telegram_bot_async_raw/requests/
answer_callback_query.rs

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