rustigram-api 0.12.0

Telegram Bot API method builders and HTTP client for rustigram
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
use crate::client::BotClient;
use crate::error::Result;
use reqwest::multipart::{Form, Part};
use rustigram_types::sticker::{
    InputSticker, MaskPosition, Sticker, StickerFormat, StickerSet, StickerType,
};
use serde::Serialize;
use std::future::{Future, IntoFuture};
use std::pin::Pin;

#[derive(Serialize)]
struct GetStickerSetParams {
    name: String,
}

/// Builder for the [`getStickerSet`](https://core.telegram.org/bots/api#getstickerset) method.
pub struct GetStickerSet {
    client: BotClient,
    params: GetStickerSetParams,
}
impl GetStickerSet {
    pub(crate) fn new(client: BotClient, name: impl Into<String>) -> Self {
        Self {
            client,
            params: GetStickerSetParams { name: name.into() },
        }
    }
}
impl IntoFuture for GetStickerSet {
    type Output = Result<StickerSet>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move { self.client.post_json("getStickerSet", &self.params).await })
    }
}

#[derive(Serialize)]
struct GetCustomEmojiStickersParams {
    custom_emoji_ids: Vec<String>,
}

/// Builder for the [`getCustomEmojiStickers`](https://core.telegram.org/bots/api#getcustomemojistickers) method.
pub struct GetCustomEmojiStickers {
    client: BotClient,
    params: GetCustomEmojiStickersParams,
}
impl GetCustomEmojiStickers {
    pub(crate) fn new(client: BotClient, ids: Vec<impl Into<String>>) -> Self {
        Self {
            client,
            params: GetCustomEmojiStickersParams {
                custom_emoji_ids: ids.into_iter().map(Into::into).collect(),
            },
        }
    }
}
impl IntoFuture for GetCustomEmojiStickers {
    type Output = Result<Vec<Sticker>>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            self.client
                .post_json("getCustomEmojiStickers", &self.params)
                .await
        })
    }
}

/// Builder for the [`uploadStickerFile`](https://core.telegram.org/bots/api#uploadstickerfile) method.
pub struct UploadStickerFile {
    client: BotClient,
    user_id: i64,
    sticker: rustigram_types::file::InputFile,
    sticker_format: StickerFormat,
}
impl UploadStickerFile {
    pub(crate) fn new(
        client: BotClient,
        user_id: i64,
        sticker: rustigram_types::file::InputFile,
        format: StickerFormat,
    ) -> Self {
        Self {
            client,
            user_id,
            sticker,
            sticker_format: format,
        }
    }
}
impl IntoFuture for UploadStickerFile {
    type Output = Result<rustigram_types::file::File>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            match self.sticker {
                rustigram_types::file::InputFile::Bytes {
                    filename,
                    data,
                    mime_type,
                } => {
                    let part = Part::bytes(data)
                        .file_name(filename)
                        .mime_str(&mime_type)
                        .map_err(|e| crate::error::Error::Decode(e.to_string()))?;
                    let fmt = match self.sticker_format {
                        StickerFormat::Static => "static",
                        StickerFormat::Animated => "animated",
                        StickerFormat::Video => "video",
                    };
                    let form = Form::new()
                        .text("user_id", self.user_id.to_string())
                        .text("sticker_format", fmt)
                        .part("sticker", part);
                    self.client.post_multipart("uploadStickerFile", form).await
                }
                ref other => {
                    let body = serde_json::json!({ "user_id": self.user_id, "sticker": other.as_str(), "sticker_format": self.sticker_format });
                    self.client.post_json("uploadStickerFile", &body).await
                }
            }
        })
    }
}

#[derive(Serialize)]
struct InputStickerJson {
    sticker: String,
    format: StickerFormat,
    emoji_list: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    mask_position: Option<MaskPosition>,
    #[serde(skip_serializing_if = "Option::is_none")]
    keywords: Option<Vec<String>>,
}

#[derive(Serialize)]
struct CreateNewStickerSetParams {
    user_id: i64,
    name: String,
    title: String,
    stickers: Vec<InputStickerJson>,
    #[serde(skip_serializing_if = "Option::is_none")]
    sticker_type: Option<StickerType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    needs_repainting: Option<bool>,
}

