common_uu 1.9.4

公共工具库
Documentation
// 发送钉钉消息
// todo 需要完成消息合并发送功能

use crate::req::post_json;
use crate::JsonV;
// use std::thread::sleep;
// use std::time::Duration;

/// 发送消息模版
const MSG_TEMP: &str = r##"{
    "msgtype": "text", 
    "text": {
        "content": "{1}"
    }, 
    "at": {
        "atMobiles": [], 
        "isAtAll": false
    }
}"##;

/// 发送错误消息
pub fn error(body_str: impl Into<String>, url: impl Into<String>) {
    send_message("错误消息", body_str.into(), url.into())
}

/// 发送警告消息
pub fn warn(body_str: impl Into<String>, url: impl Into<String>) {
    send_message("警告消息", body_str.into(), url.into())
}

fn send_message(msg_type: &str, body_str: String, url: String) {
    let msg = format!("[{}] {}", msg_type, body_str).replace('"', "'");
    let msg = MSG_TEMP.replace("{1}", &msg);
    let result = post_json::<JsonV>(url, serde_json::from_str(&msg).unwrap_or_default());
    if let Err(e) = result {
        println!("发送钉钉消息失败: {:?}", e);
    } else {
        println!("发送钉钉消息成功: {:?}", result);
    }
}

#[test]
fn test_send_warn_message() {
    warn("网络异常: 可能网速不佳导致".to_owned(), "https://oapi.dingtalk.com/robot/send?access_token=c70af6e8299cdb5c30d1895ae54e5288557c79911f6bd6276b4d499c22a19f42".to_owned());
}

#[test]
fn send_error_message_test() {
    error("测试".to_owned(), "https://oapi.dingtalk.com/robot/send?access_token=c70af6e8299cdb5c30d1895ae54e5288557c79911f6bd6276b4d499c22a19f42".to_owned());
}

#[test]
fn post_test() {
    let msg = MSG_TEMP.replace("{1}", "警告消息 郭宇的测试消息1111");
    let res = post_json::<serde_json::Value>("https://oapi.dingtalk.com/robot/send?access_token=c70af6e8299cdb5c30d1895ae54e5288557c79911f6bd6276b4d499c22a19f42".to_owned(), serde_json::from_str(&msg).unwrap());
    println!("res: {:?}", res);
}