v1.3.0
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dingtalk"
|
name = "dingtalk"
|
||||||
version = "1.2.1"
|
version = "1.3.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, 钉钉机器人"
|
||||||
|
|||||||
20
README.md
20
README.md
@@ -88,9 +88,29 @@ dt.send_message(&DingTalkMessage::new_action_card("action card 002", "action car
|
|||||||
).await?;
|
).await?;
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### JSON Config
|
||||||
|
|
||||||
|
DingTalk config:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"access_token": "<access token>",
|
||||||
|
"sec_token": "<sec token>"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
WeChat Work config:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"type": "wechat",
|
||||||
|
"access_token": "<token>"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
#### Changelog
|
#### Changelog
|
||||||
|
|
||||||
|
* v1.3.0
|
||||||
|
* Suports WeChat Work now, add type `"type": "wechat"`, supports method `DingTalk::send_text`
|
||||||
* v1.2.1
|
* v1.2.1
|
||||||
* Remove `maplit` crate
|
* Remove `maplit` crate
|
||||||
* v1.2.0
|
* v1.2.0
|
||||||
|
|||||||
33
src/lib.rs
33
src/lib.rs
@@ -28,6 +28,20 @@ const CONTENT_TYPE: &str = "Content-Type";
|
|||||||
const APPLICATION_JSON_UTF8: &str = "application/json; charset=utf-8";
|
const APPLICATION_JSON_UTF8: &str = "application/json; charset=utf-8";
|
||||||
|
|
||||||
const DEFAULT_DINGTALK_ROBOT_URL: &str = "https://oapi.dingtalk.com/robot/send";
|
const DEFAULT_DINGTALK_ROBOT_URL: &str = "https://oapi.dingtalk.com/robot/send";
|
||||||
|
const DEFAULT_WECHAT_WORK_ROBOT_URL: &str = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send";
|
||||||
|
|
||||||
|
/// Send Dingtalk or WeChatWork message
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum DingTalkType {
|
||||||
|
|
||||||
|
DingTalk,
|
||||||
|
|
||||||
|
WeChatWork,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for DingTalkType {
|
||||||
|
fn default() -> Self { DingTalkType::DingTalk }
|
||||||
|
}
|
||||||
|
|
||||||
/// `DingTalk` is a simple SDK for DingTalk webhook robot
|
/// `DingTalk` is a simple SDK for DingTalk webhook robot
|
||||||
///
|
///
|
||||||
@@ -45,6 +59,7 @@ const DEFAULT_DINGTALK_ROBOT_URL: &str = "https://oapi.dingtalk.com/robot/send";
|
|||||||
/// ```
|
/// ```
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DingTalk<'a> {
|
pub struct DingTalk<'a> {
|
||||||
|
pub dingtalk_type: DingTalkType,
|
||||||
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,
|
||||||
@@ -208,13 +223,24 @@ impl <'a> DingTalk<'a> {
|
|||||||
if !json_value.is_object() {
|
if !json_value.is_object() {
|
||||||
return Err(Box::new(Error::new(ErrorKind::Other, format!("JSON format erorr: {}", json))));
|
return Err(Box::new(Error::new(ErrorKind::Other, format!("JSON format erorr: {}", json))));
|
||||||
}
|
}
|
||||||
|
let type_str = json_value["type"].as_str().unwrap_or_default().to_lowercase();
|
||||||
|
let dingtalk_type = match type_str.as_str() {
|
||||||
|
"wechat" | "wechatwork" => DingTalkType::WeChatWork,
|
||||||
|
_ => DingTalkType::DingTalk,
|
||||||
|
};
|
||||||
|
|
||||||
let default_webhook_url = Self::string_to_a_str(json_value["default_webhook_url"].as_str().unwrap_or(DEFAULT_DINGTALK_ROBOT_URL));
|
let default_webhook_url = Self::string_to_a_str(json_value["default_webhook_url"].as_str().unwrap_or_else(
|
||||||
|
|| match dingtalk_type {
|
||||||
|
DingTalkType::DingTalk => DEFAULT_DINGTALK_ROBOT_URL,
|
||||||
|
DingTalkType::WeChatWork => DEFAULT_WECHAT_WORK_ROBOT_URL,
|
||||||
|
}
|
||||||
|
));
|
||||||
let access_token = Self::string_to_a_str(json_value["access_token"].as_str().unwrap_or_default());
|
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 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());
|
let direct_url = Self::string_to_a_str(json_value["direct_url"].as_str().unwrap_or_default());
|
||||||
|
|
||||||
Ok(DingTalk {
|
Ok(DingTalk {
|
||||||
|
dingtalk_type,
|
||||||
default_webhook_url,
|
default_webhook_url,
|
||||||
access_token,
|
access_token,
|
||||||
sec_token,
|
sec_token,
|
||||||
@@ -383,7 +409,10 @@ impl <'a> DingTalk<'a> {
|
|||||||
signed_url.push('?');
|
signed_url.push('?');
|
||||||
}
|
}
|
||||||
|
|
||||||
signed_url.push_str("access_token=");
|
match self.dingtalk_type {
|
||||||
|
DingTalkType::DingTalk => signed_url.push_str("access_token="),
|
||||||
|
DingTalkType::WeChatWork => signed_url.push_str("key="),
|
||||||
|
}
|
||||||
signed_url.push_str(&urlencoding::encode(self.access_token));
|
signed_url.push_str(&urlencoding::encode(self.access_token));
|
||||||
|
|
||||||
if !self.sec_token.is_empty() {
|
if !self.sec_token.is_empty() {
|
||||||
|
|||||||
13
tests/test_wechatwork.rs
Normal file
13
tests/test_wechatwork.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use dingtalk::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_all_tests_wechat_work() {
|
||||||
|
tokio_test::block_on(_test_send_wechat_work()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn _test_send_wechat_work() -> XResult<()> {
|
||||||
|
let dt = DingTalk::from_file("~/.wechat-work-token.json")?;
|
||||||
|
dt.send_text("test message 001 ---------------------").await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user