dingding 0.1.1

DingTalk SDK and bot framework 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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
use std::{fmt, str::FromStr};

use serde::Serialize;
use url::Url;

use crate::{Error, Result};

/// DingTalk webhook message.
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "msgtype")]
pub enum WebhookMessage {
    /// Plain text message.
    #[serde(rename = "text")]
    Text {
        /// Message body.
        text: TextContent,
        /// Optional mention metadata.
        #[serde(skip_serializing_if = "Option::is_none")]
        at: Option<At>,
    },
    /// Markdown message.
    #[serde(rename = "markdown")]
    Markdown {
        /// Message body.
        markdown: MarkdownContent,
        /// Optional mention metadata.
        #[serde(skip_serializing_if = "Option::is_none")]
        at: Option<At>,
    },
    /// Link message.
    #[serde(rename = "link")]
    Link {
        /// Message body.
        link: LinkContent,
    },
    /// Action card message.
    #[serde(rename = "actionCard")]
    ActionCard {
        /// Message body.
        #[serde(rename = "actionCard")]
        action_card: ActionCardContent,
    },
    /// Feed card message.
    #[serde(rename = "feedCard")]
    FeedCard {
        /// Message body.
        #[serde(rename = "feedCard")]
        feed_card: FeedCardContent,
    },
}

impl WebhookMessage {
    /// Creates a text message.
    #[must_use]
    pub fn text(content: impl Into<String>) -> Self {
        Self::Text {
            text: TextContent {
                content: content.into(),
            },
            at: None,
        }
    }

    /// Creates a markdown message.
    #[must_use]
    pub fn markdown(title: impl Into<String>, text: impl Into<String>) -> Self {
        Self::Markdown {
            markdown: MarkdownContent {
                title: title.into(),
                text: text.into(),
            },
            at: None,
        }
    }

    /// Creates a link message.
    #[must_use]
    pub fn link(
        title: impl Into<String>,
        text: impl Into<String>,
        message_url: impl Into<String>,
    ) -> Self {
        Self::Link {
            link: LinkContent {
                title: title.into(),
                text: text.into(),
                message_url: trimmed_string(message_url),
                pic_url: None,
            },
        }
    }

    /// Creates a single-button action card message.
    #[must_use]
    pub fn action_card(
        title: impl Into<String>,
        text: impl Into<String>,
        button_title: impl Into<String>,
        button_url: impl Into<String>,
    ) -> Self {
        Self::ActionCard {
            action_card: ActionCardContent {
                title: title.into(),
                text: text.into(),
                btn_orientation: None,
                single_title: Some(button_title.into()),
                single_url: Some(trimmed_string(button_url)),
                btns: None,
            },
        }
    }

    /// Creates a multi-button action card message.
    #[must_use]
    pub fn action_card_buttons(
        title: impl Into<String>,
        text: impl Into<String>,
        buttons: Vec<ActionCardButton>,
    ) -> Self {
        Self::ActionCard {
            action_card: ActionCardContent {
                title: title.into(),
                text: text.into(),
                btn_orientation: None,
                single_title: None,
                single_url: None,
                btns: Some(buttons),
            },
        }
    }

    /// Creates a feed card message.
    #[must_use]
    pub fn feed_card(links: Vec<FeedCardLink>) -> Self {
        Self::FeedCard {
            feed_card: FeedCardContent { links },
        }
    }

    /// Adds mention metadata to text or markdown messages.
    #[must_use]
    pub fn at(mut self, at: At) -> Self {
        match &mut self {
            Self::Text { at: slot, .. } | Self::Markdown { at: slot, .. } => {
                *slot = (!at.is_empty()).then_some(at);
            }
            Self::Link { .. } | Self::ActionCard { .. } | Self::FeedCard { .. } => {}
        }
        self
    }

    /// Adds an image URL to a link message.
    #[must_use]
    pub fn image_url(mut self, image_url: impl Into<String>) -> Self {
        if let Self::Link { link } = &mut self {
            link.pic_url = Some(trimmed_string(image_url));
        }
        self
    }

