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
use serde_json::Value;

use std::{
    fs,
    env,
    path::PathBuf,
    time::SystemTime,
    io::{
        Error,
        ErrorKind,
    },
};
use sha2::Sha256;
use hmac::{
    Hmac,
    Mac,
};

mod msg;
use msg::*;

pub use msg:: {
    DingTalkMessage,
    DingTalkMessageType,
    DingTalkMessageActionCardHideAvatar,
    DingTalkMessageActionCardBtnOrientation,
    DingTalkMessageActionCardBtn,
    DingTalkMessageFeedCardLink,
};

pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;

const CONTENT_TYPE: &str = "Content-Type";
const APPLICATION_JSON_UTF8: &str = "application/json; charset=utf-8";

const DEFAULT_DINGTALK_ROBOT_URL: &str = "https://oapi.dingtalk.com/robot/send";

/// `DingTalk` is a simple SDK for DingTalk webhook robot
/// 
/// Document https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
/// 
/// Sample code:
/// ```
/// let dt = DingTalk::new("<token>", "");
/// dt.send_text("Hello world!")?;
/// ```
/// 
/// At all sample:
/// ```
/// dt.send_message(&DingTalkMessage::new_text("Hello World!").at_all())?;
/// ```
#[derive(Default)]
pub struct DingTalk<'a> {
    pub default_webhook_url: &'a str,
    pub access_token: &'a str,
    pub sec_token: &'a str,
    pub direct_url: &'a str,
}

impl <'a> DingTalkMessage<'a> {

    /// New text DingTalk message
    pub fn new_text(text_content: &'a str) -> Self {
        Self::new(DingTalkMessageType::Text).text(text_content)
    }

    /// New markdown DingTalk message
    pub fn new_markdown(markdown_title: &'a str, markdown_content: &'a str) -> Self {
        Self::new(DingTalkMessageType::Markdown).markdown(markdown_title, markdown_content)
    }

    /// New link DingTalk message
    pub fn new_link(link_title: &'a str, link_text: &'a str, link_pic_url: &'a str, link_message_url: &'a str) -> Self {
        Self::new(DingTalkMessageType::Link).link(link_title, link_text, link_pic_url, link_message_url)
    }

    /// New action card DingTalk message
    pub fn new_action_card(title: &'a str, text: &'a str) -> Self {
        let mut s = Self::new(DingTalkMessageType::ActionCard);
        s.action_card_title = title;
        s.action_card_text = text;
        s
    }

    /// New feed card DingTalk message
    pub fn new_feed_card() -> Self {
        Self::new(DingTalkMessageType::FeedCard)
    }
    
    /// New DingTalk message
    pub fn new(message_type: DingTalkMessageType) -> Self {
        DingTalkMessage {
            message_type,
            ..Default::default()
        }
    }

    /// Set text
    pub fn text(mut self, text_content: &'a str) -> Self {
        self.text_content = text_content;
        self
    }

    /// Set markdown
    pub fn markdown(mut self, markdown_title: &'a str, markdown_content: &'a str) -> Self {
        self.markdown_title = markdown_title;
        self.markdown_content = markdown_content;
        self
    }

    /// Set link
    pub fn link(mut self, link_title: &'a str, link_text: &'a str, link_pic_url: &'a str, link_message_url: &'a str) -> Self {
        self.link_title = link_title;
        self.link_text = link_text;
        self.link_pic_url = link_pic_url;
        self.link_message_url = link_message_url;
        self
    }

    /// Set action card show avator(default show)
    pub fn action_card_show_avatar(mut self) -> Self {
        self.action_card_hide_avatar = DingTalkMessageActionCardHideAvatar::Show;
        self
    }

    /// Set action card hide avator
    pub fn action_card_hide_avatar(mut self) -> Self {
        self.action_card_hide_avatar = DingTalkMessageActionCardHideAvatar::Hide;
        self
    }

    /// Set action card btn vertical(default vertical)
    pub fn action_card_btn_vertical(mut self) -> Self {
        self.action_card_btn_orientation = DingTalkMessageActionCardBtnOrientation::Vertical;
        self
    }

    /// Set action card btn landscape
    pub fn action_card_btn_landscape(mut self) -> Self {
        self.action_card_btn_orientation = DingTalkMessageActionCardBtnOrientation::Landscape;
        self
    }

    /// Set action card single btn
    pub fn set_action_card_signle_btn(mut self, btn: DingTalkMessageActionCardBtn) -> Self {
        self.action_card_single_btn = Some(btn);
        self
    }

    /// Add action card btn
    pub fn add_action_card_btn(mut self, btn: DingTalkMessageActionCardBtn) -> Self {
        self.action_card_btns.push(btn);
        self
    }
    
    /// Add feed card link
    pub fn add_feed_card_link(mut self, link: DingTalkMessageFeedCardLink) -> Self {
        self.feed_card_links.push(link);
        self
    }

    /// Add feed card link detail
    pub fn add_feed_card_link_detail(self, title: &'a str, message_url: &'a str, pic_url: &'a str) -> Self {
        self.add_feed_card_link(DingTalkMessageFeedCardLink {
            title: title.into(),
            message_url: message_url.into(),
            pic_url: pic_url.into(),
        })
    }

