mobot/api/
chat.rs

1use mobot_derive::BotRequest;
2use serde::{Deserialize, Serialize};
3
4use super::API;
5
6#[derive(Default, Debug, Clone, Serialize, Deserialize)]
7pub struct Chat {
8    /// Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier.
9    pub id: i64,
10
11    /// Type of chat, can be either “private”, “group”, “supergroup” or “channel”
12    #[serde(rename = "type")]
13    pub chat_type: String,
14
15    /// Title, for supergroups, channels and group chats
16    pub title: Option<String>,
17
18    /// Username, for private chats, supergroups and channels if available
19    pub username: Option<String>,
20
21    /// First name of the other party in a private chat
22    pub first_name: Option<String>,
23
24    /// Last name of the other party in a private chat
25    pub last_name: Option<String>,
26
27    /// True if a group has ‘All Members Are Admins’ enabled.
28    pub all_members_are_administrators: Option<bool>,
29
30    /// True if a channel has a discussion group, or a supergroup is public
31    /// and has more than 200 members.
32    pub is_forum: Option<bool>,
33}
34
35impl<T: Into<String>> From<T> for Chat {
36    fn from(s: T) -> Self {
37        let from = s.into();
38        Self {
39            chat_type: "private".to_string(),
40            username: Some(from.clone()),
41            first_name: Some(from),
42            ..Default::default()
43        }
44    }
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub enum ChatAction {
49    #[serde(rename = "typing")]
50    Typing,
51    #[serde(rename = "upload_photo")]
52    UploadPhoto,
53    #[serde(rename = "record_video")]
54    RecordVideo,
55    #[serde(rename = "upload_video")]
56    UploadVideo,
57    #[serde(rename = "record_audio")]
58    RecordAudio,
59    #[serde(rename = "upload_audio")]
60    UploadAudio,
61    #[serde(rename = "upload_document")]
62    UploadDocument,
63    #[serde(rename = "find_location")]
64    FindLocation,
65    #[serde(rename = "record_video_note")]
66    RecordVideoNote,
67    #[serde(rename = "upload_video_note")]
68    UploadVideoNote,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize, BotRequest)]
72pub struct SendChatActionRequest {
73    /// Unique identifier for the target chat or username of the target channel (in the format @channelusername)
74    pub chat_id: i64,
75
76    /// Unique identifier for the target message thread.
77    pub message_thread_id: Option<i64>,
78
79    /// Type of action to broadcast.
80    pub action: ChatAction,
81}
82
83impl SendChatActionRequest {
84    pub fn new(chat_id: i64, action: ChatAction) -> Self {
85        Self {
86            chat_id,
87            action,
88            message_thread_id: None,
89        }
90    }
91}
92
93/// API methods for sending, editing, and deleting messages.
94impl API {
95    /// Send a message.
96    pub async fn send_chat_action(&self, req: &SendChatActionRequest) -> anyhow::Result<bool> {
97        self.client.post("sendChatAction", req).await
98    }
99}