lark-channel 0.6.0

Lark/Feishu Channel SDK for Rust
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
use serde::Serialize;
use serde_json::Value;

use crate::message::{MessageContent, MessageId, Recipient};
use crate::validation::validate_path_identifier;
use crate::{Error, Result};

use super::{OpenApiClient, OpenApiTransport};

const MESSAGE_PATH: &str = "/open-apis/im/v1/messages";

impl<T> OpenApiClient<T>
where
    T: OpenApiTransport,
{
    pub async fn create_message(
        &self,
        recipient: Recipient,
        content: MessageContent,
    ) -> Result<MessageId> {
        self.create_message_with_options(recipient, content, MessageCreateOptions::default())
            .await
    }

    pub async fn create_message_with_options(
        &self,
        recipient: Recipient,
        content: MessageContent,
        options: MessageCreateOptions,
    ) -> Result<MessageId> {
        let recipient = CreateMessageRecipient::try_from(recipient)?;
        let content = OpenApiMessageContent::try_from(content)?;
        let path = format!(
            "{}?receive_id_type={}",
            MESSAGE_PATH, recipient.receive_id_type
        );
        let request = CreateMessageRequest {
            receive_id: recipient.receive_id,
            msg_type: content.msg_type,
            content: serde_json::to_string(&content.content)?,
            uuid: options.uuid,
        };
        let response: MessageResponse = self.post_tenant_json(&path, &request).await?;

        Ok(MessageId(response.data.message_id))
    }

    pub async fn reply_message(
        &self,
        parent_message_id: MessageId,
        content: MessageContent,
    ) -> Result<MessageId> {
        self.reply_message_with_options(parent_message_id, content, MessageReplyOptions::default())
            .await
    }

    pub async fn reply_message_with_options(
        &self,
        parent_message_id: MessageId,
        content: MessageContent,
        options: MessageReplyOptions,
    ) -> Result<MessageId> {
        validate_message_id(&parent_message_id)?;
        let content = OpenApiMessageContent::try_from(content)?;
        let path = format!("{MESSAGE_PATH}/{}/reply", parent_message_id.0);
        let request = ReplyMessageRequest {
            msg_type: content.msg_type,
            content: serde_json::to_string(&content.content)?,
            uuid: options.uuid,
            reply_in_thread: options.reply_in_thread,
        };
        let response: MessageResponse = self.post_tenant_json(&path, &request).await?;

        Ok(MessageId(response.data.message_id))
    }
}

pub(super) fn validate_message_id(message_id: &MessageId) -> Result<()> {
    validate_path_identifier(&message_id.0, "message_id")
}

/// Optional parameters for creating a new message.
///
/// `uuid` is a caller-provided idempotency key forwarded to Lark/Feishu.
/// Reuse the same value when retrying the same logical message, and use a
/// new value when sending different content.
///
/// `OpenApiClient` does not generate this value automatically. Higher-level
/// retry helpers should create one key per logical send and reuse it for all
/// retry attempts.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MessageCreateOptions {
    /// Request de-duplication key accepted by the Lark/Feishu create-message API.
    pub uuid: Option<String>,
}

impl MessageCreateOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_uuid(uuid: impl Into<String>) -> Self {
        Self {
            uuid: Some(uuid.into()),
        }
    }

    pub fn uuid(mut self, uuid: impl Into<String>) -> Self {
        self.uuid = Some(uuid.into());
        self
    }
}

/// Optional parameters for replying to an existing message.
///
/// `uuid` is a caller-provided idempotency key forwarded to Lark/Feishu.
/// Reuse the same value when retrying the same logical reply, and use a new
/// value when replying with different content.
///
/// `OpenApiClient` does not generate this value automatically. Higher-level
/// retry helpers should create one key per logical reply and reuse it for all
/// retry attempts.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct MessageReplyOptions {
    /// Request de-duplication key accepted by the Lark/Feishu reply-message API.
    pub uuid: Option<String>,
    /// Request that the reply is placed in a thread when the target chat supports it.
    pub reply_in_thread: Option<bool>,
}

impl MessageReplyOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_uuid(uuid: impl Into<String>) -> Self {
        Self {
            uuid: Some(uuid.into()),
            reply_in_thread: None,
        }
    }

    pub fn uuid(mut self, uuid: impl Into<String>) -> Self {
        self.uuid = Some(uuid.into());
        self
    }

    pub fn reply_in_thread(mut self, reply_in_thread: bool) -> Self {
        self.reply_in_thread = Some(reply_in_thread);
        self
    }
}

#[derive(Debug)]
struct CreateMessageRecipient {
    receive_id_type: &'static str,
    receive_id: String,
}

impl TryFrom<Recipient> for CreateMessageRecipient {
    type Error = Error;

    fn try_from(recipient: Recipient) -> Result<Self> {
        match recipient {
            Recipient::Chat(receive_id) => Ok(Self {
                receive_id_type: "chat_id",
                receive_id,
            }),
            Recipient::User(receive_id) => Ok(Self {
                receive_id_type: "open_id",
                receive_id,
            }),
        }
    }
}

struct OpenApiMessageContent {
    msg_type: String,
    content: Value,
}

impl TryFrom<MessageContent> for OpenApiMessageContent {
    type Error = Error;