    /// Sets action-card button orientation.
    #[must_use]
    pub fn button_orientation(mut self, orientation: ButtonOrientation) -> Self {
        if let Self::ActionCard { action_card } = &mut self {
            action_card.btn_orientation = Some(orientation.as_dingtalk_value().to_string());
        }
        self
    }

    /// Validates this message before sending.
    pub fn validate(&self) -> Result<()> {
        match self {
            Self::Text { text, at } => {
                validate_non_empty(&text.content, "text.content")?;
                if let Some(at) = at {
                    at.validate()?;
                }
                Ok(())
            }
            Self::Markdown { markdown, at } => {
                validate_non_empty(&markdown.title, "markdown.title")?;
                validate_non_empty(&markdown.text, "markdown.text")?;
                if let Some(at) = at {
                    at.validate()?;
                }
                Ok(())
            }
            Self::Link { link } => {
                validate_non_empty(&link.title, "link.title")?;
                validate_non_empty(&link.text, "link.text")?;
                validate_http_url(&link.message_url, "link.message_url")?;
                if let Some(pic_url) = &link.pic_url {
                    validate_http_url(pic_url, "link.pic_url")?;
                }
                Ok(())
            }
            Self::ActionCard { action_card } => action_card.validate(),
            Self::FeedCard { feed_card } => {
                if feed_card.links.is_empty() {
                    return Err(Error::invalid_input(
                        "feed_card.links",
                        "at least one link is required",
                    ));
                }

                for link in &feed_card.links {
                    link.validate()?;
                }

                Ok(())
            }
        }
    }
}

fn validate_non_empty(value: &str, field: &'static str) -> Result<()> {
    if value.trim().is_empty() {
        Err(Error::invalid_input(field, "value must not be empty"))
    } else {
        Ok(())
    }
}

fn validate_http_url(value: &str, field: &'static str) -> Result<()> {
    validate_non_empty(value, field)?;
    let parsed = Url::parse(value)
        .map_err(|source| Error::invalid_input(field, format!("invalid URL: {source}")))?;
    if matches!(parsed.scheme(), "http" | "https") {
        Ok(())
    } else {
        Err(Error::invalid_input(
            field,
            "URL scheme must be http or https",
        ))
    }
}

fn trimmed_string(value: impl Into<String>) -> String {
    value.into().trim().to_string()
}

/// Action-card button orientation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ButtonOrientation {
    /// Buttons are arranged vertically.
    Vertical,
    /// Buttons are arranged horizontally.
    Horizontal,
}

impl ButtonOrientation {
    /// Parses a DingTalk wire value or a human-readable label.
    ///
    /// Accepts `0` / `vertical` and `1` / `horizontal`.
    #[must_use]
    pub fn from_dingtalk_value(value: impl AsRef<str>) -> Option<Self> {
        match value.as_ref().trim().to_ascii_lowercase().as_str() {
            "0" | "vertical" => Some(Self::Vertical),
            "1" | "horizontal" => Some(Self::Horizontal),
            _ => None,
        }
    }

    /// Returns the DingTalk wire value for this orientation.
    #[must_use]
    pub fn as_dingtalk_value(self) -> &'static str {
        match self {
            Self::Vertical => "0",
            Self::Horizontal => "1",
        }
    }

    /// Returns a stable lowercase label for logs and configuration.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Vertical => "vertical",
            Self::Horizontal => "horizontal",
        }
    }

    /// Returns whether buttons are arranged vertically.
    #[must_use]
    pub fn is_vertical(self) -> bool {
        matches!(self, Self::Vertical)
    }

    /// Returns whether buttons are arranged horizontally.
    #[must_use]
    pub fn is_horizontal(self) -> bool {
        matches!(self, Self::Horizontal)
    }
}

impl FromStr for ButtonOrientation {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self> {
        Self::from_dingtalk_value(value).ok_or_else(|| {
            Error::invalid_input(
                "button_orientation",
                "value must be vertical, horizontal, 0, or 1",
            )
        })
    }
}

impl fmt::Display for ButtonOrientation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Text body.
#[derive(Debug, Clone, Serialize)]
pub struct TextContent {
    /// Text content.
    pub content: String,
}

