openlark 0.15.0

飞书开放平台 Rust SDK - 企业级高覆盖率 API 客户端,极简依赖一条命令
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use std::sync::Arc;
use std::time::Duration;

use open_lark::auth::AuthService;
use open_lark::communication::endpoints::IM_V1_MESSAGES;
use open_lark::ws_client::{EventDispatcherHandler, LarkWsClient};
use open_lark::{Config, CoreConfig};
use serde::Deserialize;
use serde_json::json;
use tokio::sync::mpsc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    dotenvy::dotenv().ok();

    let runtime_config = RuntimeConfig::from_env()?;
    println!("🚀 启动 WebSocket Echo Bot");
    println!("🌐 API Base URL: {}", runtime_config.base_url);

    let ws_config = Config::builder()
        .app_id(runtime_config.app_id.clone())
        .app_secret(runtime_config.app_secret.clone())
        .base_url(runtime_config.base_url.clone())
        .timeout(Duration::from_secs(runtime_config.timeout_secs))
        .build()
        .map_err(|e| format!("构建 WebSocket 配置失败: {e}"))?;

    let (payload_tx, payload_rx) = mpsc::unbounded_channel::<Vec<u8>>();
    tokio::spawn(process_payload_loop(payload_rx, runtime_config.clone()));

    let event_handler = EventDispatcherHandler::builder()
        .payload_sender(payload_tx)
        .build();

    println!("🔌 正在建立飞书长连接...");
    LarkWsClient::open(Arc::new(ws_config), event_handler).await?;
    Ok(())
}

#[derive(Debug, Clone)]
struct RuntimeConfig {
    app_id: String,
    app_secret: String,
    base_url: String,
    timeout_secs: u64,
}

impl RuntimeConfig {
    fn from_env() -> Result<Self, Box<dyn std::error::Error>> {
        let app_id =
            std::env::var("OPENLARK_APP_ID").map_err(|_| "未找到环境变量 OPENLARK_APP_ID")?;
        let app_secret = std::env::var("OPENLARK_APP_SECRET")
            .map_err(|_| "未找到环境变量 OPENLARK_APP_SECRET")?;
        let base_url = std::env::var("OPENLARK_BASE_URL")
            .unwrap_or_else(|_| "https://open.feishu.cn".to_string());
        let timeout_secs = std::env::var("OPENLARK_TIMEOUT")
            .ok()
            .and_then(|v| v.parse::<u64>().ok())
            .unwrap_or(30);

        Ok(Self {
            app_id,
            app_secret,
            base_url,
            timeout_secs,
        })
    }
}

async fn process_payload_loop(
    mut payload_rx: mpsc::UnboundedReceiver<Vec<u8>>,
    runtime_config: RuntimeConfig,
) {
    while let Some(payload) = payload_rx.recv().await {
        if let Err(err) = handle_payload(&runtime_config, &payload).await {
            eprintln!("❌ 处理事件失败: {err}");
        }
    }
}

async fn handle_payload(
    runtime_config: &RuntimeConfig,
    payload: &[u8],
) -> Result<(), Box<dyn std::error::Error>> {
    let envelope: EventEnvelope = match serde_json::from_slice(payload) {
        Ok(v) => v,
        Err(err) => {
            eprintln!("⚠️ 忽略无法解析的事件载荷: {err}");
            return Ok(());
        }
    };

    if envelope.header.event_type != "im.message.receive_v1" {
        return Ok(());
    }

    if envelope.event.message.message_type != "text" {
        println!("ℹ️ 跳过非文本消息: {}", envelope.event.message.message_type);
        return Ok(());
    }

    let text = extract_text(&envelope.event.message.content)?;
    if text.trim().is_empty() {
        println!("ℹ️ 跳过空文本消息");
        return Ok(());
    }

    let (receive_id, receive_id_type) = resolve_receive_target(&envelope.event)?;
    send_echo_message(runtime_config, &receive_id, receive_id_type, &text).await?;

    println!("✅ Echo 成功: receive_id_type={receive_id_type}, receive_id={receive_id}");
    Ok(())
}

fn extract_text(content: &str) -> Result<String, Box<dyn std::error::Error>> {
    let content_json: TextContent =
        serde_json::from_str(content).map_err(|e| format!("解析文本消息 content 失败: {e}"))?;

    Ok(content_json.text)
}

