use crate::req::post_json;
use crate::JsonV;
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);
}