/// Markdown body.
#[derive(Debug, Clone, Serialize)]
pub struct MarkdownContent {
    /// Title shown in notification surfaces.
    pub title: String,
    /// Markdown text.
    pub text: String,
}

/// Link body.
#[derive(Debug, Clone, Serialize)]
pub struct LinkContent {
    /// Link title.
    pub title: String,
    /// Link text.
    pub text: String,
    /// Link target.
    #[serde(rename = "messageUrl")]
    pub message_url: String,
    /// Optional image URL.
    #[serde(rename = "picUrl", skip_serializing_if = "Option::is_none")]
    pub pic_url: Option<String>,
}

/// Action card body.
#[derive(Debug, Clone, Serialize)]
pub struct ActionCardContent {
    /// Card title.
    pub title: String,
    /// Markdown text.
    pub text: String,
    /// Button orientation.
    #[serde(rename = "btnOrientation", skip_serializing_if = "Option::is_none")]
    pub btn_orientation: Option<String>,
    /// Single button title.
    #[serde(rename = "singleTitle", skip_serializing_if = "Option::is_none")]
    pub single_title: Option<String>,
    /// Single button URL.
    #[serde(rename = "singleURL", skip_serializing_if = "Option::is_none")]
    pub single_url: Option<String>,
    /// Multiple buttons.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub btns: Option<Vec<ActionCardButton>>,
}

impl ActionCardContent {
    fn validate(&self) -> Result<()> {
        validate_non_empty(&self.title, "action_card.title")?;
        validate_non_empty(&self.text, "action_card.text")?;
        if let Some(orientation) = &self.btn_orientation
            && !matches!(orientation.as_str(), "0" | "1")
        {
            return Err(Error::invalid_input(
                "action_card.btn_orientation",
                "value must be 0 or 1",
            ));
        }

        match (&self.single_title, &self.single_url, &self.btns) {
            (Some(title), Some(url), None) => {
                validate_non_empty(title, "action_card.single_title")?;
                validate_http_url(url, "action_card.single_url")
            }
            (None, None, Some(buttons)) if !buttons.is_empty() => {
                for button in buttons {
                    button.validate()?;
                }
                Ok(())
            }
            _ => Err(Error::invalid_input(
                "action_card",
                "provide either single button fields or at least one multi-button item",
            )),
        }
    }
}

/// Feed card body.
#[derive(Debug, Clone, Serialize)]
pub struct FeedCardContent {
    /// Feed links.
    pub links: Vec<FeedCardLink>,
}

/// Action card button.
#[derive(Debug, Clone, Serialize)]
pub struct ActionCardButton {
    /// Button title.
    pub title: String,
    /// Button URL.
    #[serde(rename = "actionURL")]
    pub action_url: String,
}

impl ActionCardButton {
    /// Creates an action-card button.
    #[must_use]
    pub fn new(title: impl Into<String>, action_url: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            action_url: trimmed_string(action_url),
        }
    }

    fn validate(&self) -> Result<()> {
        validate_non_empty(&self.title, "action_card.button.title")?;
        validate_http_url(&self.action_url, "action_card.button.action_url")
    }
}

/// Feed card link item.
#[derive(Debug, Clone, Serialize)]
pub struct FeedCardLink {
    /// Link title.
    pub title: String,
    /// Link target.
    #[serde(rename = "messageURL")]
    pub message_url: String,
    /// Image URL.
    #[serde(rename = "picURL")]
    pub pic_url: String,
}

impl FeedCardLink {
    /// Creates a feed-card link.
    #[must_use]
    pub fn new(
        title: impl Into<String>,
        message_url: impl Into<String>,
        pic_url: impl Into<String>,
    ) -> Self {
        Self {
            title: title.into(),
            message_url: trimmed_string(message_url),
            pic_url: trimmed_string(pic_url),
        }
    }

    fn validate(&self) -> Result<()> {
        validate_non_empty(&self.title, "feed_card.link.title")?;
        validate_http_url(&self.message_url, "feed_card.link.message_url")?;
        validate_http_url(&self.pic_url, "feed_card.link.pic_url")
    }
}