fn resolve_receive_target(
    event: &EventBody,
) -> Result<(String, &'static str), Box<dyn std::error::Error>> {
    if event.message.chat_type == "p2p" {
        let open_id = event.sender.sender_id.open_id.clone();
        if open_id.is_empty() {
            return Err("p2p 消息缺少 sender.open_id".into());
        }
        return Ok((open_id, "open_id"));
    }

    if let Some(chat) = &event.chat {
        if !chat.chat_id.is_empty() {
            return Ok((chat.chat_id.clone(), "chat_id"));
        }
    }

    if let Some(chat_id) = &event.message.chat_id {
        if !chat_id.is_empty() {
            return Ok((chat_id.clone(), "chat_id"));
        }
    }

    Err("群聊消息缺少 chat_id".into())
}

async fn send_echo_message(
    runtime_config: &RuntimeConfig,
    receive_id: &str,
    receive_id_type: &str,
    text: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    let app_access_token = fetch_app_access_token(runtime_config).await?;

    let body = json!({
        "receive_id": receive_id,
        "msg_type": "text",
        "content": json!({ "text": text }).to_string()
    });

    let base_url = runtime_config.base_url.trim_end_matches('/');
    let url = format!("{base_url}{IM_V1_MESSAGES}");

    let client = reqwest::Client::builder()
        .timeout(Duration::from_secs(runtime_config.timeout_secs))
        .build()?;

    let response = client
        .post(url)
        .query(&[("receive_id_type", receive_id_type)])
        .header("Authorization", format!("Bearer {app_access_token}"))
        .header("Content-Type", "application/json")
        .json(&body)
        .send()
        .await?;

    if !response.status().is_success() {
        let status = response.status();
        let response_text = response.text().await.unwrap_or_default();
        return Err(format!("发送 Echo 失败: {status}, body={response_text}").into());
    }

    Ok(())
}

async fn fetch_app_access_token(
    runtime_config: &RuntimeConfig,
) -> Result<String, Box<dyn std::error::Error>> {
    let core_config = CoreConfig::builder()
        .app_id(runtime_config.app_id.clone())
        .app_secret(runtime_config.app_secret.clone())
        .base_url(runtime_config.base_url.clone())
        .req_timeout(Duration::from_secs(runtime_config.timeout_secs))
        .build();
    let auth_service = AuthService::new(core_config);
    let token_response = auth_service
        .v3()
        .app_access_token_internal()
        .app_id(runtime_config.app_id.clone())
        .app_secret(runtime_config.app_secret.clone())
        .execute()
        .await
        .map_err(|e| format!("获取 app_access_token 失败: {e}"))?;

    Ok(token_response.data.app_access_token)
}

#[derive(Debug, Deserialize)]
struct EventEnvelope {
    header: EventHeader,
    event: EventBody,
}

#[derive(Debug, Deserialize)]
struct EventHeader {
    event_type: String,
}

#[derive(Debug, Deserialize)]
struct EventBody {
    sender: Sender,
    message: Message,
    #[serde(default)]
    chat: Option<Chat>,
}

#[derive(Debug, Deserialize)]
struct Sender {
    sender_id: SenderId,
}

#[derive(Debug, Deserialize)]
struct SenderId {
    open_id: String,
}

#[derive(Debug, Deserialize)]
struct Message {
    message_type: String,
    content: String,
    chat_type: String,
    #[serde(default)]
    chat_id: Option<String>,
}

#[derive(Debug, Deserialize)]
struct Chat {
    chat_id: String,
}

