tgbot/types/definitions/chat/
action.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    api::{Method, Payload},
5    types::{ChatId, Integer},
6};
7
8/// Represents a type of an action to tell a user that something is happening on a bot side.
9#[derive(Clone, Copy, Debug, Deserialize, Hash, PartialEq, PartialOrd, Serialize)]
10#[serde(rename_all = "snake_case")]
11pub enum ChatAction {
12    /// Indicates the bot is choosing a sticker.
13    ChooseSticker,
14    /// Indicates the bot is finding location data.
15    FindLocation,
16    /// Indicates the bot is recording a video.
17    RecordVideo,
18    /// Indicates the bot is recording a voice message.
19    RecordVoice,
20    /// Indicates the bot is recording a video note.
21    RecordVideoNote,
22    /// Indicates the bot is typing a text message.
23    Typing,
24    /// Indicates the bot is uploading a document file.
25    UploadDocument,
26    /// Indicates the bot is uploading a photo.
27    UploadPhoto,
28    /// Indicates the bot is uploading a video.
29    UploadVideo,
30    /// Indicates the bot is uploading a video note.
31    UploadVideoNote,
32    /// Indicates the bot is uploading a voice message.
33    UploadVoice,
34}
35
36/// Tells a user that something is happening on a bot side.
37///
38/// A status is set for 5 seconds or less
39/// (when a message arrives from your bot, Telegram clients clear its typing status).
40///
41/// Example: The ImageBot needs some time to process a request and upload the image.
42/// Instead of sending a text message along the lines of “Retrieving image, please wait…”,
43/// the bot may use [`SendChatAction`] with [`ChatAction::UploadPhoto`].
44/// The user will see a “sending photo” status for the bot.
45///
46/// We only recommend using this method when a response from the bot
47/// will take a noticeable amount of time to arrive.
48#[serde_with::skip_serializing_none]
49#[derive(Clone, Debug, Serialize)]
50pub struct SendChatAction {
51    action: ChatAction,
52    chat_id: ChatId,
53    business_connection_id: Option<String>,
54    message_thread_id: Option<Integer>,
55}
56
57impl SendChatAction {
58    /// Creates a new `SendChatAction`.
59    ///
60    /// # Arguments
61    ///
62    /// * `chat_id` - The unique identifier of the target chat.
63    /// * `action` - The type of action to broadcast.
64    pub fn new<T>(chat_id: T, action: ChatAction) -> Self
65    where
66        T: Into<ChatId>,
67    {
68        Self {
69            action,
70            chat_id: chat_id.into(),
71            business_connection_id: None,
72            message_thread_id: None,
73        }
74    }
75
76    /// Sets a new business connection ID.
77    ///
78    /// # Arguments
79    ///
80    /// * `value` - Unique identifier of the business connection on behalf of which the action will be sent.
81    pub fn with_business_connection_id<T>(mut self, value: T) -> Self
82    where
83        T: Into<String>,
84    {
85        self.business_connection_id = Some(value.into());
86        self
87    }
88
89    /// Sets a new message thread ID.
90    ///
91    /// # Arguments
92    ///
93    /// * `value` - Unique identifier of the target message thread;
94    ///   supergroups only.
95    pub fn with_message_thread_id(mut self, value: Integer) -> Self {
96        self.message_thread_id = Some(value);
97        self
98    }
99}
100
101impl Method for SendChatAction {
102    type Response = bool;
103
104    fn into_payload(self) -> Payload {
105        Payload::json("sendChatAction", self)
106    }
107}