/// Mention metadata.
#[derive(Debug, Clone, Default, Serialize)]
pub struct At {
    /// Mentioned mobile numbers.
    #[serde(rename = "atMobiles", skip_serializing_if = "Vec::is_empty")]
    pub mobiles: Vec<String>,
    /// Mentioned DingTalk user ids.
    #[serde(rename = "atUserIds", skip_serializing_if = "Vec::is_empty")]
    pub user_ids: Vec<String>,
    /// Mentions everyone.
    #[serde(rename = "isAtAll")]
    pub is_at_all: bool,
}

impl At {
    /// Creates empty mention metadata.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates mention metadata from users mentioned in an incoming bot message.
    ///
    /// DingTalk webhook replies use staff user ids in `atUserIds`; incoming
    /// `dingtalk_id` values are intentionally ignored because they are not valid
    /// webhook mention ids.
    #[cfg(feature = "bot")]
    #[must_use]
    pub fn from_mentioned_users<'a, I>(users: I) -> Self
    where
        I: IntoIterator<Item = &'a crate::bot::AtUser>,
    {
        Self::new().mentioned_users(users)
    }

    /// Adds a mobile number.
    #[must_use]
    pub fn mobile(mut self, value: impl Into<String>) -> Self {
        push_unique_trimmed(&mut self.mobiles, value);
        self
    }

    /// Adds multiple mobile numbers.
    #[must_use]
    pub fn mobiles<I, S>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        for value in values {
            push_unique_trimmed(&mut self.mobiles, value);
        }
        self
    }

    /// Adds a DingTalk user id.
    #[must_use]
    pub fn user_id(mut self, value: impl Into<String>) -> Self {
        push_unique_trimmed(&mut self.user_ids, value);
        self
    }

    /// Adds multiple DingTalk user ids.
    #[must_use]
    pub fn user_ids<I, S>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        for value in values {
            push_unique_trimmed(&mut self.user_ids, value);
        }
        self
    }

    /// Adds a user mentioned in an incoming bot message.
    ///
    /// Only `staff_id` is used because DingTalk webhook `atUserIds` expects
    /// staff user ids.
    #[cfg(feature = "bot")]
    #[must_use]
    pub fn mentioned_user(mut self, user: &crate::bot::AtUser) -> Self {
        if let Some(staff_id) = user.staff_id.as_deref() {
            push_unique_non_empty(&mut self.user_ids, staff_id);
        }
        self
    }

    /// Adds users mentioned in an incoming bot message.
    #[cfg(feature = "bot")]
    #[must_use]
    pub fn mentioned_users<'a, I>(mut self, users: I) -> Self
    where
        I: IntoIterator<Item = &'a crate::bot::AtUser>,
    {
        for user in users {
            self = self.mentioned_user(user);
        }
        self
    }

    /// Mentions everyone in the conversation.
    #[must_use]
    pub fn all_users(mut self) -> Self {
        self.is_at_all = true;
        self
    }

    /// Returns whether no users or everyone are mentioned.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.mobiles.is_empty() && self.user_ids.is_empty() && !self.is_at_all
    }

    /// Validates mention metadata before sending.
    pub fn validate(&self) -> Result<()> {
        for mobile in &self.mobiles {
            validate_non_empty(mobile, "at.mobiles")?;
        }
        for user_id in &self.user_ids {
            validate_non_empty(user_id, "at.user_ids")?;
        }
        Ok(())
    }
}

fn push_unique_trimmed(values: &mut Vec<String>, value: impl Into<String>) {
    let value = value.into();
    let value = value.trim();
    if !values.iter().any(|existing| existing == value) {
        values.push(value.to_string());
    }
}

#[cfg(feature = "bot")]
fn push_unique_non_empty(values: &mut Vec<String>, value: &str) {
    let value = value.trim();
    if !value.is_empty() && !values.iter().any(|existing| existing == value) {
        values.push(value.to_string());
    }
}

/// Standard webhook response.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WebhookResponse {
    /// DingTalk error code.
    pub errcode: i64,
    /// DingTalk message.
    pub errmsg: String,
    /// Optional request id.
    pub request_id: Option<String>,
}

