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