#[derive(Debug, Deserialize)]
struct TextContent {
    text: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_text_success() {
        let text = extract_text(r#"{"text":"hello"}"#).expect("should parse text content");
        assert_eq!(text, "hello");
    }

    #[test]
    fn test_resolve_receive_target_for_p2p() {
        let event = EventBody {
            sender: Sender {
                sender_id: SenderId {
                    open_id: "ou_test_user".to_string(),
                },
            },
            message: Message {
                message_type: "text".to_string(),
                content: "{\"text\":\"hello\"}".to_string(),
                chat_type: "p2p".to_string(),
                chat_id: None,
            },
            chat: None,
        };

        let (receive_id, receive_id_type) =
            resolve_receive_target(&event).expect("should resolve p2p receive target");
        assert_eq!(receive_id, "ou_test_user");
        assert_eq!(receive_id_type, "open_id");
    }

    #[test]
    fn test_resolve_receive_target_for_group() {
        let event = EventBody {
            sender: Sender {
                sender_id: SenderId {
                    open_id: "ou_test_user".to_string(),
                },
            },
            message: Message {
                message_type: "text".to_string(),
                content: "{\"text\":\"hello\"}".to_string(),
                chat_type: "group".to_string(),
                chat_id: None,
            },
            chat: Some(Chat {
                chat_id: "oc_group_001".to_string(),
            }),
        };

        let (receive_id, receive_id_type) =
            resolve_receive_target(&event).expect("should resolve group receive target");
        assert_eq!(receive_id, "oc_group_001");
        assert_eq!(receive_id_type, "chat_id");
    }

    #[test]
    fn test_extract_text_invalid_json() {
        let result = extract_text("not-json");
        assert!(result.is_err());
    }

    #[test]
    fn test_resolve_receive_target_group_fallback_message_chat_id() {
        let event = EventBody {
            sender: Sender {
                sender_id: SenderId {
                    open_id: "ou_test_user".to_string(),
                },
            },
            message: Message {
                message_type: "text".to_string(),
                content: "{\"text\":\"hello\"}".to_string(),
                chat_type: "group".to_string(),
                chat_id: Some("oc_group_from_message".to_string()),
            },
            chat: None,
        };

        let (receive_id, receive_id_type) =
            resolve_receive_target(&event).expect("should resolve from message.chat_id");
        assert_eq!(receive_id, "oc_group_from_message");
        assert_eq!(receive_id_type, "chat_id");
    }

    #[test]
    fn test_resolve_receive_target_p2p_missing_open_id() {
        let event = EventBody {
            sender: Sender {
                sender_id: SenderId {
                    open_id: String::new(),
                },
            },
            message: Message {
                message_type: "text".to_string(),
                content: "{\"text\":\"hello\"}".to_string(),
                chat_type: "p2p".to_string(),
                chat_id: None,
            },
            chat: None,
        };

        let result = resolve_receive_target(&event);
        assert!(result.is_err());
    }

    #[test]
    fn test_resolve_receive_target_group_missing_chat_id() {
        let event = EventBody {
            sender: Sender {
                sender_id: SenderId {
                    open_id: "ou_test_user".to_string(),
                },
            },
            message: Message {
                message_type: "text".to_string(),
                content: "{\"text\":\"hello\"}".to_string(),
                chat_type: "group".to_string(),
                chat_id: None,
            },
            chat: None,
        };

        let result = resolve_receive_target(&event);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_handle_payload_invalid_json_returns_ok() {
        let runtime_config = RuntimeConfig {
            app_id: "cli_test".to_string(),
            app_secret: "secret".to_string(),
            base_url: "https://open.feishu.cn".to_string(),
            timeout_secs: 3,
        };

        let result = handle_payload(&runtime_config, b"invalid-json").await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_payload_non_message_event_returns_ok() {
        let runtime_config = RuntimeConfig {
            app_id: "cli_test".to_string(),
            app_secret: "secret".to_string(),
            base_url: "https://open.feishu.cn".to_string(),
            timeout_secs: 3,
        };

        let payload = serde_json::to_vec(&json!({
            "header": {"event_type": "im.chat.member.added_v1"},
            "event": {
                "sender": {"sender_id": {"open_id": "ou_test"}},
                "message": {
                    "message_type": "text",
                    "content": "{\"text\":\"hi\"}",
                    "chat_type": "p2p"
                }
            }
        }))
        .expect("serialize payload");

        let result = handle_payload(&runtime_config, &payload).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_payload_non_text_event_returns_ok() {
        let runtime_config = RuntimeConfig {
            app_id: "cli_test".to_string(),
            app_secret: "secret".to_string(),
            base_url: "https://open.feishu.cn".to_string(),
            timeout_secs: 3,
        };

        let payload = serde_json::to_vec(&json!({
            "header": {"event_type": "im.message.receive_v1"},
            "event": {
                "sender": {"sender_id": {"open_id": "ou_test"}},
                "message": {
                    "message_type": "image",
                    "content": "{}",
                    "chat_type": "p2p"
                }
            }
        }))
        .expect("serialize payload");

        let result = handle_payload(&runtime_config, &payload).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_payload_empty_text_returns_ok() {
        let runtime_config = RuntimeConfig {
            app_id: "cli_test".to_string(),
            app_secret: "secret".to_string(),
            base_url: "https://open.feishu.cn".to_string(),
            timeout_secs: 3,
        };

        let payload = serde_json::to_vec(&json!({
            "header": {"event_type": "im.message.receive_v1"},
            "event": {
                "sender": {"sender_id": {"open_id": "ou_test"}},
                "message": {
                    "message_type": "text",
                    "content": "{\"text\":\"   \"}",
                    "chat_type": "p2p"
                }
            }
        }))
        .expect("serialize payload");

        let result = handle_payload(&runtime_config, &payload).await;
        assert!(result.is_ok());
    }
}