diff --git a/Cargo.toml b/Cargo.toml index b31c17f..754c16b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dingtalk" -version = "1.3.1" +version = "1.3.2" authors = ["Hatter Jiang "] edition = "2018" description = "DingTalk Robot Util, Send text/markdown/link messages using DingTalk robot, 钉钉机器人" diff --git a/README.md b/README.md index cfe2a27..0cf2199 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,8 @@ WeChat Work config: #### Changelog +* v1.3.2 + * Add `DingTalk::from_token` * v1.3.1 * Add `DingTalk::new_wechat` * v1.3.0 diff --git a/src/lib.rs b/src/lib.rs index c1b699e..3eb8959 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,6 +183,27 @@ impl <'a> DingTalkMessage<'a> { impl <'a> DingTalk<'a> { + /// Create `DingTalk` from token: + /// wechatwork:access_token + /// dingtalk:access_token?sec_token + pub fn from_token(token: &str) -> XResult { + if token.starts_with("dingtalk:") { + let token_and_or_sec = &token["dingtalk:".len()..]; + let mut token_and_or_sec_vec = token_and_or_sec.split('?'); + let access_token = match token_and_or_sec_vec.next() { + Some(t) => t, None => token_and_or_sec, + }; + let sec_token = match token_and_or_sec_vec.next() { + Some(t) => t, None => "", + }; + Ok(Self::new(Self::string_to_a_str(access_token), Self::string_to_a_str(sec_token))) + } else if token.starts_with("wechatwork:") { + Ok(Self::new_wechat(Self::string_to_a_str(&token["wechatwork:".len()..]))) + } else { + Err(Box::new(Error::new(ErrorKind::Other, format!("Tokne format erorr: {}", token)))) + } + } + /// Create `DingTalk` from file /// /// Format see `DingTalk::from_json(json: &str)`