openlark-docs 0.17.0

飞书开放平台云文档服务模块 - 文档、表格、知识库API (202 APIs, 100% 覆盖,不含旧版本)
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//! Lingo(飞书词典)v1 数据模型
//!
//! 说明:
//! - 本文件仅存放模型(不计入 API 数量)。
//! - 字段严格对齐飞书开放平台文档的 `lingo-v1` schema。

use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

/// 用户 ID 类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum UserIdType {
    /// 公开项说明。
    OpenId,
    /// 公开项说明。
    UnionId,
    /// 公开项说明。
    UserId,
}

impl UserIdType {
    /// 返回对应的字符串表示。
    pub fn as_str(&self) -> &'static str {
        match self {
            UserIdType::OpenId => "open_id",
            UserIdType::UnionId => "union_id",
            UserIdType::UserId => "user_id",
        }
    }
}

/// 语言类型(schema: Language)
#[derive(Debug, Clone, Copy, Serialize_repr, Deserialize_repr, PartialEq, Eq)]
#[repr(i32)]
pub enum Language {
    /// 公开项说明。
    ZhCn = 1,
    /// 公开项说明。
    EnUs = 2,
    /// 公开项说明。
    JaJp = 3,
}

/// 名称展示范围(display_status)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DisplayStatus {
    /// 是否允许在 IM 和 Doc 等场景进行高亮提示
    pub allow_highlight: bool,
    /// 是否允许在飞书中被搜索到 / 是否在搜索结果中展示
    pub allow_search: bool,
}

/// 词条名/别名(term)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Term {
    /// 名称
    pub key: String,
    /// 展示状态
    pub display_status: DisplayStatus,
}

/// 外部系统关联数据(outer_info)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct OuterInfo {
    /// 数据提供方(不能包含 '-')
    pub provider: String,
    /// 外部系统唯一 ID(不能包含 '-')
    pub outer_id: String,
}

/// 相关联系人(referer)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RelatedUser {
    /// 格式根据 user_id_type 不同需要符合 open_id、user_id、union_id 格式的有效 id
    pub id: String,
    /// 备注
    pub title: Option<String>,
}

/// 相关公开群(referer)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RelatedChat {
    /// 公开群 id
    pub id: String,
}

/// 相关飞书文档/wiki(referer)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RelatedDoc {
    /// 文档标题
    pub title: Option<String>,
    /// 文档 url
    pub url: Option<String>,
}

/// 相关值班号(referer)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RelatedOncall {
    /// 值班号 id
    pub id: String,
}

/// 其他网页链接(referer)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RelatedLink {
    /// 标题
    pub title: Option<String>,
    /// 网页链接
    pub url: Option<String>,
}

/// 相关词条(abbreviation)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Abbreviation {
    /// 其他相关词条 id
    pub id: Option<String>,
}

/// 分类引用(classification)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ClassificationRef {
    /// 二级分类 ID
    pub id: String,
    /// 对应一级分类 ID
    pub father_id: Option<String>,
}

/// 上传图片 token(baike_image)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct BaikeImage {
    /// 通过文件接口上传后的图片 token
    pub token: String,
}

/// 关联信息(related_meta)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RelatedMeta {
    /// 相关联系人
    pub users: Option<Vec<RelatedUser>>,
    /// 有关的公开群
    pub chats: Option<Vec<RelatedChat>>,
    /// 飞书文档或飞书 wiki
    pub docs: Option<Vec<RelatedDoc>>,
    /// 飞书值班号
    pub oncalls: Option<Vec<RelatedOncall>>,
    /// 其他网页链接
    pub links: Option<Vec<RelatedLink>>,
    /// 相关词条
    pub abbreviations: Option<Vec<Abbreviation>>,
    /// 当前词条所属分类(仅二级分类)
    pub classifications: Option<Vec<ClassificationRef>>,
    /// 上传的相关图片
    pub images: Option<Vec<BaikeImage>>,
}

/// 国际化词条释义(i18n_entry_desc)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct I18nEntryDesc {
    /// 语言类型
    pub language: Language,
    /// 纯文本释义
    pub description: Option<String>,
    /// 富文本描述
    pub rich_text: Option<String>,
}

/// 反馈统计数据(statistics)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Statistics {
    /// 点赞数量
    pub like_count: i32,
    /// 点踩数量
    pub dislike_count: i32,
}

/// 创建/更新免审词条请求体(entity)
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct EntityInput {
    /// 词条名(最大 1 个)
    #[serde(default)]
    pub main_keys: Vec<Term>,
    /// 别名
    #[serde(default)]
    pub aliases: Option<Vec<Term>>,
    /// 纯文本释义(当填写 rich_text 时会失效)
    #[serde(default)]
    pub description: Option<String>,
    /// 关联信息
    #[serde(default)]
    pub related_meta: Option<RelatedMeta>,
    /// 外部系统关联数据
    #[serde(default)]
    pub outer_info: Option<OuterInfo>,
    /// 富文本释义
    #[serde(default)]
    pub rich_text: Option<String>,
    /// 国际化的词条释义
    #[serde(default)]
    pub i18n_descs: Option<Vec<I18nEntryDesc>>,
}

