v1.0
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dingtalk"
|
name = "dingtalk"
|
||||||
version = "0.4.0"
|
version = "1.0.0"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "DingTalk Robot Util, Send text/markdown/link messages using DingTalk robot"
|
description = "DingTalk Robot Util, Send text/markdown/link messages using DingTalk robot"
|
||||||
|
|||||||
@@ -28,9 +28,11 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
#### Changelog
|
#### Changelog
|
||||||
|
|
||||||
* v0.4.0
|
* v1.0.0
|
||||||
* `TEXT` -> `Text` ..., change enum caps
|
* `TEXT` -> `Text` ..., change enum caps
|
||||||
* Add `ActionCard` message, send action card message type
|
* Add `ActionCard` message, send action card message type
|
||||||
|
* Add `direct_url` for `DingTalk`, for outgoing robot
|
||||||
|
* Implemented almost the functions listed on https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq/0fa88adc
|
||||||
* v0.3.0
|
* v0.3.0
|
||||||
* Add `FeedCard` message, send feed card message type
|
* Add `FeedCard` message, send feed card message type
|
||||||
* v0.2.1
|
* v0.2.1
|
||||||
|
|||||||
29
src/lib.rs
29
src/lib.rs
@@ -39,10 +39,12 @@ const DEFAULT_DINGTALK_ROBOT_URL: &str = "https://oapi.dingtalk.com/robot/send?a
|
|||||||
/// ```
|
/// ```
|
||||||
/// dt.send_message(&DingTalkMessage::new_text("Hello World!").at_all())?;
|
/// dt.send_message(&DingTalkMessage::new_text("Hello World!").at_all())?;
|
||||||
/// ```
|
/// ```
|
||||||
|
#[derive(Default)]
|
||||||
pub struct DingTalk<'a> {
|
pub struct DingTalk<'a> {
|
||||||
pub default_webhook_url: &'a str,
|
pub default_webhook_url: &'a str,
|
||||||
pub access_token: &'a str,
|
pub access_token: &'a str,
|
||||||
pub sec_token: &'a str,
|
pub sec_token: &'a str,
|
||||||
|
pub direct_url: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// DingTalk message type
|
/// DingTalk message type
|
||||||
@@ -64,16 +66,19 @@ impl Default for DingTalkMessageType {
|
|||||||
fn default() -> Self { DingTalkMessageType::Text }
|
fn default() -> Self { DingTalkMessageType::Text }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// DingTalk messge action card avatar
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum DingTalkMessageActionCardHideAvatar {
|
pub enum DingTalkMessageActionCardHideAvatar {
|
||||||
Hide,
|
Hide,
|
||||||
Show,
|
Show,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// default value
|
||||||
impl Default for DingTalkMessageActionCardHideAvatar {
|
impl Default for DingTalkMessageActionCardHideAvatar {
|
||||||
fn default() -> Self { DingTalkMessageActionCardHideAvatar::Show }
|
fn default() -> Self { DingTalkMessageActionCardHideAvatar::Show }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// into JsonValue
|
||||||
impl From<DingTalkMessageActionCardHideAvatar> for json::JsonValue {
|
impl From<DingTalkMessageActionCardHideAvatar> for json::JsonValue {
|
||||||
fn from(a: DingTalkMessageActionCardHideAvatar) -> Self {
|
fn from(a: DingTalkMessageActionCardHideAvatar) -> Self {
|
||||||
json::JsonValue::String(match a {
|
json::JsonValue::String(match a {
|
||||||
@@ -83,16 +88,19 @@ impl From<DingTalkMessageActionCardHideAvatar> for json::JsonValue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// DingTalk message action card orientation
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum DingTalkMessageActionCardBtnOrientation {
|
pub enum DingTalkMessageActionCardBtnOrientation {
|
||||||
Vertical,
|
Vertical,
|
||||||
Landscape,
|
Landscape,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// default value
|
||||||
impl Default for DingTalkMessageActionCardBtnOrientation {
|
impl Default for DingTalkMessageActionCardBtnOrientation {
|
||||||
fn default() -> Self { DingTalkMessageActionCardBtnOrientation::Vertical }
|
fn default() -> Self { DingTalkMessageActionCardBtnOrientation::Vertical }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// into JsonValue
|
||||||
impl From<DingTalkMessageActionCardBtnOrientation> for json::JsonValue {
|
impl From<DingTalkMessageActionCardBtnOrientation> for json::JsonValue {
|
||||||
fn from(o: DingTalkMessageActionCardBtnOrientation) -> Self {
|
fn from(o: DingTalkMessageActionCardBtnOrientation) -> Self {
|
||||||
json::JsonValue::String(match o {
|
json::JsonValue::String(match o {
|
||||||
@@ -130,7 +138,7 @@ pub struct DingTalkMessage<'a> {
|
|||||||
pub link_message_url: &'a str,
|
pub link_message_url: &'a str,
|
||||||
pub action_card_title: &'a str,
|
pub action_card_title: &'a str,
|
||||||
pub action_card_text: &'a str,
|
pub action_card_text: &'a str,
|
||||||
pub action_card_hide_vatar: DingTalkMessageActionCardHideAvatar,
|
pub action_card_hide_avatar: DingTalkMessageActionCardHideAvatar,
|
||||||
pub action_card_btn_orientation: DingTalkMessageActionCardBtnOrientation,
|
pub action_card_btn_orientation: DingTalkMessageActionCardBtnOrientation,
|
||||||
pub action_card_single_btn: Option<DingTalkMessageActionCardBtn>,
|
pub action_card_single_btn: Option<DingTalkMessageActionCardBtn>,
|
||||||
pub action_card_btns: Vec<DingTalkMessageActionCardBtn>,
|
pub action_card_btns: Vec<DingTalkMessageActionCardBtn>,
|
||||||
@@ -201,13 +209,13 @@ impl <'a> DingTalkMessage<'a> {
|
|||||||
|
|
||||||
/// Set action card show avator(default show)
|
/// Set action card show avator(default show)
|
||||||
pub fn action_card_show_avatar(mut self) -> Self {
|
pub fn action_card_show_avatar(mut self) -> Self {
|
||||||
self.action_card_hide_vatar = DingTalkMessageActionCardHideAvatar::Show;
|
self.action_card_hide_avatar = DingTalkMessageActionCardHideAvatar::Show;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set action card hide avator
|
/// Set action card hide avator
|
||||||
pub fn action_card_hide_avatar(mut self) -> Self {
|
pub fn action_card_hide_avatar(mut self) -> Self {
|
||||||
self.action_card_hide_vatar = DingTalkMessageActionCardHideAvatar::Show;
|
self.action_card_hide_avatar = DingTalkMessageActionCardHideAvatar::Hide;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,9 +313,18 @@ impl <'a> DingTalk<'a> {
|
|||||||
default_webhook_url: default_webhook_url,
|
default_webhook_url: default_webhook_url,
|
||||||
access_token: access_token,
|
access_token: access_token,
|
||||||
sec_token: sec_token,
|
sec_token: sec_token,
|
||||||
|
..Default::default()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create `DingTalk` from url, for outgoing robot
|
||||||
|
pub fn from_url(url: &'a str) -> Self {
|
||||||
|
DingTalk {
|
||||||
|
direct_url: url,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create `DingTalk`
|
/// Create `DingTalk`
|
||||||
/// `access_token` is access token, `sec_token` can be empty `""`
|
/// `access_token` is access token, `sec_token` can be empty `""`
|
||||||
pub fn new(access_token: &'a str, sec_token: &'a str) -> Self {
|
pub fn new(access_token: &'a str, sec_token: &'a str) -> Self {
|
||||||
@@ -315,6 +332,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
default_webhook_url: DEFAULT_DINGTALK_ROBOT_URL,
|
default_webhook_url: DEFAULT_DINGTALK_ROBOT_URL,
|
||||||
access_token: access_token,
|
access_token: access_token,
|
||||||
sec_token: sec_token,
|
sec_token: sec_token,
|
||||||
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -356,7 +374,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
"actionCard" => object!{
|
"actionCard" => object!{
|
||||||
"title" => dingtalk_message.action_card_title,
|
"title" => dingtalk_message.action_card_title,
|
||||||
"text" => dingtalk_message.action_card_text,
|
"text" => dingtalk_message.action_card_text,
|
||||||
"hideAvatar" => dingtalk_message.action_card_hide_vatar,
|
"hideAvatar" => dingtalk_message.action_card_hide_avatar,
|
||||||
"btnOrientation" => dingtalk_message.action_card_btn_orientation,
|
"btnOrientation" => dingtalk_message.action_card_btn_orientation,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -439,6 +457,9 @@ impl <'a> DingTalk<'a> {
|
|||||||
|
|
||||||
/// Generate signed dingtalk webhook URL
|
/// Generate signed dingtalk webhook URL
|
||||||
pub fn generate_signed_url(&self) -> String {
|
pub fn generate_signed_url(&self) -> String {
|
||||||
|
if !self.direct_url.is_empty() {
|
||||||
|
return self.direct_url.into();
|
||||||
|
}
|
||||||
let mut signed_url = String::with_capacity(1024);
|
let mut signed_url = String::with_capacity(1024);
|
||||||
signed_url.push_str(self.default_webhook_url);
|
signed_url.push_str(self.default_webhook_url);
|
||||||
signed_url.push_str(&urlencoding::encode(self.access_token));
|
signed_url.push_str(&urlencoding::encode(self.access_token));
|
||||||
|
|||||||
Reference in New Issue
Block a user