common_uu/
send_dingding_msg.rs

1// 发送钉钉消息
2// todo 需要完成消息合并发送功能
3
4use crate::req::post_json;
5use crate::JsonV;
6// use std::thread::sleep;
7// use std::time::Duration;
8
9/// 发送消息模版
10const MSG_TEMP: &str = r##"{
11    "msgtype": "text", 
12    "text": {
13        "content": "{1}"
14    }, 
15    "at": {
16        "atMobiles": [], 
17        "isAtAll": false
18    }
19}"##;
20
21/// 发送错误消息
22pub fn error(body_str: impl Into<String>, url: impl Into<String>) {
23    send_message("错误消息", body_str.into(), url.into())
24}
25
26/// 发送警告消息
27pub fn warn(body_str: impl Into<String>, url: impl Into<String>) {
28    send_message("警告消息", body_str.into(), url.into())
29}
30
31fn send_message(msg_type: &str, body_str: String, url: String) {
32    let msg = format!("[{}] {}", msg_type, body_str).replace('"', "'");
33    let msg = MSG_TEMP.replace("{1}", &msg);
34    let result = post_json::<JsonV>(url, serde_json::from_str(&msg).unwrap_or_default());
35    if let Err(e) = result {
36        println!("发送钉钉消息失败: {:?}", e);
37    } else {
38        println!("发送钉钉消息成功: {:?}", result);
39    }
40}
41
42#[test]
43fn test_send_warn_message() {
44    warn("网络异常: 可能网速不佳导致".to_owned(), "https://oapi.dingtalk.com/robot/send?access_token=c70af6e8299cdb5c30d1895ae54e5288557c79911f6bd6276b4d499c22a19f42".to_owned());
45}
46
47#[test]
48fn send_error_message_test() {
49    error("测试".to_owned(), "https://oapi.dingtalk.com/robot/send?access_token=c70af6e8299cdb5c30d1895ae54e5288557c79911f6bd6276b4d499c22a19f42".to_owned());
50}
51
52#[test]
53fn post_test() {
54    let msg = MSG_TEMP.replace("{1}", "警告消息 郭宇的测试消息1111");
55    let res = post_json::<serde_json::Value>("https://oapi.dingtalk.com/robot/send?access_token=c70af6e8299cdb5c30d1895ae54e5288557c79911f6bd6276b4d499c22a19f42".to_owned(), serde_json::from_str(&msg).unwrap());
56    println!("res: {:?}", res);
57}