tgbot 0.47.0

A Telegram Bot library
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
use serde::{Deserialize, Serialize};

use crate::{
    api::{Form, Method, Payload, PayloadError, WriteForm},
    types::{InputFile, InputSticker, InputStickerData, Integer, PhotoSize, Sticker, StickerFormat, StickerType},
};

/// Represents a sticker set.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct StickerSet {
    /// Name of the sticker set.
    pub name: String,
    /// Type of stickers.
    pub sticker_type: StickerType,
    /// List of stickers.
    pub stickers: Vec<Sticker>,
    /// Title of the sticker set.
    pub title: String,
    /// Sticker set thumbnail in the WEBP or TGS format.
    pub thumbnail: Option<PhotoSize>,
}

/// Adds a new sticker to a set created by the bot.
///
/// The format of the added sticker must match the format of the other stickers in the set.
/// Emoji sticker sets can have up to 200 stickers.
/// Animated and video sticker sets can have up to 50 stickers.
/// Static sticker sets can have up to 120 stickers.
#[derive(Debug)]
pub struct AddStickerToSet {
    sticker: InputSticker,
    parameters: AddStickerToSetParameters,
}

impl AddStickerToSet {
    /// Creates a new `AddStickerToSet`.
    ///
    /// # Arguments
    ///
    /// * `user_id` - User identifier of sticker set owner.
    /// * `name` - Sticker set name.
    /// * `sticker` - Sticker file.
    pub fn new<T>(user_id: Integer, name: T, sticker: InputSticker) -> Self
    where
        T: Into<String>,
    {
        Self {
            sticker,
            parameters: AddStickerToSetParameters {
                user_id: Some(user_id),
                name: Some(name.into()),
                ..Default::default()
            },
        }
    }
}

#[serde_with::skip_serializing_none]
#[derive(Debug, Default, Serialize)]
struct AddStickerToSetParameters {
    name: Option<String>,
    sticker: Option<InputStickerData>,
    user_id: Option<Integer>,
}

impl Method for AddStickerToSet {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        let Self {
            sticker,
            mut parameters,
        } = self;
        let mut form = Form::default();
        parameters.sticker = Some(sticker.write(&mut form));
        parameters.serialize(&mut form)?;
        Payload::form("addStickerToSet", form)
    }
}

/// Creates a new sticker set owned by a user.
///
/// The bot will be able to edit the created sticker set.
#[derive(Debug)]
pub struct CreateNewStickerSet {
    stickers: Vec<InputSticker>,
    parameters: CreateNewStickerSetParameters,
}

impl CreateNewStickerSet {
    /// Creates a new `CreateNewStickerSet`.
    ///
    /// # Arguments
    ///
    /// * `user_id` - User identifier of created sticker set owner.
    /// * `name` - Short name of sticker set, to be used in t.me/addstickers/ URLs (e.g., animals);
    ///   can contain only english letters, digits and underscores;
    ///   must begin with a letter, can't contain consecutive underscores
    ///   and must end in `_by_<bot username>`;
    ///   <bot_username> is case insensitive;
    ///   1-64 characters.
    /// * `title` - Sticker set title; 1-64 characters.
    /// * `stickers` - A list of 1-50 initial stickers to be added to the sticker set.
    pub fn new<A, B, C>(user_id: Integer, name: A, title: B, stickers: C) -> Self
    where
        A: Into<String>,
        B: Into<String>,
        C: IntoIterator<Item = InputSticker>,
    {
        Self {
            stickers: Vec::from_iter(stickers),
            parameters: CreateNewStickerSetParameters {
                user_id: Some(user_id),
                name: Some(name.into()),
                title: Some(title.into()),
                ..Default::default()
            },
        }
    }

    /// Sets a new value for the `needs_repainting` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether stickers in the sticker set must be repainted to the color
    ///   of text when used in messages, the accent color if used as emoji status,
    ///   white on chat photos, or another appropriate color based on context;
    ///   for custom emoji sticker sets only.
    pub fn with_needs_repainting(mut self, value: bool) -> Self {
        self.parameters.needs_repainting = Some(value);
        self
    }

    /// Sets a new sticker type.
    ///
    /// # Arguments
    ///
    /// * `value` - Type of stickers in the set.
    ///
    /// By default, a regular sticker set is created.
    pub fn with_sticker_type(mut self, value: StickerType) -> Self {
        self.parameters.sticker_type = Some(value);
        self
    }
}