    /// At all
    pub fn at_all(mut self) -> Self {
        self.at_all = true;
        self
    }

    /// At mobiles
    pub fn at_mobiles(mut self, mobiles: &[String]) -> Self {
        for m in mobiles {
            self.at_mobiles.push(m.clone());
        }
        self
    }
}

impl <'a> DingTalk<'a> {

    /// Create `DingTalk` from file
    /// 
    /// Format see `DingTalk::from_json(json: &str)`
    pub fn from_file(f: &str) -> XResult<Self> {
        let f_path_buf = if f.starts_with("~/") {
            let home = PathBuf::from(env::var("HOME")?);
            home.join(f.chars().skip(2).collect::<String>())
        } else {
            PathBuf::from(f)
        };
        let f_content = fs::read_to_string(f_path_buf)?;
        Self::from_json(&f_content)
    }

    /// Create `DingTalk` from JSON string
    /// 
    /// Format:
    /// ```json
    /// {
    ///     "default_webhook_url": "", // option
    ///     "access_token": "<access token>",
    ///     "sec_token": "<sec token>" // option
    /// }
    /// ```
    pub fn from_json(json: &str) -> XResult<Self> {
        let json_value: Value = serde_json::from_str(json)?;
        if !json_value.is_object() {
            return Err(Box::new(Error::new(ErrorKind::Other, format!("JSON format erorr: {}", json))));
        }

        let default_webhook_url = Self::string_to_a_str(json_value["default_webhook_url"].as_str().unwrap_or(DEFAULT_DINGTALK_ROBOT_URL));
        let access_token = Self::string_to_a_str(json_value["access_token"].as_str().unwrap_or_default());
        let sec_token = Self::string_to_a_str(json_value["sec_token"].as_str().unwrap_or_default());
        let direct_url = Self::string_to_a_str(json_value["direct_url"].as_str().unwrap_or_default());
        
        Ok(DingTalk {
            default_webhook_url,
            access_token,
            sec_token,
            direct_url,
        })
    }

    /// Create `DingTalk` from url, for outgoing robot
    pub fn from_url(direct_url: &'a str) -> Self {
        DingTalk {
            direct_url,
            ..Default::default()
        }
    }

    /// Create `DingTalk`
    /// `access_token` is access token, `sec_token` can be empty `""`
    pub fn new(access_token: &'a str, sec_token: &'a str) -> Self {
        DingTalk {
            default_webhook_url: DEFAULT_DINGTALK_ROBOT_URL,
            access_token,
            sec_token,
            ..Default::default()
        }
    }

    /// Set default webhook url
    pub fn set_default_webhook_url(&mut self, default_webhook_url: &'a str) {
        self.default_webhook_url = default_webhook_url;
    }

    /// Send DingTalk message
    /// 
    /// 1. Create DingTalk JSON message
    /// 2. POST JSON message to DingTalk server
    pub async fn send_message(&self, dingtalk_message: &DingTalkMessage<'_>) -> XResult<()> {
        let mut message_json = match dingtalk_message.message_type {
            DingTalkMessageType::Text => serde_json::to_value(InnerTextMessage {
                msgtype: DingTalkMessageType::Text,
                text: InnerTextMessageText {
                    content: dingtalk_message.text_content.into(),
                }
            }),
            DingTalkMessageType::Link => serde_json::to_value(InnerLinkMessage {
                msgtype: DingTalkMessageType::Link,
                link: InnerLinkMessageLink {
                    title: dingtalk_message.link_title.into(),
                    text: dingtalk_message.link_text.into(),
                    pic_url: dingtalk_message.link_pic_url.into(),
                    message_url: dingtalk_message.link_message_url.into(),
                }
            }),
            DingTalkMessageType::Markdown => serde_json::to_value(InnerMarkdownMessage {
                msgtype: DingTalkMessageType::Markdown,
                markdown: InnerMarkdownMessageMarkdown {
                    title: dingtalk_message.markdown_title.into(),
                    text: dingtalk_message.markdown_content.into(),
                }
            }),
            DingTalkMessageType::ActionCard => serde_json::to_value(InnerActionCardMessage {
                msgtype: DingTalkMessageType::ActionCard,
                action_card: InnerActionCardMessageActionCard {
                    title: dingtalk_message.action_card_title.into(),
                    text: dingtalk_message.action_card_text.into(),
                    hide_avatar: dingtalk_message.action_card_hide_avatar,
                    btn_orientation: dingtalk_message.action_card_btn_orientation,
                }
            }),
            DingTalkMessageType::FeedCard => serde_json::to_value(InnerFeedCardMessage {
                msgtype: DingTalkMessageType::FeedCard,
                feed_card: InnerFeedCardMessageFeedCard {
                    links: {
                        let mut links: Vec<InnerFeedCardMessageFeedCardLink> = vec![];
                        for feed_card_link in &dingtalk_message.feed_card_links {
                            links.push(InnerFeedCardMessageFeedCardLink {
                                title: feed_card_link.title.clone(),
                                message_url: feed_card_link.message_url.clone(),
                                pic_url: feed_card_link.pic_url.clone(),
                            });
                        }
                        links
                    }
                }
            })
        }?;
        if DingTalkMessageType::ActionCard == dingtalk_message.message_type {
            if dingtalk_message.action_card_single_btn.is_some() {
                if let Some(single_btn) = dingtalk_message.action_card_single_btn.as_ref() {
                    message_json["actionCard"]["singleTitle"] = single_btn.title.as_str().into();
                    message_json["actionCard"]["singleURL"] = single_btn.action_url.as_str().into();
                };
            } else {
                let mut btns: Vec<InnerActionCardMessageBtn> = vec![];
                for action_card_btn in &dingtalk_message.action_card_btns {
                    btns.push(InnerActionCardMessageBtn {
                        title: action_card_btn.title.clone(),
                        action_url: action_card_btn.action_url.clone(),
                    });
                }
                message_json["actionCard"]["btns"] = serde_json::to_value(btns)?;
            }
        }
        if dingtalk_message.at_all || !dingtalk_message.at_mobiles.is_empty() {
            if let Some(m) = message_json.as_object_mut() {
                let mut at_mobiles: Vec<Value> = vec![];
                for m in &dingtalk_message.at_mobiles {
                    at_mobiles.push(Value::String(m.clone()));
                }
                let mut at_map = serde_json::Map::new();
                at_map.insert("atMobiles".into(), Value::Array(at_mobiles));
                at_map.insert("isAtAll".into(), Value::Bool(dingtalk_message.at_all));

                m.insert("at".into(), Value::Object(at_map));
            }
        }
        self.send(&serde_json::to_string(&message_json)?).await
    }