/// Builder for the [`createNewStickerSet`](https://core.telegram.org/bots/api#createnewstickerset) method.
pub struct CreateNewStickerSet {
    client: BotClient,
    params: CreateNewStickerSetParams,
}
impl CreateNewStickerSet {
    pub(crate) fn new(
        client: BotClient,
        user_id: i64,
        name: impl Into<String>,
        title: impl Into<String>,
        stickers: Vec<InputSticker>,
    ) -> Self {
        let stickers_json = stickers
            .into_iter()
            .map(|s| InputStickerJson {
                sticker: s.sticker,
                format: s.format,
                emoji_list: s.emoji_list,
                mask_position: s.mask_position,
                keywords: s.keywords,
            })
            .collect();
        Self {
            client,
            params: CreateNewStickerSetParams {
                user_id,
                name: name.into(),
                title: title.into(),
                stickers: stickers_json,
                sticker_type: None,
                needs_repainting: None,
            },
        }
    }
    /// Sets the sticker set type (`Regular`, `Mask`, or `CustomEmoji`).
    pub fn sticker_type(mut self, k: StickerType) -> Self {
        self.params.sticker_type = Some(k);
        self
    }
    /// Enables colour replacement for custom emoji stickers when `true`.
    pub fn needs_repainting(mut self, v: bool) -> Self {
        self.params.needs_repainting = Some(v);
        self
    }
}
impl IntoFuture for CreateNewStickerSet {
    type Output = Result<bool>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            self.client
                .post_json("createNewStickerSet", &self.params)
                .await
        })
    }
}

#[derive(Serialize)]
struct AddStickerToSetParams {
    user_id: i64,
    name: String,
    sticker: InputStickerJson,
}

/// Builder for the [`addStickerToSet`](https://core.telegram.org/bots/api#addstickertoset) method.
pub struct AddStickerToSet {
    client: BotClient,
    params: AddStickerToSetParams,
}
impl AddStickerToSet {
    pub(crate) fn new(
        client: BotClient,
        user_id: i64,
        name: impl Into<String>,
        sticker: InputSticker,
    ) -> Self {
        Self {
            client,
            params: AddStickerToSetParams {
                user_id,
                name: name.into(),
                sticker: InputStickerJson {
                    sticker: sticker.sticker,
                    format: sticker.format,
                    emoji_list: sticker.emoji_list,
                    mask_position: sticker.mask_position,
                    keywords: sticker.keywords,
                },
            },
        }
    }
}
impl IntoFuture for AddStickerToSet {
    type Output = Result<bool>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move { self.client.post_json("addStickerToSet", &self.params).await })
    }
}

macro_rules! simple_sticker_action {
    ($(#[$doc:meta])* $name:ident, $params_ty:ident { $($f:ident: $t:ty),+ }, $method:literal, $ret:ty) => {
        #[derive(Serialize)]
        struct $params_ty { $($f: $t),+ }

        $(#[$doc])*
        pub struct $name { client: BotClient, params: $params_ty }

        impl IntoFuture for $name {
            type Output = Result<$ret>;
            type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
            fn into_future(self) -> Self::IntoFuture {
                Box::pin(async move { self.client.post_json($method, &self.params).await })
            }
        }
    };
}

simple_sticker_action!(
    /// Builder for the [`setStickerPositionInSet`](https://core.telegram.org/bots/api#setstickerpositioninset) method.
    SetStickerPositionInSet,
    SetStickerPositionInSetParams { sticker: String, position: u32 },
    "setStickerPositionInSet",
    bool
);
impl SetStickerPositionInSet {
    pub(crate) fn new(client: BotClient, sticker: impl Into<String>, position: u32) -> Self {
        Self {
            client,
            params: SetStickerPositionInSetParams {
                sticker: sticker.into(),
                position,
            },
        }
    }
}

simple_sticker_action!(
    /// Builder for the [`deleteStickerFromSet`](https://core.telegram.org/bots/api#deletestickerfromset) method.
    DeleteStickerFromSet,
    DeleteStickerFromSetParams { sticker: String },
    "deleteStickerFromSet",
    bool
);
impl DeleteStickerFromSet {
    pub(crate) fn new(client: BotClient, sticker: impl Into<String>) -> Self {
        Self {
            client,
            params: DeleteStickerFromSetParams {
                sticker: sticker.into(),
            },
        }
    }
}

simple_sticker_action!(
    /// Builder for the [`setStickerSetTitle`](https://core.telegram.org/bots/api#setstickersettitle) method.
    SetStickerSetTitle,
    SetStickerSetTitleParams { name: String, title: String },
    "setStickerSetTitle",
    bool
);
impl SetStickerSetTitle {
    pub(crate) fn new(
        client: BotClient,
        name: impl Into<String>,
        title: impl Into<String>,
    ) -> Self {
        Self {
            client,
            params: SetStickerSetTitleParams {
                name: name.into(),
                title: title.into(),
            },
        }
    }
}

simple_sticker_action!(
    /// Builder for the [`deleteStickerSet`](https://core.telegram.org/bots/api#deletestickerset) method.
    DeleteStickerSet,
    DeleteStickerSetParams { name: String },
    "deleteStickerSet",
    bool
);
impl DeleteStickerSet {
    pub(crate) fn new(client: BotClient, name: impl Into<String>) -> Self {
        Self {
            client,
            params: DeleteStickerSetParams { name: name.into() },
        }
    }
}

#[derive(Serialize)]
struct SetStickerEmojiListParams {
    sticker: String,
    emoji_list: Vec<String>,
}

/// Builder for the [`setStickerEmojiList`](https://core.telegram.org/bots/api#setstickeremojilist) method.
pub struct SetStickerEmojiList {
    client: BotClient,
    params: SetStickerEmojiListParams,
}
impl SetStickerEmojiList {
    pub(crate) fn new(
        client: BotClient,
        sticker: impl Into<String>,
        emoji_list: Vec<impl Into<String>>,
    ) -> Self {
        Self {
            client,
            params: SetStickerEmojiListParams {
                sticker: sticker.into(),
                emoji_list: emoji_list.into_iter().map(Into::into).collect(),
            },
        }
    }
}
impl IntoFuture for SetStickerEmojiList {
    type Output = Result<bool>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            self.client
                .post_json("setStickerEmojiList", &self.params)
                .await
        })
    }
}