    fn try_from(content: MessageContent) -> Result<Self> {
        match content {
            MessageContent::Text { text } => Ok(Self {
                msg_type: "text".to_owned(),
                content: serde_json::json!({ "text": text }),
            }),
            MessageContent::Post { post } => {
                post.validate()?;
                Ok(Self {
                    msg_type: "post".to_owned(),
                    content: serde_json::to_value(post)?,
                })
            }
            MessageContent::Image { image_key } => Ok(Self {
                msg_type: "image".to_owned(),
                content: serde_json::json!({
                    "image_key": validate_resource_key(image_key, "image_key")?
                }),
            }),
            MessageContent::File { file_key } => Ok(Self {
                msg_type: "file".to_owned(),
                content: serde_json::json!({
                    "file_key": validate_resource_key(file_key, "file_key")?
                }),
            }),
            MessageContent::Audio { file_key } => Ok(Self {
                msg_type: "audio".to_owned(),
                content: serde_json::json!({
                    "file_key": validate_resource_key(file_key, "file_key")?
                }),
            }),
            MessageContent::Media {
                file_key,
                image_key,
            } => {
                let mut content = serde_json::json!({
                    "file_key": validate_resource_key(file_key, "file_key")?
                });
                if let Some(image_key) = image_key {
                    content["image_key"] =
                        Value::String(validate_resource_key(image_key, "image_key")?);
                }
                Ok(Self {
                    msg_type: "media".to_owned(),
                    content,
                })
            }
            MessageContent::Card { card } => Ok(Self {
                msg_type: "interactive".to_owned(),
                content: card,
            }),
            MessageContent::CardReference { card_id } => {
                card_id.validate()?;
                Ok(Self {
                    msg_type: "interactive".to_owned(),
                    content: serde_json::json!({
                        "type": "card",
                        "data": { "card_id": card_id.as_str() }
                    }),
                })
            }
            MessageContent::Custom { msg_type, content } => Ok(Self { msg_type, content }),
        }
    }
}

fn validate_resource_key(key: String, field: &str) -> Result<String> {
    if key.trim().is_empty() {
        return Err(Error::Validation(format!("{field} must not be empty")));
    }
    Ok(key)
}

#[derive(Serialize)]
struct CreateMessageRequest {
    receive_id: String,
    msg_type: String,
    content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    uuid: Option<String>,
}

#[derive(Serialize)]
struct ReplyMessageRequest {
    msg_type: String,
    content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    uuid: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    reply_in_thread: Option<bool>,
}

#[derive(Debug, serde::Deserialize)]
struct MessageResponse {
    data: SendMessageData,
}

#[derive(Debug, serde::Deserialize)]
struct SendMessageData {
    message_id: String,
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;
    use crate::ChannelConfig;
    use crate::lark_openapi::HttpResponse;
    use crate::lark_openapi::test_support::{FakeTransport, block_on};

    fn message_client() -> (OpenApiClient<FakeTransport>, FakeTransport) {
        let transport = FakeTransport::new(vec![
            HttpResponse::json(
                200,
                json!({
                    "code": 0,
                    "msg": "ok",
                    "tenant_access_token": "tenant-token-1",
                    "expire": 7200
                }),
            ),
            HttpResponse::json(
                200,
                json!({
                    "code": 0,
                    "msg": "ok",
                    "data": { "message_id": "om_media" }
                }),
            ),
        ]);
        let client = OpenApiClient::new(ChannelConfig::new("cli_a", "secret"), transport.clone());
        (client, transport)
    }

    #[test]
    fn create_message_serializes_official_media_content_shapes() {
        let cases = [
            (
                MessageContent::Image {
                    image_key: "img_123".to_owned(),
                },
                "image",
                json!({ "image_key": "img_123" }),
            ),
            (
                MessageContent::File {
                    file_key: "file_123".to_owned(),
                },
                "file",
                json!({ "file_key": "file_123" }),
            ),
            (
                MessageContent::Audio {
                    file_key: "file_audio".to_owned(),
                },
                "audio",
                json!({ "file_key": "file_audio" }),
            ),
            (
                MessageContent::Media {
                    file_key: "file_video".to_owned(),
                    image_key: Some("img_cover".to_owned()),
                },
                "media",
                json!({
                    "file_key": "file_video",
                    "image_key": "img_cover"
                }),
            ),
            (
                MessageContent::Media {
                    file_key: "file_video".to_owned(),
                    image_key: None,
                },
                "media",
                json!({ "file_key": "file_video" }),
            ),
        ];

        for (content, expected_type, expected_content) in cases {
            let (client, transport) = message_client();

            block_on(client.create_message(Recipient::Chat("oc_123".to_owned()), content))
                .expect("sent media message");

            let body = &transport.calls()[1].body;
            assert_eq!(body["msg_type"], expected_type);
            assert_eq!(
                serde_json::from_str::<Value>(body["content"].as_str().expect("content string"))
                    .expect("content json"),
                expected_content
            );
        }
    }

    #[test]
    fn media_content_rejects_empty_keys_before_authentication() {
        let cases = [
            MessageContent::Image {
                image_key: " ".to_owned(),
            },
            MessageContent::File {
                file_key: String::new(),
            },
            MessageContent::Audio {
                file_key: "\t".to_owned(),
            },
            MessageContent::Media {
                file_key: "file_video".to_owned(),
                image_key: Some(String::new()),
            },
        ];

        for content in cases {
            let transport = FakeTransport::new(Vec::new());
            let client =
                OpenApiClient::new(ChannelConfig::new("cli_a", "secret"), transport.clone());

            let error =
                block_on(client.create_message(Recipient::Chat("oc_123".to_owned()), content))
                    .expect_err("empty resource key");

            assert!(matches!(error, Error::Validation(_)));
            assert!(transport.calls().is_empty());
        }
    }
}