/// 创建/更新草稿请求体(entity)
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct DraftEntityInput {
    /// 词条 ID(可选)
    #[serde(default)]
    pub id: Option<String>,
    /// 词条名(最大 1 个)
    #[serde(default)]
    pub main_keys: Vec<Term>,
    /// 别名
    #[serde(default)]
    pub aliases: Option<Vec<Term>>,
    /// 纯文本释义(当填写 rich_text 时会失效)
    #[serde(default)]
    pub description: Option<String>,
    /// 关联信息
    #[serde(default)]
    pub related_meta: Option<RelatedMeta>,
    /// 外部系统关联数据(仅创建草稿时支持)
    #[serde(default)]
    pub outer_info: Option<OuterInfo>,
    /// 富文本释义
    #[serde(default)]
    pub rich_text: Option<String>,
    /// 国际化的词条释义
    #[serde(default)]
    pub i18n_descs: Option<Vec<I18nEntryDesc>>,
}

/// 更新草稿请求体(entity)
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct DraftUpdateEntityInput {
    /// 词条 ID(可选)
    #[serde(default)]
    pub id: Option<String>,
    /// 词条名(最大 1 个)
    #[serde(default)]
    pub main_keys: Vec<Term>,
    /// 别名
    #[serde(default)]
    pub aliases: Option<Vec<Term>>,
    /// 纯文本释义(当填写 rich_text 时会失效)
    #[serde(default)]
    pub description: Option<String>,
    /// 关联信息
    #[serde(default)]
    pub related_meta: Option<RelatedMeta>,
    /// 富文本释义
    #[serde(default)]
    pub rich_text: Option<String>,
    /// 国际化的词条释义
    #[serde(default)]
    pub i18n_descs: Option<Vec<I18nEntryDesc>>,
}

/// 免审词条实体(entity)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Entity {
    /// 词条 ID
    pub id: Option<String>,
    /// 词条名(最大 1 个)
    pub main_keys: Vec<Term>,
    /// 别名
    pub aliases: Option<Vec<Term>>,
    /// 纯文本释义
    pub description: Option<String>,
    /// 创建者
    pub creator: Option<String>,
    /// 词条创建时间(秒级时间戳)
    pub create_time: Option<String>,
    /// 更新者
    pub updater: Option<String>,
    /// 最近一次更新词条时间(秒级时间戳)
    pub update_time: Option<String>,
    /// 关联信息
    pub related_meta: Option<RelatedMeta>,
    /// 反馈统计数据
    pub statistics: Option<Statistics>,
    /// 外部系统关联数据
    pub outer_info: Option<OuterInfo>,
    /// 富文本释义
    pub rich_text: Option<String>,
    /// 词条的创建来源
    pub source: Option<i32>,
    /// 国际化释义
    pub i18n_descs: Option<Vec<I18nEntryDesc>>,
}

/// 草稿信息(draft)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Draft {
    /// 草稿 ID
    pub draft_id: String,
    /// 词条信息
    pub entity: Entity,
}

/// 分类国际化名称(i18n_cls_name)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct I18nClsName {
    /// 公开项说明。
    pub language: Language,
    /// 公开项说明。
    pub name: String,
}

/// 分类(classification)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Classification {
    /// 二级分类 ID
    pub id: String,
    /// 二级分类名称
    pub name: Option<String>,
    /// 对应一级分类 ID
    pub father_id: Option<String>,
    /// 国际化分类名
    pub i18n_names: Option<Vec<I18nClsName>>,
}

/// 词库(repo)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Repo {
    /// 词库 id
    pub id: String,
    /// 词库名
    pub name: String,
}