    /// Send text message
    pub async fn send_text(&self, text_message: &str) -> XResult<()> {
        self.send_message(&DingTalkMessage::new_text(text_message)).await
    }

    /// Send markdown message
    pub async fn send_markdown(&self, title: &str, text: &str) -> XResult<()> {
        self.send_message(&DingTalkMessage::new_markdown(title, text)).await
    }

    /// Send link message
    pub async fn send_link(&self, link_title: &'a str, link_text: &'a str, link_pic_url: &'a str, link_message_url: &'a str) -> XResult<()> {
        self.send_message(&DingTalkMessage::new_link(link_title, link_text, link_pic_url, link_message_url)).await
    }

    /// Direct send JSON message
    pub async fn send(&self, json_message: &str) -> XResult<()> {
        let client = reqwest::Client::new();
        let response = match client.post(&self.generate_signed_url()?)
              .header(CONTENT_TYPE, APPLICATION_JSON_UTF8)
              .body(json_message.as_bytes().to_vec())
              .send().await {
                  Ok(r) => r,
                  Err(e) => return Err(Box::new(Error::new(ErrorKind::Other, format!("Unknown error: {}", e))) as Box<dyn std::error::Error>),
              };

        match response.status().as_u16() {
            200_u16 => Ok(()),
            _ => Err(Box::new(Error::new(ErrorKind::Other, format!("Unknown status: {}", response.status().as_u16()))) as Box<dyn std::error::Error>),
        }
    }

    /// Generate signed dingtalk webhook URL
    pub fn generate_signed_url(&self) -> XResult<String> {
        if !self.direct_url.is_empty() {
            return Ok(self.direct_url.into());
        }
        let mut signed_url = String::with_capacity(1024);
        signed_url.push_str(self.default_webhook_url);

        if self.default_webhook_url.ends_with('?') {
            // Just Ok
        } else if self.default_webhook_url.contains('?') {
            if !self.default_webhook_url.ends_with('&') {
                signed_url.push('&');
            }
        } else {
            signed_url.push('?');
        }

        signed_url.push_str("access_token=");
        signed_url.push_str(&urlencoding::encode(self.access_token));

        if !self.sec_token.is_empty() {
            let timestamp = &format!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis());
            let timestamp_and_secret = &format!("{}\n{}", timestamp, self.sec_token);
            let hmac_sha256 = base64::encode(&calc_hmac_sha256(self.sec_token.as_bytes(), timestamp_and_secret.as_bytes())?[..]);

            signed_url.push_str("&timestamp=");
            signed_url.push_str(timestamp);
            signed_url.push_str("&sign=");
            signed_url.push_str(&urlencoding::encode(&hmac_sha256));
        }

        Ok(signed_url)
    }

    // SAFE? may these codes cause memory leak?
    fn string_to_a_str(s: &str) -> &'a str {
        Box::leak(s.to_owned().into_boxed_str())
    }
}

/// calc hma_sha256 digest
fn calc_hmac_sha256(key: &[u8], message: &[u8]) -> XResult<Vec<u8>> {
    let mut mac = match Hmac::<Sha256>::new_varkey(key) {
        Ok(m) => m,
        Err(e) => return Err(Box::new(Error::new(ErrorKind::Other, format!("Hmac error: {}", e))))
    };
    mac.input(message);
    Ok(mac.result().code().to_vec())
}