diff --git a/Cargo.toml b/Cargo.toml index 5572cfa..0f4736a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dingtalk" -version = "0.0.2" +version = "0.0.3" authors = ["Hatter Jiang "] edition = "2018" description = "DingTalk Util" diff --git a/README.md b/README.md index be11838..ea70e6b 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,9 @@ pub fn main() { } ``` + +#### Changelog + +* v0.0.3 + * Add `DingTalkMessage` , can set at_all, at_mobiles now + diff --git a/src/lib.rs b/src/lib.rs index 924107e..a941376 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,11 +28,88 @@ const DEFAULT_DINGTALK_ROBOT_URL: &str = "https://oapi.dingtalk.com/robot/send?a /// let dt = DingTalk::new("", ""); /// dt.send_text("Hello world!")?; /// ``` +/// +/// At all sample: +/// ``` +/// dt.send_message(&DingTalkMessage::new_text("Hello World!").at_all())?; +/// ``` pub struct DingTalk<'a> { pub access_token: &'a str, pub sec_token: &'a str, } +/// DingTalk message type +/// * TEXT - text message +/// * MARKDONW - markdown message +#[derive(Clone, Copy, Debug)] +pub enum DingTalkMessageType { + TEXT, + // LINK, + MARKDOWN, +} + +/// DingTalk message +pub struct DingTalkMessage<'a> { + pub message_type: DingTalkMessageType, + pub text_content: &'a str, + pub markdown_title: &'a str, + pub markdown_content: &'a str, + pub at_all: bool, + pub at_mobiles: Vec, +} + +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 DingTalk message + pub fn new(message_type: DingTalkMessageType) -> Self { + DingTalkMessage { + message_type: message_type, + text_content: "", + markdown_title: "", + markdown_content: "", + at_all: false, + at_mobiles: vec![], + } + } + + /// 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 + } + + // At all + pub fn at_all(mut self) -> Self { + self.at_all = true; + self + } + + // At mobiles + pub fn at_mobiles(mut self, mobiles: &Vec) -> Self { + for m in mobiles { + self.at_mobiles.push(m.clone()); + } + self + } +} + impl <'a> DingTalk<'a> { /// Create `DingTalk` @@ -44,25 +121,43 @@ impl <'a> DingTalk<'a> { } } + pub fn send_message(&self, dingtalk_message: &DingTalkMessage) -> Result<(), Box> { + let mut message_json = match dingtalk_message.message_type { + DingTalkMessageType::TEXT => object!{ + "msgtype" => "text", + "text" => object! { + "content" => dingtalk_message.text_content, + } + }, + DingTalkMessageType::MARKDOWN => object!{ + "msgtype" => "markdown", + "markdown" => object! { + "title" => dingtalk_message.markdown_title, + "text" => dingtalk_message.markdown_content, + } + }, + }; + if dingtalk_message.at_all || dingtalk_message.at_mobiles.len() > 0 { + let mut at_mobiles = json::JsonValue::new_object(); + for m in &dingtalk_message.at_mobiles { + at_mobiles.push(m.clone()).ok(); + } + message_json["at"] = object!{ + "atMobiles" => at_mobiles, + "isAtAll" => dingtalk_message.at_all, + }; + } + self.send(&json::stringify(message_json)) + } + /// Send text message pub fn send_text(&self, text_message: &str) -> Result<(), Box> { - self.send(&json::stringify(object!{ - "msgtype" => "text", - "text" => object! { - "content" => text_message, - } - })) + self.send_message(&DingTalkMessage::new_text(text_message)) } /// Send markdown message pub fn send_markdown(&self, title: &str, text: &str) -> Result<(), Box> { - self.send(&json::stringify(object!{ - "msgtype" => "markdown", - "markdown" => object! { - "title" => title, - "text" => text, - } - })) + self.send_message(&DingTalkMessage::new_markdown(title, text)) } /// Direct send JSON message