#[serde_with::skip_serializing_none]
#[derive(Debug, Default, Serialize)]
struct CreateNewStickerSetParameters {
    name: Option<String>,
    needs_repainting: Option<bool>,
    sticker_type: Option<StickerType>,
    stickers: Option<Vec<InputStickerData>>,
    title: Option<String>,
    user_id: Option<Integer>,
}

impl Method for CreateNewStickerSet {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        let Self {
            stickers,
            mut parameters,
        } = self;
        let mut form = Form::default();
        parameters.stickers = Some(stickers.into_iter().map(|x| x.write(&mut form)).collect());
        parameters.serialize(&mut form)?;
        Payload::form("createNewStickerSet", form)
    }
}

/// Deletes a sticker from a set created by the bot.
#[derive(Clone, Debug, Serialize)]
pub struct DeleteStickerFromSet {
    sticker: String,
}

impl DeleteStickerFromSet {
    /// Creates a new `DeleteStickerFromSet`.
    ///
    /// # Arguments
    ///
    /// * `sticker` - File identifier of the sticker.
    pub fn new<T>(sticker: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            sticker: sticker.into(),
        }
    }
}

impl Method for DeleteStickerFromSet {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("deleteStickerFromSet", self)
    }
}

/// Deletes a sticker set that was created by the bot.
#[derive(Clone, Debug, Serialize)]
pub struct DeleteStickerSet {
    name: String,
}

impl DeleteStickerSet {
    /// Creates a new `DeleteStickerSet`.
    ///
    /// # Arguments
    ///
    /// * `name` - Sticker set name.
    pub fn new<T>(name: T) -> Self
    where
        T: Into<String>,
    {
        Self { name: name.into() }
    }
}

impl Method for DeleteStickerSet {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("deleteStickerSet", self)
    }
}

/// Returns a sticker set.
#[derive(Clone, Debug, Serialize)]
pub struct GetStickerSet {
    name: String,
}

impl GetStickerSet {
    /// Creates a new `GetStickerSet`.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the sticker set.
    pub fn new<T>(name: T) -> Self
    where
        T: Into<String>,
    {
        Self { name: name.into() }
    }
}

impl Method for GetStickerSet {
    type Response = StickerSet;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("getStickerSet", self)
    }
}

/// Replaces an existing sticker in a sticker set with a new one.
///
/// The method is equivalent to calling [`crate::types::DeleteStickerFromSet`],
/// then [`crate::types::AddStickerToSet`],
/// then [`crate::types::SetStickerPositionInSet`].
#[derive(Debug)]
pub struct ReplaceStickerInSet {
    sticker: InputSticker,
    parameters: ReplaceStickerInSetParameters,
}

impl ReplaceStickerInSet {
    /// Creates a new `ReplaceStickerInSet`.
    ///
    /// # Arguments
    ///
    /// * `name` - Sticker set name.
    /// * `old_sticker` - File identifier of the replaced sticker.
    /// * `sticker` - Information about the added sticker;
    ///   if exactly the same sticker had already been added to the set, then the set remains unchanged.
    /// * `user_id` - User identifier of the sticker set owner.
    pub fn new<A, B>(name: A, old_sticker: B, sticker: InputSticker, user_id: Integer) -> Self
    where
        A: Into<String>,
        B: Into<String>,
    {
        Self {
            sticker,
            parameters: ReplaceStickerInSetParameters {
                name: Some(name.into()),
                old_sticker: Some(old_sticker.into()),
                user_id: Some(user_id),
                ..Default::default()
            },
        }
    }
}

#[derive(Debug, Default, Serialize)]
struct ReplaceStickerInSetParameters {
    name: Option<String>,
    old_sticker: Option<String>,
    sticker: Option<InputStickerData>,
    user_id: Option<Integer>,
}

impl Method for ReplaceStickerInSet {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        let Self {
            sticker,
            mut parameters,
        } = self;
        let mut form = Form::default();
        parameters.sticker = Some(sticker.write(&mut form));
        parameters.serialize(&mut form)?;
        Payload::form("replaceStickerInSet", form)
    }
}

/// Sets the thumbnail of a custom emoji sticker set.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct SetCustomEmojiStickerSetThumbnail {
    name: String,
    custom_emoji_id: Option<String>,
}

