Skip to main content

larkrs_client/bot/
mod.rs

1use serde::{Deserialize, Serialize};
2
3pub mod chat;
4
5#[derive(Debug, Serialize, Deserialize, Default)]
6pub struct ChatListResponse {
7    pub items: Vec<ChatInfo>,
8    #[serde(default)]
9    pub page_token: Option<String>,
10    #[serde(default)]
11    pub has_more: bool,
12}
13
14#[derive(Debug, Serialize, Deserialize)]
15pub struct ChatInfo {
16    pub chat_id: String,
17    pub name: String,
18    pub avatar: Option<String>,
19    pub description: Option<String>,
20    pub owner_id: Option<String>,
21    pub owner_id_type: Option<String>,
22    #[serde(default)]
23    pub chat_mode: Option<String>,
24    #[serde(default)]
25    pub chat_type: Option<String>,
26    #[serde(default)]
27    pub external: Option<bool>,
28}
29
30#[derive(Debug, Serialize, Deserialize)]
31pub struct ChatInfoItem {
32    pub chat_id: String,
33    pub name: String,
34}
35
36impl From<ChatListResponse> for Vec<ChatInfoItem> {
37    fn from(response: ChatListResponse) -> Self {
38        response
39            .items
40            .into_iter()
41            .map(|chat| ChatInfoItem {
42                chat_id: chat.chat_id,
43                name: chat.name,
44            })
45            .collect()
46    }
47}
48
49#[derive(Debug, Serialize, Deserialize)]
50pub struct SendMessageRequest {
51    pub content: String,
52    pub msg_type: String,
53    pub receive_id: String,
54}
55
56#[derive(Debug, Serialize, Deserialize)]
57pub struct TextContent {
58    pub text: String,
59}
60
61impl TextContent {
62    pub fn new(text: &str) -> Self {
63        Self {
64            text: text.to_string(),
65        }
66    }
67}
68
69/// Markdown content structure for Feishu messages
70#[derive(Debug, Serialize, Deserialize)]
71pub struct MarkdownContent {
72    pub zh_cn: MarkdownLanguageContent,
73}
74
75#[derive(Debug, Serialize, Deserialize)]
76pub struct MarkdownLanguageContent {
77    pub title: String,
78    pub content: Vec<Vec<MarkdownElement>>,
79}
80
81#[derive(Debug, Serialize, Deserialize)]
82pub struct MarkdownElement {
83    pub tag: String,
84    pub text: String,
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub style: Option<Vec<String>>,
87}
88
89impl SendMessageRequest {
90    pub fn text(receive_id: &str, content: &str) -> Self {
91        let text_content = TextContent::new(content);
92        Self {
93            content: serde_json::to_string(&text_content).unwrap_or_default(),
94            msg_type: "text".to_string(),
95            receive_id: receive_id.to_string(),
96        }
97    }
98
99    /// Create a markdown message request
100    pub fn markdown(receive_id: &str, title: &str, elements: Vec<Vec<MarkdownElement>>) -> Self {
101        let markdown_content = MarkdownContent {
102            zh_cn: MarkdownLanguageContent {
103                title: title.to_string(),
104                content: elements,
105            },
106        };
107
108        Self {
109            content: serde_json::to_string(&markdown_content).unwrap_or_default(),
110            msg_type: "post".to_string(),
111            receive_id: receive_id.to_string(),
112        }
113    }
114}