tgbot 0.48.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
use serde::{Deserialize, Serialize};

use crate::{
    api::{Form, Method, Payload, PayloadError, WriteForm},
    types::{
        ChatId,
        InlineKeyboardMarkup,
        InputMedia,
        InputMediaData,
        InputRichMessage,
        InputRichMessageData,
        InputText,
        InputTextCaption,
        Integer,
        LinkPreviewOptions,
    },
};

/// Ephemeral message edit parameters.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EphemeralMessageParameters {
    receiver_user_id: Integer,
    callback_query_id: Option<String>,
    replace_callback_query_message: Option<bool>,
}

impl From<Integer> for EphemeralMessageParameters {
    fn from(value: Integer) -> Self {
        Self {
            receiver_user_id: value,
            callback_query_id: None,
            replace_callback_query_message: None,
        }
    }
}

impl EphemeralMessageParameters {
    /// Sets a new callback query ID.
    ///
    /// # Arguments
    ///
    /// * `value` - Identifier of the callback query which triggered the message.
    pub fn with_callback_query_id<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.callback_query_id = Some(value.into());
        self
    }

    /// Sets a new value for the `replace_callback_query_message` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the ephemeral message must be shown in place of the original message.
    ///
    /// Must be false for callback queries from ephemeral messages,
    /// which must be edited using regular editEphemeralMessage methods.
    pub fn with_replace_callback_query_message(mut self, value: bool) -> Self {
        self.replace_callback_query_message = Some(value);
        self
    }
}

/// Ephemeral message identity.
///
/// Consists of a chat ID, receiver user ID and ephemeral message ID in the chat.
#[derive(Clone, Debug, Serialize)]
pub struct EphemeralMessageIdentity {
    chat_id: ChatId,
    receiver_user_id: Integer,
    ephemeral_message_id: Integer,
}

impl<T> From<(T, Integer, Integer)> for EphemeralMessageIdentity
where
    T: Into<ChatId>,
{
    fn from((chat_id, receiver_user_id, ephemeral_message_id): (T, Integer, Integer)) -> Self {
        Self {
            chat_id: chat_id.into(),
            receiver_user_id,
            ephemeral_message_id,
        }
    }
}

/// Deletes an ephemeral message.
///
/// Note that it is not guaranteed that the user will receive the message deletion event, especially if they are offline.
#[derive(Clone, Debug)]
pub struct DeleteEphemeralMessage {
    identity: EphemeralMessageIdentity,
}

impl<T> From<T> for DeleteEphemeralMessage
where
    T: Into<EphemeralMessageIdentity>,
{
    fn from(value: T) -> Self {
        Self { identity: value.into() }
    }
}

impl Method for DeleteEphemeralMessage {
    type Response = bool;

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

/// Edits the caption of an ephemeral message.
///
/// Note that it is not guaranteed that
/// the user will receive the message edit event,
/// especially if they are offline.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct EditEphemeralMessageCaption {
    #[serde(flatten)]
    identity: EphemeralMessageIdentity,
    #[serde(flatten)]
    caption: Option<InputTextCaption>,
    show_caption_above_media: Option<bool>,
    reply_markup: Option<InlineKeyboardMarkup>,
}

impl<T> From<T> for EditEphemeralMessageCaption
where
    T: Into<EphemeralMessageIdentity>,
{
    fn from(value: T) -> Self {
        Self {
            identity: value.into(),
            caption: None,
            show_caption_above_media: None,
            reply_markup: None,
        }
    }
}

impl EditEphemeralMessageCaption {
    /// Sets a new caption
    ///
    /// # Arguments
    ///
    /// * `value` - New caption of the message; 0-1024 characters after entities parsing.
    pub fn with_caption<T>(mut self, value: T) -> Self
    where
        T: Into<InputTextCaption>,
    {
        self.caption = Some(value.into());
        self
    }

    /// Sets a new value for the `show_caption_above_media` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Whether the caption must be shown above the message media.
    ///
    /// Supported only for animation, photo and video messages.
    pub fn with_show_caption_above_media(mut self, value: bool) -> Self {
        self.show_caption_above_media = Some(value);
        self
    }

    /// Sets a new reply markup.
    ///
    /// # Arguments
    ///
    /// * `value` - An object for an inline keyboard
    pub fn with_reply_markup<T>(mut self, value: T) -> Self
    where
        T: Into<InlineKeyboardMarkup>,
    {
        self.reply_markup = Some(value.into());
        self
    }
}

impl Method for EditEphemeralMessageCaption {
    type Response = bool;

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

/// Edits the media of an ephemeral message.
///
/// Note that it is not guaranteed that
/// the user will receive the message edit event,
/// especially if they are offline.
#[derive(Debug)]
pub struct EditEphemeralMessageMedia {
    identity: EphemeralMessageIdentity,
    media: InputMedia,
    reply_markup: Option<InlineKeyboardMarkup>,
}

impl EditEphemeralMessageMedia {
    /// Creates a new `EditEphemeralMessageMedia`.
    ///
    /// # Arguments
    ///
    /// * `identity` - Identity of the ephemeral message.
    /// * `media` - The new media content of the message.
    pub fn new<A, B>(identity: A, media: B) -> Self
    where
        A: Into<EphemeralMessageIdentity>,
        B: Into<InputMedia>,
    {
        Self {
            identity: identity.into(),
            media: media.into(),
            reply_markup: None,
        }
    }

