rust-tg-bot-raw 1.0.0-rc.1

Pure Telegram Bot API types and methods for Rust -- a faithful port of python-telegram-bot's core layer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
use super::{input_file_param, push_opt, push_opt_str, Bot, ChatId, Result};
use crate::request::request_parameter::RequestParameter;
use crate::types::{chat_full_info, chat_invite_link, chat_member, chat_permissions, files};

#[allow(dead_code)]
impl Bot {
    // ======================================================================
    // Chat management
    // ======================================================================

    /// Internal raw method to leave a group, supergroup or channel.
    ///
    /// Calls the Telegram `leaveChat` API method.
    pub(crate) async fn leave_chat_raw(&self, chat_id: ChatId) -> Result<bool> {
        let params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        self.do_post("leaveChat", params).await
    }

    /// Internal raw method to get up-to-date information about the chat.
    ///
    /// Calls the Telegram `getChat` API method.
    pub(crate) async fn get_chat_raw(
        &self,
        chat_id: ChatId,
    ) -> Result<chat_full_info::ChatFullInfo> {
        let params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        self.do_post("getChat", params).await
    }

    /// Internal raw method to get a list of administrators in a chat.
    ///
    /// Calls the Telegram `getChatAdministrators` API method.
    pub(crate) async fn get_chat_administrators_raw(
        &self,
        chat_id: ChatId,
    ) -> Result<Vec<chat_member::ChatMember>> {
        let params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        self.do_post("getChatAdministrators", params).await
    }

    /// Internal raw method to get the number of members in a chat.
    ///
    /// Calls the Telegram `getChatMemberCount` API method.
    pub(crate) async fn get_chat_member_count_raw(&self, chat_id: ChatId) -> Result<i64> {
        let params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        self.do_post("getChatMemberCount", params).await
    }

