tgbot/types/chat/action/
mod.rs

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