    /// Sets a new reply markup.
    ///
    /// # Arguments
    ///
    /// * `value` - An object for an inline keyboard
    pub fn with_reply_markup<T>(mut self, value: T) -> Self
    where
        T: Into<InlineKeyboardMarkup>,
    {
        self.reply_markup = Some(value.into());
        self
    }
}

#[serde_with::skip_serializing_none]
#[derive(Debug, Serialize)]
struct EditEphemeralMessageMediaParameters {
    #[serde(flatten)]
    identity: EphemeralMessageIdentity,
    media: InputMediaData,
    reply_markup: Option<InlineKeyboardMarkup>,
}

impl Method for EditEphemeralMessageMedia {
    type Response = bool;

    fn into_payload(self) -> Result<Payload, PayloadError> {
        let Self {
            identity,
            media,
            reply_markup,
        } = self;
        let mut form = Form::default();
        let parameters = EditEphemeralMessageMediaParameters {
            identity,
            media: media.write(&mut form),
            reply_markup,
        };
        parameters.serialize(&mut form)?;
        Payload::form("editEphemeralMessageMedia", form)
    }
}

/// Edits only the reply markup of an ephemeral message.
///
/// Note that it is not guaranteed that
/// the user will receive the message edit event,
/// especially if they are offline.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Serialize)]
pub struct EditEphemeralMessageReplyMarkup {
    #[serde(flatten)]
    identity: EphemeralMessageIdentity,
    reply_markup: Option<InlineKeyboardMarkup>,
}

impl<T> From<T> for EditEphemeralMessageReplyMarkup
where
    T: Into<EphemeralMessageIdentity>,
{
    fn from(value: T) -> Self {
        Self {
            identity: value.into(),
            reply_markup: None,
        }
    }
}

impl EditEphemeralMessageReplyMarkup {
    /// Sets a new reply markup.
    ///
    /// # Arguments
    ///
    /// * `value` - An object for an inline keyboard
    pub fn with_reply_markup<T>(mut self, value: T) -> Self
    where
        T: Into<InlineKeyboardMarkup>,
    {
        self.reply_markup = Some(value.into());
        self
    }
}

impl Method for EditEphemeralMessageReplyMarkup {
    type Response = bool;

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

/// Edits an ephemeral text message.
///
/// Note that it is not guaranteed that the user will receive the message edit event,
/// especially if they are offline.
#[derive(Debug)]
pub struct EditEphemeralMessageText {
    identity: EphemeralMessageIdentity,
    rich_message: Option<InputRichMessage>,
    parameters: EditEphemeralMessageTextParameters,
}

impl EditEphemeralMessageText {
    /// Creates a new `EditEpehemeralMessageText`.
    ///
    /// # Arguments
    ///
    /// * `identity` - Identity of the ephemeral message.
    /// * `text` - New text of the message; 1-4096 characters after entity parsing.
    pub fn rich_message<T>(identity: T, rich_message: InputRichMessage) -> Self
    where
        T: Into<EphemeralMessageIdentity>,
    {
        Self {
            identity: identity.into(),
            rich_message: Some(rich_message),
            parameters: Default::default(),
        }
    }

    /// Creates a new `EditEpehemeralMessageText`.
    ///
    /// # Arguments
    ///
    /// * `identity` - Identity of the ephemeral message.
    /// * `text` - New text of the message; 1-4096 characters after entity parsing.
    pub fn text<A, B>(identity: A, text: B) -> Self
    where
        A: Into<EphemeralMessageIdentity>,
        B: Into<InputText>,
    {
        Self {
            identity: identity.into(),
            rich_message: None,
            parameters: EditEphemeralMessageTextParameters {
                text: Some(text.into()),
                ..Default::default()
            },
        }
    }

    /// Sets a new link preview options.
    ///
    /// # Arguments
    ///
    /// * `value` - Link preview generation options for the message.
    pub fn with_link_preview_options(mut self, value: LinkPreviewOptions) -> Self {
        self.parameters.link_preview_options = Some(value);
        self
    }

    /// Sets a new reply markup.
    ///
    /// # Arguments
    ///
    /// * `value` - An object for an inline keyboard
    pub fn with_reply_markup<T>(mut self, value: T) -> Self
    where
        T: Into<InlineKeyboardMarkup>,
    {
        self.parameters.reply_markup = Some(value.into());
        self
    }
}

#[serde_with::skip_serializing_none]
#[derive(Debug, Default, Serialize)]
struct EditEphemeralMessageTextParameters {
    #[serde(flatten)]
    identity: Option<EphemeralMessageIdentity>,
    #[serde(flatten)]
    text: Option<InputText>,
    link_preview_options: Option<LinkPreviewOptions>,
    reply_markup: Option<InlineKeyboardMarkup>,
    rich_message: Option<InputRichMessageData>,
}

impl Method for EditEphemeralMessageText {
    type Response = bool;

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