    /// Internal raw method to get information about a member of a chat.
    ///
    /// Calls the Telegram `getChatMember` API method.
    pub(crate) async fn get_chat_member_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
    ) -> Result<chat_member::ChatMember> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
        ];
        self.do_post("getChatMember", params).await
    }

    /// Internal raw method to ban a user in a group, supergroup or channel.
    ///
    /// Calls the Telegram `banChatMember` API method.
    pub(crate) async fn ban_chat_member_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
        until_date: Option<i64>,
        revoke_messages: Option<bool>,
    ) -> Result<bool> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
        ];
        push_opt(&mut params, "until_date", &until_date)?;
        push_opt(&mut params, "revoke_messages", &revoke_messages)?;
        self.do_post("banChatMember", params).await
    }

    /// Internal raw method to unban a previously banned user in a supergroup or channel.
    ///
    /// Calls the Telegram `unbanChatMember` API method.
    pub(crate) async fn unban_chat_member_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
        only_if_banned: Option<bool>,
    ) -> Result<bool> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
        ];
        push_opt(&mut params, "only_if_banned", &only_if_banned)?;
        self.do_post("unbanChatMember", params).await
    }

    /// Internal raw method to ban a channel chat in a supergroup or channel.
    ///
    /// Calls the Telegram `banChatSenderChat` API method.
    pub(crate) async fn ban_chat_sender_chat_raw(
        &self,
        chat_id: ChatId,
        sender_chat_id: i64,
    ) -> Result<bool> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("sender_chat_id", serde_json::to_value(sender_chat_id)?),
        ];
        self.do_post("banChatSenderChat", params).await
    }

    /// Internal raw method to unban a previously banned channel chat.
    ///
    /// Calls the Telegram `unbanChatSenderChat` API method.
    pub(crate) async fn unban_chat_sender_chat_raw(
        &self,
        chat_id: ChatId,
        sender_chat_id: i64,
    ) -> Result<bool> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("sender_chat_id", serde_json::to_value(sender_chat_id)?),
        ];
        self.do_post("unbanChatSenderChat", params).await
    }

    /// Internal raw method to restrict a user in a supergroup.
    ///
    /// Calls the Telegram `restrictChatMember` API method.
    pub(crate) async fn restrict_chat_member_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
        permissions: chat_permissions::ChatPermissions,
        until_date: Option<i64>,
        use_independent_chat_permissions: Option<bool>,
    ) -> Result<bool> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
            RequestParameter::new("permissions", serde_json::to_value(&permissions)?),
        ];
        push_opt(&mut params, "until_date", &until_date)?;
        push_opt(
            &mut params,
            "use_independent_chat_permissions",
            &use_independent_chat_permissions,
        )?;
        self.do_post("restrictChatMember", params).await
    }

    /// Internal raw method to promote or demote a user in a supergroup or channel.
    ///
    /// Calls the Telegram `promoteChatMember` API method.
    pub(crate) async fn promote_chat_member_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
        is_anonymous: Option<bool>,
        can_manage_chat: Option<bool>,
        can_post_messages: Option<bool>,
        can_edit_messages: Option<bool>,
        can_delete_messages: Option<bool>,
        can_manage_video_chats: Option<bool>,
        can_restrict_members: Option<bool>,
        can_promote_members: Option<bool>,
        can_change_info: Option<bool>,
        can_invite_users: Option<bool>,
        can_pin_messages: Option<bool>,
        can_manage_topics: Option<bool>,
        can_post_stories: Option<bool>,
        can_edit_stories: Option<bool>,
        can_delete_stories: Option<bool>,
        can_manage_direct_messages: Option<bool>,
        can_manage_tags: Option<bool>,
    ) -> Result<bool> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
        ];
        push_opt(&mut params, "is_anonymous", &is_anonymous)?;
        push_opt(&mut params, "can_manage_chat", &can_manage_chat)?;
        push_opt(&mut params, "can_post_messages", &can_post_messages)?;
        push_opt(&mut params, "can_edit_messages", &can_edit_messages)?;
        push_opt(&mut params, "can_delete_messages", &can_delete_messages)?;
        push_opt(
            &mut params,
            "can_manage_video_chats",
            &can_manage_video_chats,
        )?;
        push_opt(&mut params, "can_restrict_members", &can_restrict_members)?;
        push_opt(&mut params, "can_promote_members", &can_promote_members)?;
        push_opt(&mut params, "can_change_info", &can_change_info)?;
        push_opt(&mut params, "can_invite_users", &can_invite_users)?;
        push_opt(&mut params, "can_pin_messages", &can_pin_messages)?;
        push_opt(&mut params, "can_manage_topics", &can_manage_topics)?;
        push_opt(&mut params, "can_post_stories", &can_post_stories)?;
        push_opt(&mut params, "can_edit_stories", &can_edit_stories)?;
        push_opt(&mut params, "can_delete_stories", &can_delete_stories)?;
        push_opt(
            &mut params,
            "can_manage_direct_messages",
            &can_manage_direct_messages,
        )?;
        push_opt(&mut params, "can_manage_tags", &can_manage_tags)?;
        self.do_post("promoteChatMember", params).await
    }

    /// Internal raw method to set a custom title for an administrator in a supergroup.
    ///
    /// Calls the Telegram `setChatAdministratorCustomTitle` API method.
    pub(crate) async fn set_chat_administrator_custom_title_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
        custom_title: &str,
    ) -> Result<bool> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
            RequestParameter::new(
                "custom_title",
                serde_json::Value::String(custom_title.to_owned()),
            ),
        ];
        self.do_post("setChatAdministratorCustomTitle", params)
            .await
    }

    /// Internal raw method to set default chat permissions for all members.
    ///
    /// Calls the Telegram `setChatPermissions` API method.
    pub(crate) async fn set_chat_permissions_raw(
        &self,
        chat_id: ChatId,
        permissions: chat_permissions::ChatPermissions,
        use_independent_chat_permissions: Option<bool>,
    ) -> Result<bool> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("permissions", serde_json::to_value(&permissions)?),
        ];
        push_opt(
            &mut params,
            "use_independent_chat_permissions",
            &use_independent_chat_permissions,
        )?;
        self.do_post("setChatPermissions", params).await
    }

    /// Internal raw method to set a new profile photo for the chat.
    ///
    /// Calls the Telegram `setChatPhoto` API method.
    pub(crate) async fn set_chat_photo_raw(
        &self,
        chat_id: ChatId,
        photo: files::input_file::InputFile,
    ) -> Result<bool> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            input_file_param("photo", photo),
        ];
        self.do_post("setChatPhoto", params).await
    }

    /// Internal raw method to delete a chat photo.
    ///
    /// Calls the Telegram `deleteChatPhoto` API method.
    pub(crate) async fn delete_chat_photo_raw(&self, chat_id: ChatId) -> Result<bool> {
        let params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        self.do_post("deleteChatPhoto", params).await
    }

    /// Internal raw method to change the title of a chat.
    ///
    /// Calls the Telegram `setChatTitle` API method.
    pub(crate) async fn set_chat_title_raw(&self, chat_id: ChatId, title: &str) -> Result<bool> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("title", serde_json::Value::String(title.to_owned())),
        ];
        self.do_post("setChatTitle", params).await
    }

    /// Internal raw method to change the description of a group, supergroup or channel.
    ///
    /// Calls the Telegram `setChatDescription` API method.
    pub(crate) async fn set_chat_description_raw(
        &self,
        chat_id: ChatId,
        description: Option<&str>,
    ) -> Result<bool> {
        let mut params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        push_opt_str(&mut params, "description", description);
        self.do_post("setChatDescription", params).await
    }

    /// Internal raw method to set a new group sticker set for a supergroup.
    ///
    /// Calls the Telegram `setChatStickerSet` API method.
    pub(crate) async fn set_chat_sticker_set_raw(
        &self,
        chat_id: ChatId,
        sticker_set_name: &str,
    ) -> Result<bool> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new(
                "sticker_set_name",
                serde_json::Value::String(sticker_set_name.to_owned()),
            ),
        ];
        self.do_post("setChatStickerSet", params).await
    }

    /// Internal raw method to delete a group sticker set from a supergroup.
    ///
    /// Calls the Telegram `deleteChatStickerSet` API method.
    pub(crate) async fn delete_chat_sticker_set_raw(&self, chat_id: ChatId) -> Result<bool> {
        let params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        self.do_post("deleteChatStickerSet", params).await
    }

    /// Internal raw method to set a tag for a member of a chat.
    ///
    /// Calls the Telegram `setChatMemberTag` API method.
    pub(crate) async fn set_chat_member_tag_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
        tag: Option<&str>,
    ) -> Result<bool> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
        ];
        push_opt_str(&mut params, "tag", tag);
        self.do_post("setChatMemberTag", params).await
    }

    // ======================================================================
    // Chat pinning
    // ======================================================================

    /// Internal raw method to add a message to the list of pinned messages in a chat.
    ///
    /// Calls the Telegram `pinChatMessage` API method.
    pub(crate) async fn pin_chat_message_raw(
        &self,
        chat_id: ChatId,
        message_id: i64,
        disable_notification: Option<bool>,
        business_connection_id: Option<&str>,
    ) -> Result<bool> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("message_id", serde_json::to_value(message_id)?),
        ];
        push_opt(&mut params, "disable_notification", &disable_notification)?;
        push_opt_str(
            &mut params,
            "business_connection_id",
            business_connection_id,
        );
        self.do_post("pinChatMessage", params).await
    }

    /// Internal raw method to remove a message from the list of pinned messages in a chat.
    ///
    /// Calls the Telegram `unpinChatMessage` API method.
    pub(crate) async fn unpin_chat_message_raw(
        &self,
        chat_id: ChatId,
        message_id: Option<i64>,
        business_connection_id: Option<&str>,
    ) -> Result<bool> {
        let mut params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        push_opt(&mut params, "message_id", &message_id)?;
        push_opt_str(
            &mut params,
            "business_connection_id",
            business_connection_id,
        );
        self.do_post("unpinChatMessage", params).await
    }

    /// Internal raw method to clear the list of pinned messages in a chat.
    ///
    /// Calls the Telegram `unpinAllChatMessages` API method.
    pub(crate) async fn unpin_all_chat_messages_raw(&self, chat_id: ChatId) -> Result<bool> {
        let params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        self.do_post("unpinAllChatMessages", params).await
    }

    // ======================================================================
    // Chat invite links
    // ======================================================================

    /// Internal raw method to generate a new primary invite link for a chat.
    ///
    /// Calls the Telegram `exportChatInviteLink` API method.
    pub(crate) async fn export_chat_invite_link_raw(&self, chat_id: ChatId) -> Result<String> {
        let params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        self.do_post("exportChatInviteLink", params).await
    }

    /// Internal raw method to create an additional invite link for a chat.
    ///
    /// Calls the Telegram `createChatInviteLink` API method.
    pub(crate) async fn create_chat_invite_link_raw(
        &self,
        chat_id: ChatId,
        expire_date: Option<i64>,
        member_limit: Option<i64>,
        name: Option<&str>,
        creates_join_request: Option<bool>,
    ) -> Result<chat_invite_link::ChatInviteLink> {
        let mut params = vec![RequestParameter::new(
            "chat_id",
            serde_json::to_value(&chat_id)?,
        )];
        push_opt(&mut params, "expire_date", &expire_date)?;
        push_opt(&mut params, "member_limit", &member_limit)?;
        push_opt_str(&mut params, "name", name);
        push_opt(&mut params, "creates_join_request", &creates_join_request)?;
        self.do_post("createChatInviteLink", params).await
    }

    /// Internal raw method to edit a non-primary invite link created by the bot.
    ///
    /// Calls the Telegram `editChatInviteLink` API method.
    pub(crate) async fn edit_chat_invite_link_raw(
        &self,
        chat_id: ChatId,
        invite_link: &str,
        expire_date: Option<i64>,
        member_limit: Option<i64>,
        name: Option<&str>,
        creates_join_request: Option<bool>,
    ) -> Result<chat_invite_link::ChatInviteLink> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new(
                "invite_link",
                serde_json::Value::String(invite_link.to_owned()),
            ),
        ];
        push_opt(&mut params, "expire_date", &expire_date)?;
        push_opt(&mut params, "member_limit", &member_limit)?;
        push_opt_str(&mut params, "name", name);
        push_opt(&mut params, "creates_join_request", &creates_join_request)?;
        self.do_post("editChatInviteLink", params).await
    }

    /// Internal raw method to revoke an invite link created by the bot.
    ///
    /// Calls the Telegram `revokeChatInviteLink` API method.
    pub(crate) async fn revoke_chat_invite_link_raw(
        &self,
        chat_id: ChatId,
        invite_link: &str,
    ) -> Result<chat_invite_link::ChatInviteLink> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new(
                "invite_link",
                serde_json::Value::String(invite_link.to_owned()),
            ),
        ];
        self.do_post("revokeChatInviteLink", params).await
    }

    /// Internal raw method to create a subscription invite link for a channel chat.
    ///
    /// Calls the Telegram `createChatSubscriptionInviteLink` API method.
    pub(crate) async fn create_chat_subscription_invite_link_raw(
        &self,
        chat_id: ChatId,
        subscription_period: i64,
        subscription_price: i64,
        name: Option<&str>,
    ) -> Result<chat_invite_link::ChatInviteLink> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new(
                "subscription_period",
                serde_json::to_value(subscription_period)?,
            ),
            RequestParameter::new(
                "subscription_price",
                serde_json::to_value(subscription_price)?,
            ),
        ];
        push_opt_str(&mut params, "name", name);
        self.do_post("createChatSubscriptionInviteLink", params)
            .await
    }

    /// Internal raw method to edit a subscription invite link created by the bot.
    ///
    /// Calls the Telegram `editChatSubscriptionInviteLink` API method.
    pub(crate) async fn edit_chat_subscription_invite_link_raw(
        &self,
        chat_id: ChatId,
        invite_link: &str,
        name: Option<&str>,
    ) -> Result<chat_invite_link::ChatInviteLink> {
        let mut params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new(
                "invite_link",
                serde_json::Value::String(invite_link.to_owned()),
            ),
        ];
        push_opt_str(&mut params, "name", name);
        self.do_post("editChatSubscriptionInviteLink", params).await
    }

    /// Internal raw method to approve a chat join request.
    ///
    /// Calls the Telegram `approveChatJoinRequest` API method.
    pub(crate) async fn approve_chat_join_request_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
    ) -> Result<bool> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
        ];
        self.do_post("approveChatJoinRequest", params).await
    }

    /// Internal raw method to decline a chat join request.
    ///
    /// Calls the Telegram `declineChatJoinRequest` API method.
    pub(crate) async fn decline_chat_join_request_raw(
        &self,
        chat_id: ChatId,
        user_id: i64,
    ) -> Result<bool> {
        let params = vec![
            RequestParameter::new("chat_id", serde_json::to_value(&chat_id)?),
            RequestParameter::new("user_id", serde_json::to_value(user_id)?),
        ];
        self.do_post("declineChatJoinRequest", params).await
    }
}