#[derive(Serialize)]
struct SetStickerKeywordsParams {
    sticker: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    keywords: Option<Vec<String>>,
}

/// Builder for the [`setStickerKeywords`](https://core.telegram.org/bots/api#setstickerkeywords) method.
pub struct SetStickerKeywords {
    client: BotClient,
    params: SetStickerKeywordsParams,
}
impl SetStickerKeywords {
    pub(crate) fn new(client: BotClient, sticker: impl Into<String>) -> Self {
        Self {
            client,
            params: SetStickerKeywordsParams {
                sticker: sticker.into(),
                keywords: None,
            },
        }
    }
    /// Sets the search keywords for this sticker (up to 20 words).
    pub fn keywords(mut self, kw: Vec<impl Into<String>>) -> Self {
        self.params.keywords = Some(kw.into_iter().map(Into::into).collect());
        self
    }
}
impl IntoFuture for SetStickerKeywords {
    type Output = Result<bool>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            self.client
                .post_json("setStickerKeywords", &self.params)
                .await
        })
    }
}

#[derive(Serialize)]
struct SetStickerMaskPositionParams {
    sticker: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    mask_position: Option<MaskPosition>,
}

/// Builder for the [`setStickerMaskPosition`](https://core.telegram.org/bots/api#setstickermaskposition) method.
pub struct SetStickerMaskPosition {
    client: BotClient,
    params: SetStickerMaskPositionParams,
}
impl SetStickerMaskPosition {
    pub(crate) fn new(client: BotClient, sticker: impl Into<String>) -> Self {
        Self {
            client,
            params: SetStickerMaskPositionParams {
                sticker: sticker.into(),
                mask_position: None,
            },
        }
    }
    /// Sets the mask position for this mask sticker.
    pub fn mask_position(mut self, mp: MaskPosition) -> Self {
        self.params.mask_position = Some(mp);
        self
    }
}
impl IntoFuture for SetStickerMaskPosition {
    type Output = Result<bool>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            self.client
                .post_json("setStickerMaskPosition", &self.params)
                .await
        })
    }
}

/// Builder for the [`getForumTopicIconStickers`](https://core.telegram.org/bots/api#getforumtopiciconstickers) method.
pub struct GetForumTopicIconStickers {
    client: BotClient,
}
impl GetForumTopicIconStickers {
    pub(crate) fn new(client: BotClient) -> Self {
        Self { client }
    }
}
impl IntoFuture for GetForumTopicIconStickers {
    type Output = Result<Vec<Sticker>>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            self.client
                .post_json("getForumTopicIconStickers", &serde_json::json!({}))
                .await
        })
    }
}

// ─── replaceStickerInSet ──────────────────────────────────────────────────────

#[derive(Serialize)]
struct ReplaceStickerInSetParams {
    user_id: i64,
    name: String,
    old_sticker: String,
    sticker: InputStickerJson,
}

/// Builder for the [`replaceStickerInSet`](https://core.telegram.org/bots/api#replacestickerinset) method.
///
/// Equivalent to calling `deleteStickerFromSet`, `addStickerToSet`, and
/// `setStickerPositionInSet` in sequence.
pub struct ReplaceStickerInSet {
    client: BotClient,
    params: ReplaceStickerInSetParams,
}

