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