Skip to main content

rustigram_api/methods/
getters.rs

1use crate::client::BotClient;
2use crate::error::Result;
3use rustigram_types::chat::ChatFullInfo;
4use rustigram_types::chat_member::ChatMember;
5use rustigram_types::file::File;
6use rustigram_types::user::{ChatId, User, UserProfilePhotos};
7use serde::Serialize;
8use std::future::{Future, IntoFuture};
9use std::pin::Pin;
10
11macro_rules! simple_getter {
12    ($(#[$doc:meta])* $name:ident, $params_ty:ty, $return_ty:ty, $method:literal) => {
13        $(#[$doc])*
14        pub struct $name {
15            client: BotClient,
16            params: $params_ty,
17        }
18        impl IntoFuture for $name {
19            type Output = Result<$return_ty>;
20            type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
21            fn into_future(self) -> Self::IntoFuture {
22                Box::pin(async move { self.client.post_json($method, &self.params).await })
23            }
24        }
25    };
26}
27
28/// Builder for the [`getMe`](https://core.telegram.org/bots/api#getme) method.
29pub struct GetMe {
30    client: BotClient,
31}
32impl GetMe {
33    pub(crate) fn new(client: BotClient) -> Self {
34        Self { client }
35    }
36}
37impl IntoFuture for GetMe {
38    type Output = Result<User>;
39    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
40    fn into_future(self) -> Self::IntoFuture {
41        Box::pin(async move { self.client.post_json("getMe", &serde_json::json!({})).await })
42    }
43}
44
45#[derive(Serialize)]
46struct GetChatParams {
47    chat_id: ChatId,
48}
49simple_getter!(
50    /// Builder for the [`getChat`](https://core.telegram.org/bots/api#getchat) method.
51    GetChat,
52    GetChatParams,
53    ChatFullInfo,
54    "getChat"
55);
56impl GetChat {
57    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
58        Self {
59            client,
60            params: GetChatParams {
61                chat_id: chat_id.into(),
62            },
63        }
64    }
65}
66
67#[derive(Serialize)]
68struct GetChatAdministratorsParams {
69    chat_id: ChatId,
70    #[serde(skip_serializing_if = "Option::is_none")]
71    return_bots: Option<bool>,
72}
73simple_getter!(
74    /// Builder for the [`getChatAdministrators`](https://core.telegram.org/bots/api#getchatadministrators) method.
75    GetChatAdministrators,
76    GetChatAdministratorsParams,
77    Vec<ChatMember>,
78    "getChatAdministrators"
79);
80impl GetChatAdministrators {
81    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
82        Self {
83            client,
84            params: GetChatAdministratorsParams {
85                chat_id: chat_id.into(),
86                return_bots: None,
87            },
88        }
89    }
90
91    /// Pass `true` to additionally receive all bots that are administrators of the chat.
92    /// By default, bots other than the current bot are omitted.
93    pub fn return_bots(mut self, v: bool) -> Self {
94        self.params.return_bots = Some(v);
95        self
96    }
97}
98
99#[derive(Serialize)]
100struct GetChatMemberCountParams {
101    chat_id: ChatId,
102}
103simple_getter!(
104    /// Builder for the [`getChatMemberCount`](https://core.telegram.org/bots/api#getchatmembercount) method.
105    GetChatMemberCount,
106    GetChatMemberCountParams,
107    u32,
108    "getChatMemberCount"
109);
110impl GetChatMemberCount {
111    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>) -> Self {
112        Self {
113            client,
114            params: GetChatMemberCountParams {
115                chat_id: chat_id.into(),
116            },
117        }
118    }
119}
120
121#[derive(Serialize)]
122struct GetChatMemberParams {
123    chat_id: ChatId,
124    user_id: i64,
125}
126simple_getter!(
127    /// Builder for the [`getChatMember`](https://core.telegram.org/bots/api#getchatmember) method.
128    GetChatMember,
129    GetChatMemberParams,
130    ChatMember,
131    "getChatMember"
132);
133impl GetChatMember {
134    pub(crate) fn new(client: BotClient, chat_id: impl Into<ChatId>, user_id: i64) -> Self {
135        Self {
136            client,
137            params: GetChatMemberParams {
138                chat_id: chat_id.into(),
139                user_id,
140            },
141        }
142    }
143}
144
145#[derive(Serialize)]
146struct GetFileParams {
147    file_id: String,
148}
149simple_getter!(
150    /// Builder for the [`getFile`](https://core.telegram.org/bots/api#getfile) method.
151    GetFile, GetFileParams, File, "getFile");
152impl GetFile {
153    pub(crate) fn new(client: BotClient, file_id: impl Into<String>) -> Self {
154        Self {
155            client,
156            params: GetFileParams {
157                file_id: file_id.into(),
158            },
159        }
160    }
161}
162
163#[derive(Serialize)]
164struct GetUserProfilePhotosParams {
165    user_id: i64,
166    #[serde(skip_serializing_if = "Option::is_none")]
167    offset: Option<u32>,
168    #[serde(skip_serializing_if = "Option::is_none")]
169    limit: Option<u8>,
170}
171
172/// Builder for the [`getUserProfilePhotos`](https://core.telegram.org/bots/api#getuserprofilephotos) method.
173pub struct GetUserProfilePhotos {
174    client: BotClient,
175    params: GetUserProfilePhotosParams,
176}
177impl GetUserProfilePhotos {
178    pub(crate) fn new(client: BotClient, user_id: i64) -> Self {
179        Self {
180            client,
181            params: GetUserProfilePhotosParams {
182                user_id,
183                offset: None,
184                limit: None,
185            },
186        }
187    }
188    /// Skips the first N profile photos.
189    pub fn offset(mut self, v: u32) -> Self {
190        self.params.offset = Some(v);
191        self
192    }
193    /// Limits the number of photos returned (1–100).
194    pub fn limit(mut self, v: u8) -> Self {
195        self.params.limit = Some(v);
196        self
197    }
198}
199impl IntoFuture for GetUserProfilePhotos {
200    type Output = Result<UserProfilePhotos>;
201    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
202    fn into_future(self) -> Self::IntoFuture {
203        Box::pin(async move {
204            self.client
205                .post_json("getUserProfilePhotos", &self.params)
206                .await
207        })
208    }
209}
210
211// ─── getUserProfileAudios ─────────────────────────────────────────────────────
212
213#[derive(Serialize)]
214struct GetUserProfileAudiosParams {
215    user_id: i64,
216    #[serde(skip_serializing_if = "Option::is_none")]
217    offset: Option<u32>,
218    #[serde(skip_serializing_if = "Option::is_none")]
219    limit: Option<u8>,
220}
221
222/// Builder for the [`getUserProfileAudios`](https://core.telegram.org/bots/api#getuserprofileaudios) method (Bot API 9.4).
223///
224/// Returns the list of audios displayed on a user's profile.
225pub struct GetUserProfileAudios {
226    client: BotClient,
227    params: GetUserProfileAudiosParams,
228}
229
230impl GetUserProfileAudios {
231    pub(crate) fn new(client: BotClient, user_id: i64) -> Self {
232        Self {
233            client,
234            params: GetUserProfileAudiosParams {
235                user_id,
236                offset: None,
237                limit: None,
238            },
239        }
240    }
241    /// Skips the first N profile audios.
242    pub fn offset(mut self, v: u32) -> Self {
243        self.params.offset = Some(v);
244        self
245    }
246    /// Limits the number of audios returned (1–100, default 100).
247    pub fn limit(mut self, v: u8) -> Self {
248        self.params.limit = Some(v);
249        self
250    }
251}
252
253impl IntoFuture for GetUserProfileAudios {
254    type Output = Result<rustigram_types::user::UserProfileAudios>;
255    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
256    fn into_future(self) -> Self::IntoFuture {
257        Box::pin(async move {
258            self.client
259                .post_json("getUserProfileAudios", &self.params)
260                .await
261        })
262    }
263}
264
265// ─── getUserPersonalChatMessages ──────────────────────────────────────────────
266
267#[derive(Serialize)]
268struct GetUserPersonalChatMessagesParams {
269    user_id: i64,
270    limit: u32,
271}
272
273/// Builder for the [`getUserPersonalChatMessages`](https://core.telegram.org/bots/api#getuserpersonalchatmessages) method (Bot API 9.7).
274///
275/// Returns the last messages from the personal chat of a given user. Limit must be 1–20.
276pub struct GetUserPersonalChatMessages {
277    client: BotClient,
278    params: GetUserPersonalChatMessagesParams,
279}
280
281impl GetUserPersonalChatMessages {
282    pub(crate) fn new(client: BotClient, user_id: i64, limit: u32) -> Self {
283        Self {
284            client,
285            params: GetUserPersonalChatMessagesParams { user_id, limit },
286        }
287    }
288}
289
290impl IntoFuture for GetUserPersonalChatMessages {
291    type Output = Result<Vec<rustigram_types::message::Message>>;
292    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
293    fn into_future(self) -> Self::IntoFuture {
294        Box::pin(async move {
295            self.client
296                .post_json("getUserPersonalChatMessages", &self.params)
297                .await
298        })
299    }
300}