telegram_bot_raw/requests/
answer_callback_query.rs1use std::borrow::Cow;
2use std::ops::Not;
3
4use crate::requests::*;
5use crate::types::*;
6
7#[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 pub fn show_alert(&mut self) -> &mut Self {
64 self.show_alert = true;
65 self
66 }
67
68 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 pub fn cache_time(&mut self, time: i64) -> &mut Self {
86 self.cache_time = Some(time);
87 self
88 }
89}
90
91pub 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}