impl ReplaceStickerInSet {
    pub(crate) fn new(
        client: BotClient,
        user_id: i64,
        name: impl Into<String>,
        old_sticker: impl Into<String>,
        sticker: InputSticker,
    ) -> Self {
        Self {
            client,
            params: ReplaceStickerInSetParams {
                user_id,
                name: name.into(),
                old_sticker: old_sticker.into(),
                sticker: InputStickerJson {
                    sticker: sticker.sticker,
                    format: sticker.format,
                    emoji_list: sticker.emoji_list,
                    mask_position: sticker.mask_position,
                    keywords: sticker.keywords,
                },
            },
        }
    }
}

impl IntoFuture for ReplaceStickerInSet {
    type Output = Result<bool>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            self.client
                .post_json("replaceStickerInSet", &self.params)
                .await
        })
    }
}

// ─── setStickerSetThumbnail ───────────────────────────────────────────────────

/// Builder for the [`setStickerSetThumbnail`](https://core.telegram.org/bots/api#setstickersetthumbnail) method.
///
/// Sets the thumbnail of a regular or mask sticker set.
/// The thumbnail format must match the sticker format in the set.
/// Omit `thumbnail` to drop the current thumbnail and use the first sticker instead.
pub struct SetStickerSetThumbnail {
    client: BotClient,
    name: String,
    user_id: i64,
    /// The thumbnail format string: `"static"`, `"animated"`, or `"video"`.
    format: String,
    thumbnail: Option<rustigram_types::file::InputFile>,
}

impl SetStickerSetThumbnail {
    pub(crate) fn new(
        client: BotClient,
        name: impl Into<String>,
        user_id: i64,
        format: impl Into<String>,
    ) -> Self {
        Self {
            client,
            name: name.into(),
            user_id,
            format: format.into(),
            thumbnail: None,
        }
    }
    /// Sets the thumbnail file to upload. Omit to remove the current thumbnail.
    pub fn thumbnail(mut self, f: rustigram_types::file::InputFile) -> Self {
        self.thumbnail = Some(f);
        self
    }
}

impl IntoFuture for SetStickerSetThumbnail {
    type Output = Result<bool>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            match self.thumbnail {
                Some(rustigram_types::file::InputFile::Bytes {
                    filename,
                    data,
                    mime_type,
                }) => {
                    let part = Part::bytes(data)
                        .file_name(filename)
                        .mime_str(&mime_type)
                        .map_err(|e| crate::error::Error::Decode(e.to_string()))?;
                    let form = Form::new()
                        .text("name", self.name)
                        .text("user_id", self.user_id.to_string())
                        .text("format", self.format)
                        .part("thumbnail", part);
                    self.client
                        .post_multipart("setStickerSetThumbnail", form)
                        .await
                }
                other => {
                    let mut body = serde_json::json!({
                        "name": self.name,
                        "user_id": self.user_id,
                        "format": self.format,
                    });
                    if let Some(f) = other {
                        body["thumbnail"] = serde_json::json!(f.as_str());
                    }
                    self.client.post_json("setStickerSetThumbnail", &body).await
                }
            }
        })
    }
}

// ─── setCustomEmojiStickerSetThumbnail ────────────────────────────────────────

#[derive(Serialize)]
struct SetCustomEmojiStickerSetThumbnailParams {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    custom_emoji_id: Option<String>,
}

/// Builder for the [`setCustomEmojiStickerSetThumbnail`](https://core.telegram.org/bots/api#setcustomemojistickersetthumbnail) method.
///
/// Sets the thumbnail of a custom emoji sticker set.
/// Omit `custom_emoji_id` or pass an empty string to drop the current thumbnail
/// and use the first sticker instead.
pub struct SetCustomEmojiStickerSetThumbnail {
    client: BotClient,
    params: SetCustomEmojiStickerSetThumbnailParams,
}

impl SetCustomEmojiStickerSetThumbnail {
    pub(crate) fn new(client: BotClient, name: impl Into<String>) -> Self {
        Self {
            client,
            params: SetCustomEmojiStickerSetThumbnailParams {
                name: name.into(),
                custom_emoji_id: None,
            },
        }
    }
    /// Sets the custom emoji identifier to use as the thumbnail.
    /// Pass an empty string to remove the current thumbnail.
    pub fn custom_emoji_id(mut self, id: impl Into<String>) -> Self {
        self.params.custom_emoji_id = Some(id.into());
        self
    }
}

impl IntoFuture for SetCustomEmojiStickerSetThumbnail {
    type Output = Result<bool>;
    type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
    fn into_future(self) -> Self::IntoFuture {
        Box::pin(async move {
            self.client
                .post_json("setCustomEmojiStickerSetThumbnail", &self.params)
                .await
        })
    }
}