Skip to main content

rustigram_api/methods/
inline.rs

1use crate::client::BotClient;
2use rustigram_types::inline::InlineQueryResult;
3use serde::Serialize;
4use std::future::{Future, IntoFuture};
5use std::pin::Pin;
6
7#[derive(Serialize)]
8struct AnswerInlineQueryParams {
9    inline_query_id: String,
10    results: Vec<InlineQueryResult>,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    cache_time: Option<u32>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    is_personal: Option<bool>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    next_offset: Option<String>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    button: Option<serde_json::Value>,
19}
20
21/// Builder for the [`answerInlineQuery`](https://core.telegram.org/bots/api#answerinlinequery) method.
22pub struct AnswerInlineQuery {
23    client: BotClient,
24    params: AnswerInlineQueryParams,
25}
26impl AnswerInlineQuery {
27    pub(crate) fn new(
28        client: BotClient,
29        inline_query_id: impl Into<String>,
30        results: Vec<InlineQueryResult>,
31    ) -> Self {
32        Self {
33            client,
34            params: AnswerInlineQueryParams {
35                inline_query_id: inline_query_id.into(),
36                results,
37                cache_time: None,
38                is_personal: None,
39                next_offset: None,
40                button: None,
41            },
42        }
43    }
44    /// Sets how many seconds the results may be cached on the client (default 300).
45    pub fn cache_time(mut self, secs: u32) -> Self {
46        self.params.cache_time = Some(secs);
47        self
48    }
49    /// Makes the results personal to the user — disables shared caching.
50    pub fn is_personal(mut self, v: bool) -> Self {
51        self.params.is_personal = Some(v);
52        self
53    }
54    /// Sets the offset for pagination when there are more results available.
55    pub fn next_offset(mut self, o: impl Into<String>) -> Self {
56        self.params.next_offset = Some(o.into());
57        self
58    }
59}
60impl IntoFuture for AnswerInlineQuery {
61    type Output = crate::error::Result<bool>;
62    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
63    fn into_future(self) -> Self::IntoFuture {
64        Box::pin(async move {
65            self.client
66                .post_json("answerInlineQuery", &self.params)
67                .await
68        })
69    }
70}
71
72// ─── answerWebAppQuery ────────────────────────────────────────────────────────
73
74#[derive(Serialize)]
75struct AnswerWebAppQueryParams {
76    web_app_query_id: String,
77    result: InlineQueryResult,
78}
79
80/// Builder for the [`answerWebAppQuery`](https://core.telegram.org/bots/api#answerwebappquery) method.
81///
82/// Sets the result of an interaction with a Web App and sends the corresponding
83/// message on behalf of the user to the originating chat.
84/// Returns a `SentWebAppMessage` object.
85pub struct AnswerWebAppQuery {
86    client: BotClient,
87    params: AnswerWebAppQueryParams,
88}
89
90impl AnswerWebAppQuery {
91    pub(crate) fn new(
92        client: BotClient,
93        web_app_query_id: impl Into<String>,
94        result: InlineQueryResult,
95    ) -> Self {
96        Self {
97            client,
98            params: AnswerWebAppQueryParams {
99                web_app_query_id: web_app_query_id.into(),
100                result,
101            },
102        }
103    }
104}
105
106impl IntoFuture for AnswerWebAppQuery {
107    /// Returns `SentWebAppMessage` as `serde_json::Value` until the type is defined in Priority 4.
108    type Output = crate::error::Result<serde_json::Value>;
109    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
110    fn into_future(self) -> Self::IntoFuture {
111        Box::pin(async move {
112            self.client
113                .post_json("answerWebAppQuery", &self.params)
114                .await
115        })
116    }
117}
118
119// ─── savePreparedInlineMessage ────────────────────────────────────────────────
120
121#[derive(Serialize)]
122struct SavePreparedInlineMessageParams {
123    user_id: i64,
124    result: InlineQueryResult,
125    #[serde(skip_serializing_if = "Option::is_none")]
126    allow_user_chats: Option<bool>,
127    #[serde(skip_serializing_if = "Option::is_none")]
128    allow_bot_chats: Option<bool>,
129    #[serde(skip_serializing_if = "Option::is_none")]
130    allow_group_chats: Option<bool>,
131    #[serde(skip_serializing_if = "Option::is_none")]
132    allow_channel_chats: Option<bool>,
133}
134
135/// Builder for the [`savePreparedInlineMessage`](https://core.telegram.org/bots/api#savepreparedinlinemessage) method.
136///
137/// Stores a message that can be sent by a user from a Mini App.
138/// Returns a `PreparedInlineMessage` object as `serde_json::Value` until the type
139/// is defined in Priority 4.
140pub struct SavePreparedInlineMessage {
141    client: BotClient,
142    params: SavePreparedInlineMessageParams,
143}
144
145impl SavePreparedInlineMessage {
146    pub(crate) fn new(client: BotClient, user_id: i64, result: InlineQueryResult) -> Self {
147        Self {
148            client,
149            params: SavePreparedInlineMessageParams {
150                user_id,
151                result,
152                allow_user_chats: None,
153                allow_bot_chats: None,
154                allow_group_chats: None,
155                allow_channel_chats: None,
156            },
157        }
158    }
159    /// Allows the message to be sent to private chats with users.
160    pub fn allow_user_chats(mut self, v: bool) -> Self {
161        self.params.allow_user_chats = Some(v);
162        self
163    }
164    /// Allows the message to be sent to private chats with bots.
165    pub fn allow_bot_chats(mut self, v: bool) -> Self {
166        self.params.allow_bot_chats = Some(v);
167        self
168    }
169    /// Allows the message to be sent to group and supergroup chats.
170    pub fn allow_group_chats(mut self, v: bool) -> Self {
171        self.params.allow_group_chats = Some(v);
172        self
173    }
174    /// Allows the message to be sent to channel chats.
175    pub fn allow_channel_chats(mut self, v: bool) -> Self {
176        self.params.allow_channel_chats = Some(v);
177        self
178    }
179}
180
181impl IntoFuture for SavePreparedInlineMessage {
182    /// Returns `PreparedInlineMessage` as `serde_json::Value` until the type is defined in Priority 4.
183    type Output = crate::error::Result<serde_json::Value>;
184    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
185    fn into_future(self) -> Self::IntoFuture {
186        Box::pin(async move {
187            self.client
188                .post_json("savePreparedInlineMessage", &self.params)
189                .await
190        })
191    }
192}
193
194// ─── answerGuestQuery ─────────────────────────────────────────────────────────
195
196#[derive(Serialize)]
197struct AnswerGuestQueryParams {
198    guest_query_id: String,
199    result: rustigram_types::inline::InlineQueryResult,
200}
201
202/// Builder for the [`answerGuestQuery`](https://core.telegram.org/bots/api#answerguestquery) method.
203///
204/// Replies to a received guest message. Returns a [`SentGuestMessage`](rustigram_types::inline::SentGuestMessage)
205/// on success.
206pub struct AnswerGuestQuery {
207    client: crate::client::BotClient,
208    params: AnswerGuestQueryParams,
209}
210
211impl AnswerGuestQuery {
212    pub(crate) fn new(
213        client: crate::client::BotClient,
214        guest_query_id: impl Into<String>,
215        result: rustigram_types::inline::InlineQueryResult,
216    ) -> Self {
217        Self {
218            client,
219            params: AnswerGuestQueryParams {
220                guest_query_id: guest_query_id.into(),
221                result,
222            },
223        }
224    }
225}
226
227impl std::future::IntoFuture for AnswerGuestQuery {
228    type Output = crate::error::Result<rustigram_types::inline::SentGuestMessage>;
229    type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output> + Send>>;
230    fn into_future(self) -> Self::IntoFuture {
231        Box::pin(async move {
232            self.client
233                .post_json("answerGuestQuery", &self.params)
234                .await
235        })
236    }
237}