impl SetCustomEmojiStickerSetThumbnail {
    /// Creates a new `SetCustomEmojiStickerSetThumbnail`.
    ///
    /// # Arguments
    ///
    /// * `name` - Sticker set name.
    pub fn new<T>(name: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            name: name.into(),
            custom_emoji_id: None,
        }
    }

    /// Sets a new custom emoji ID.
    ///
    /// # Arguments
    ///
    /// * `value` - Custom emoji identifier of a sticker from the sticker set.
    ///
    /// Pass an empty string to drop the thumbnail and use the first sticker as the thumbnail.
    pub fn with_custom_emoji_id<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.custom_emoji_id = Some(value.into());
        self
    }
}

impl Method for SetCustomEmojiStickerSetThumbnail {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("setCustomEmojiStickerSetThumbnail", self)
    }
}

/// Moves a sticker in a set created by the bot to a specific position.
#[derive(Clone, Debug, Serialize)]
pub struct SetStickerPositionInSet {
    position: Integer,
    sticker: String,
}

impl SetStickerPositionInSet {
    /// Creates a new `SetStickerPositionInSet`.
    ///
    /// # Arguments
    ///
    /// * `position` - New sticker position in the set, zero-based.
    /// * `sticker` - File identifier of the sticker.
    pub fn new<T>(position: Integer, sticker: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            position,
            sticker: sticker.into(),
        }
    }
}

impl Method for SetStickerPositionInSet {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("setStickerPositionInSet", self)
    }
}

/// Sets a title of a created sticker set.
#[derive(Clone, Debug, Serialize)]
pub struct SetStickerSetTitle {
    name: String,
    title: String,
}

impl SetStickerSetTitle {
    /// Creates a new `SetStickerSetTitle`.
    ///
    /// # Arguments
    ///
    /// * `name` - Sticker set name.
    /// * `title` - Sticker set title; 1-64 characters.
    pub fn new<A, B>(name: A, title: B) -> Self
    where
        A: Into<String>,
        B: Into<String>,
    {
        Self {
            name: name.into(),
            title: title.into(),
        }
    }
}

impl Method for SetStickerSetTitle {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        Payload::json("setStickerSetTitle", self)
    }
}

/// Sets a thumbnail of a sticker set.
#[derive(Debug)]
pub struct SetStickerSetThumbnail {
    parameters: SetStickerSetThumbnailParameters,
    thumbnail: Option<InputFile>,
}

impl SetStickerSetThumbnail {
    /// Creates a new `SetStickerSetThumbnail`.
    ///
    /// # Arguments
    ///
    /// * `name` - Sticker set name.
    /// * `user_id` - User identifier of the sticker set owner.
    /// * `format` - Format of the thumbnail.
    pub fn new<N>(name: N, user_id: Integer, format: StickerFormat) -> Self
    where
        N: Into<String>,
    {
        Self {
            parameters: SetStickerSetThumbnailParameters {
                name: Some(name.into()),
                user_id: Some(user_id),
                format: Some(format),
                ..Default::default()
            },
            thumbnail: None,
        }
    }

    /// Sets a new thumbnail.
    ///
    /// # Arguments
    ///
    /// * `value` - A WEBP or PNG image with the thumbnail.
    ///
    /// Must be up to 128 kilobytes in size and have a width and height of exactly 100px,
    /// or a .TGS animation with a thumbnail up to 32 kilobytes in size
    /// (see <https://core.telegram.org/stickers#animated-sticker-requirements> for animated sticker
    /// technical requirements), or a WEBM video with the thumbnail up to 32 kilobytes in size;
    /// see <https://core.telegram.org/stickers#video-sticker-requirements> for video sticker
    /// technical requirements.
    ///
    /// Animated and video sticker set thumbnails can't be uploaded via HTTP URL.
    /// If omitted, then the thumbnail is dropped and the first sticker is used as the thumbnail.
    pub fn with_thumbnail<T>(mut self, value: T) -> Self
    where
        T: Into<InputFile>,
    {
        self.thumbnail = Some(value.into());
        self
    }
}

#[derive(Debug, Default, Serialize)]
struct SetStickerSetThumbnailParameters {
    format: Option<StickerFormat>,
    name: Option<String>,
    thumbnail: Option<String>,
    user_id: Option<Integer>,
}

impl Method for SetStickerSetThumbnail {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        let Self {
            thumbnail,
            mut parameters,
        } = self;
        let mut form = Form::default();
        parameters.thumbnail = thumbnail.map(|x| x.write(&mut form));
        parameters.serialize(&mut form)?;
        Payload::form("setStickerSetThumbnail", form)
    }
}