add comments
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dingtalk"
|
name = "dingtalk"
|
||||||
version = "0.0.1"
|
version = "0.0.2"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "DingTalk Util"
|
description = "DingTalk Util"
|
||||||
|
|||||||
14
src/lib.rs
14
src/lib.rs
@@ -20,6 +20,14 @@ const APPLICATION_JSON_UTF8: &str = "application/json; charset=utf-8";
|
|||||||
|
|
||||||
const DEFAULT_DINGTALK_ROBOT_URL: &str = "https://oapi.dingtalk.com/robot/send?access_token=";
|
const DEFAULT_DINGTALK_ROBOT_URL: &str = "https://oapi.dingtalk.com/robot/send?access_token=";
|
||||||
|
|
||||||
|
/// `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!")?;
|
||||||
|
/// ```
|
||||||
pub struct DingTalk<'a> {
|
pub struct DingTalk<'a> {
|
||||||
pub access_token: &'a str,
|
pub access_token: &'a str,
|
||||||
pub sec_token: &'a str,
|
pub sec_token: &'a str,
|
||||||
@@ -27,6 +35,8 @@ pub struct DingTalk<'a> {
|
|||||||
|
|
||||||
impl <'a> DingTalk<'a> {
|
impl <'a> DingTalk<'a> {
|
||||||
|
|
||||||
|
/// Create `DingTalk`
|
||||||
|
/// `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 {
|
||||||
DingTalk {
|
DingTalk {
|
||||||
access_token: access_token,
|
access_token: access_token,
|
||||||
@@ -34,6 +44,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send text message
|
||||||
pub fn send_text(&self, text_message: &str) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn send_text(&self, text_message: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
self.send(&json::stringify(object!{
|
self.send(&json::stringify(object!{
|
||||||
"msgtype" => "text",
|
"msgtype" => "text",
|
||||||
@@ -43,6 +54,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send markdown message
|
||||||
pub fn send_markdown(&self, title: &str, text: &str) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn send_markdown(&self, title: &str, text: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
self.send(&json::stringify(object!{
|
self.send(&json::stringify(object!{
|
||||||
"msgtype" => "markdown",
|
"msgtype" => "markdown",
|
||||||
@@ -53,6 +65,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Direct send JSON message
|
||||||
pub fn send(&self, json_message: &str) -> Result<(), Box<dyn std::error::Error>> {
|
pub fn send(&self, json_message: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = client.post(&self.generate_signed_url())
|
let response = client.post(&self.generate_signed_url())
|
||||||
@@ -66,6 +79,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generate signed dingtalk webhook URL
|
||||||
pub fn generate_signed_url(&self) -> String {
|
pub fn generate_signed_url(&self) -> String {
|
||||||
let mut signed_url = String::with_capacity(1024);
|
let mut signed_url = String::with_capacity(1024);
|
||||||
signed_url.push_str(DEFAULT_DINGTALK_ROBOT_URL);
|
signed_url.push_str(DEFAULT_DINGTALK_ROBOT_URL);
|
||||||
|
|||||||
Reference in New Issue
Block a user