/// 兼容导出:保留历史类型名(不建议在新代码中使用)
pub type LingoEntity = Entity;
/// 公开项说明。
pub type LingoDraft = Draft;

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

    fn test_roundtrip<T: Serialize + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug>(
        original: &T,
    ) {
        let json = serde_json::to_string(original).expect("序列化失败");
        let deserialized: T = serde_json::from_str(&json).expect("反序列化失败");
        assert_eq!(original, &deserialized, "roundtrip 后数据不一致");
    }

    #[test]
    fn test_user_id_type_serialization() {
        test_roundtrip(&UserIdType::OpenId);
        test_roundtrip(&UserIdType::UnionId);
        test_roundtrip(&UserIdType::UserId);
    }

    #[test]
    fn test_language_serialization() {
        test_roundtrip(&Language::ZhCn);
        test_roundtrip(&Language::EnUs);
        test_roundtrip(&Language::JaJp);
    }

    #[test]
    fn test_display_status_serialization() {
        let status = DisplayStatus {
            allow_highlight: true,
            allow_search: false,
        };
        test_roundtrip(&status);
    }

    #[test]
    fn test_term_serialization() {
        let term = Term {
            key: "测试词条".to_string(),
            display_status: DisplayStatus {
                allow_highlight: true,
                allow_search: true,
            },
        };
        test_roundtrip(&term);
    }

    #[test]
    fn test_outer_info_serialization() {
        let info = OuterInfo {
            provider: "provider1".to_string(),
            outer_id: "outer123".to_string(),
        };
        test_roundtrip(&info);
    }

    #[test]
    fn test_related_user_serialization() {
        let user = RelatedUser {
            id: "user123".to_string(),
            title: Some("负责人".to_string()),
        };
        test_roundtrip(&user);
    }

    #[test]
    fn test_related_chat_serialization() {
        let chat = RelatedChat {
            id: "chat456".to_string(),
        };
        test_roundtrip(&chat);
    }

    #[test]
    fn test_related_doc_serialization() {
        let doc = RelatedDoc {
            title: Some("文档标题".to_string()),
            url: Some("https://doc.example.com".to_string()),
        };
        test_roundtrip(&doc);
    }

    #[test]
    fn test_related_oncall_serialization() {
        let oncall = RelatedOncall {
            id: "oncall789".to_string(),
        };
        test_roundtrip(&oncall);
    }

    #[test]
    fn test_related_link_serialization() {
        let link = RelatedLink {
            title: Some("链接标题".to_string()),
            url: Some("https://example.com".to_string()),
        };
        test_roundtrip(&link);
    }

    #[test]
    fn test_abbreviation_serialization() {
        let abbr = Abbreviation {
            id: Some("abbr123".to_string()),
        };
        test_roundtrip(&abbr);
    }

    #[test]
    fn test_classification_ref_serialization() {
        let class = ClassificationRef {
            id: "class456".to_string(),
            father_id: Some("father123".to_string()),
        };
        test_roundtrip(&class);
    }

    #[test]
    fn test_baike_image_serialization() {
        let img = BaikeImage {
            token: "img_token_123".to_string(),
        };
        test_roundtrip(&img);
    }

    #[test]
    fn test_related_meta_serialization() {
        let meta = RelatedMeta {
            users: Some(vec![RelatedUser {
                id: "user1".to_string(),
                title: None,
            }]),
            chats: None,
            docs: None,
            oncalls: None,
            links: None,
            abbreviations: None,
            classifications: None,
            images: None,
        };
        test_roundtrip(&meta);
    }

    #[test]
    fn test_i18n_entry_desc_serialization() {
        let desc = I18nEntryDesc {
            language: Language::ZhCn,
            description: Some("描述".to_string()),
            rich_text: Some("<p>富文本</p>".to_string()),
        };
        test_roundtrip(&desc);
    }

    #[test]
    fn test_statistics_serialization() {
        let stats = Statistics {
            like_count: 100,
            dislike_count: 5,
        };
        test_roundtrip(&stats);
    }

    #[test]
    fn test_entity_input_serialization() {
        let input = EntityInput {
            main_keys: vec![Term {
                key: "主词条".to_string(),
                display_status: DisplayStatus {
                    allow_highlight: true,
                    allow_search: true,
                },
            }],
            aliases: None,
            description: Some("词条描述".to_string()),
            related_meta: None,
            outer_info: None,
            rich_text: None,
            i18n_descs: None,
        };
        test_roundtrip(&input);
    }

    #[test]
    fn test_entity_serialization() {
        let entity = Entity {
            id: Some("entity123".to_string()),
            main_keys: vec![Term {
                key: "词条名".to_string(),
                display_status: DisplayStatus {
                    allow_highlight: true,
                    allow_search: true,
                },
            }],
            aliases: None,
            description: Some("描述".to_string()),
            creator: Some("creator1".to_string()),
            create_time: Some("1234567890".to_string()),
            updater: None,
            update_time: None,
            related_meta: None,
            statistics: None,
            outer_info: None,
            rich_text: None,
            source: Some(1),
            i18n_descs: None,
        };
        test_roundtrip(&entity);
    }

    #[test]
    fn test_classification_serialization() {
        let class = Classification {
            id: "class123".to_string(),
            name: Some("分类名称".to_string()),
            father_id: Some("father123".to_string()),
            i18n_names: None,
        };
        test_roundtrip(&class);
    }

    #[test]
    fn test_repo_serialization() {
        let repo = Repo {
            id: "repo123".to_string(),
            name: "词库名称".to_string(),
        };
        test_roundtrip(&repo);
    }

    #[test]
    fn test_i18n_cls_name_serialization() {
        let name = I18nClsName {
            language: Language::EnUs,
            name: "English Name".to_string(),
        };
        test_roundtrip(&name);
    }
}