impl WebhookResponse {
    /// Returns whether DingTalk accepted the webhook message.
    #[must_use]
    pub fn is_success(&self) -> bool {
        self.errcode == 0
    }

    /// Returns whether DingTalk reported a non-zero business code.
    #[must_use]
    pub fn is_error(&self) -> bool {
        !self.is_success()
    }

    /// Returns the DingTalk business code.
    #[must_use]
    pub fn code(&self) -> i64 {
        self.errcode
    }

    /// Returns the DingTalk response message.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.errmsg
    }

    /// Returns DingTalk request id when present.
    #[must_use]
    pub fn request_id(&self) -> Option<&str> {
        self.request_id.as_deref()
    }
}

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

    use super::*;

    #[test]
    fn serializes_markdown_with_mentions() {
        let message = WebhookMessage::markdown("title", "**body**").at(At::new()
            .mobile("13800000000")
            .mobiles(["13900000000"])
            .user_ids(["user-1"]));
        let value = serde_json::to_value(message).expect("serialize");

        assert_eq!(
            value,
            json!({
                "msgtype": "markdown",
                "markdown": {
                    "title": "title",
                    "text": "**body**"
                },
                "at": {
                    "atMobiles": ["13800000000", "13900000000"],
                    "atUserIds": ["user-1"],
                    "isAtAll": false
                }
            })
        );
    }

    #[test]
    fn serializes_action_card_orientation() {
        let message = WebhookMessage::action_card("title", "body", "open", "https://example.com")
            .button_orientation(ButtonOrientation::Horizontal);
        let value = serde_json::to_value(message).expect("serialize");

        assert_eq!(
            ButtonOrientation::from_dingtalk_value(" horizontal "),
            Some(ButtonOrientation::Horizontal)
        );
        assert_eq!(
            "0".parse::<ButtonOrientation>().expect("orientation"),
            ButtonOrientation::Vertical
        );
        assert_eq!(ButtonOrientation::Horizontal.as_dingtalk_value(), "1");
        assert_eq!(ButtonOrientation::Horizontal.as_str(), "horizontal");
        assert_eq!(ButtonOrientation::Horizontal.to_string(), "horizontal");
        assert!(ButtonOrientation::Vertical.is_vertical());
        assert!(ButtonOrientation::Horizontal.is_horizontal());
        assert_eq!(
            value,
            json!({
                "msgtype": "actionCard",
                "actionCard": {
                    "title": "title",
                    "text": "body",
                    "btnOrientation": "1",
                    "singleTitle": "open",
                    "singleURL": "https://example.com"
                }
            })
        );
    }

    #[test]
    fn normalizes_webhook_message_urls() {
        let link = WebhookMessage::link("title", "body", " https://example.com/page ")
            .image_url(" https://example.com/pic.png ");
        let action_card =
            WebhookMessage::action_card("title", "body", "open", " https://example.com/action ");
        let feed_card = WebhookMessage::feed_card(vec![FeedCardLink::new(
            "title",
            " https://example.com/feed ",
            " https://example.com/feed.png ",
        )]);
        let button = ActionCardButton::new("open", " https://example.com/button ");

        assert_eq!(
            serde_json::to_value(link).expect("serialize"),
            json!({
                "msgtype": "link",
                "link": {
                    "title": "title",
                    "text": "body",
                    "messageUrl": "https://example.com/page",
                    "picUrl": "https://example.com/pic.png"
                }
            })
        );
        assert_eq!(
            serde_json::to_value(action_card).expect("serialize")["actionCard"]["singleURL"],
            "https://example.com/action"
        );
        assert_eq!(
            serde_json::to_value(feed_card).expect("serialize")["feedCard"]["links"][0]["messageURL"],
            "https://example.com/feed"
        );
        assert_eq!(button.action_url, "https://example.com/button");
    }

    #[test]
    fn webhook_response_exposes_helpers() {
        let ok = WebhookResponse {
            errcode: 0,
            errmsg: "ok".to_string(),
            request_id: Some("request-1".to_string()),
        };
        let error = WebhookResponse {
            errcode: 310000,
            errmsg: "invalid token".to_string(),
            request_id: None,
        };

        assert!(ok.is_success());
        assert!(!ok.is_error());
        assert_eq!(ok.code(), 0);
        assert_eq!(ok.message(), "ok");
        assert_eq!(ok.request_id(), Some("request-1"));
        assert!(!error.is_success());
        assert!(error.is_error());
        assert_eq!(error.code(), 310000);
        assert_eq!(error.message(), "invalid token");
        assert_eq!(error.request_id(), None);
    }

    #[test]
    fn normalizes_mention_values() {
        let at = At::new()
            .mobile(" 13800000000 ")
            .mobiles(["13900000000", " 13900000000 "])
            .user_id(" user-1 ")
            .user_ids(["user-1", " user-2 "]);

        assert_eq!(at.mobiles, ["13800000000", "13900000000"]);
        assert_eq!(at.user_ids, ["user-1", "user-2"]);
    }

    #[test]
    fn rejects_empty_text_message() {
        let error = WebhookMessage::text("  ")
            .validate()
            .expect_err("empty text should fail");

        assert_eq!(error.kind(), crate::ErrorKind::InvalidInput);
    }

    #[test]
    fn rejects_empty_mention_values() {
        let mobile_error = WebhookMessage::text("hello")
            .at(At::new().mobile(" "))
            .validate()
            .expect_err("empty mobile should fail");
        let user_id_error = WebhookMessage::markdown("title", "body")
            .at(At::new().user_id(""))
            .validate()
            .expect_err("empty user id should fail");

        assert_eq!(mobile_error.kind(), crate::ErrorKind::InvalidInput);
        assert_eq!(user_id_error.kind(), crate::ErrorKind::InvalidInput);
    }

    #[cfg(feature = "bot")]
    #[test]
    fn creates_mentions_from_incoming_at_users() {
        let users = [
            crate::bot::AtUser {
                dingtalk_id: Some("$:LWCP_v1:$open-id".to_string()),
                staff_id: Some(" staff-1 ".to_string()),
            },
            crate::bot::AtUser {
                dingtalk_id: None,
                staff_id: Some("staff-1".to_string()),
            },
            crate::bot::AtUser {
                dingtalk_id: Some("$:LWCP_v1:$only-open-id".to_string()),
                staff_id: None,
            },
            crate::bot::AtUser {
                dingtalk_id: None,
                staff_id: Some(" ".to_string()),
            },
        ];

        let at = At::new().user_id("existing").mentioned_users(&users);
        let from_users = At::from_mentioned_users(&users);

        assert_eq!(at.user_ids, ["existing", "staff-1"]);
        assert_eq!(from_users.user_ids, ["staff-1"]);
        assert!(from_users.mobiles.is_empty());
        assert!(!from_users.is_at_all);
    }

    #[test]
    fn empty_mentions_are_omitted() {
        let message = WebhookMessage::text("hello").at(At::new());
        let value = serde_json::to_value(message).expect("serialize");

        assert!(value.get("at").is_none());
    }

    #[test]
    fn rejects_invalid_action_card_orientation() {
        let mut message =
            WebhookMessage::action_card("title", "body", "open", "https://example.com");
        let WebhookMessage::ActionCard { action_card } = &mut message else {
            panic!("expected action card");
        };
        action_card.btn_orientation = Some("sideways".to_string());

        let error = message
            .validate()
            .expect_err("invalid orientation should fail");

        assert_eq!(error.kind(), crate::ErrorKind::InvalidInput);
    }

    #[test]
    fn rejects_feed_card_without_links() {
        let error = WebhookMessage::feed_card(Vec::new())
            .validate()
            .expect_err("empty feed card should fail");

        assert_eq!(error.kind(), crate::ErrorKind::InvalidInput);
    }

    #[test]
    fn rejects_non_http_link_url() {
        let error = WebhookMessage::link("title", "text", "ftp://example.com")
            .validate()
            .expect_err("non-http link should fail");

        assert_eq!(error.kind(), crate::ErrorKind::InvalidInput);
    }
}