2 个版本

0.1.1 2020年1月8日
0.1.0 2020年1月7日

#9 in #示例

MIT 许可证

5KB
62 代码行

rs-notifier

当你的程序崩溃时,向你的服务器(例如:slack)发送消息通知

示例


use rs-notifier::notification::Notifier;
use rs-notifier::slack_notify::SlackNotify;

fn main() {
    let slack = SlackNotify::new("YOUR SLACK INCOMING WEBHOOK URL");
    let _ = slack.notify("test test");
}

开发

你可以通过实现 rs-notifier::notification::Notifier 来自定义通知器

示例

pub struct customNotify {
    pub url: String
}


impl customNotify {
    pub fn new(url: &str)-> SlackNotify{
        SlackNotify{
            url: url.to_owned()
        }
    }
}

impl Notifier for customNotify {
    type body = String;

    fn notify(&self, msg: &str) -> Result<(), String> {
        if &self.url == "" {
            return Err("not set url".to_owned());
        }
        let resp = ureq::post(&self.url)
                .set("Content-Type", "application/json")
                .set("Accept", "application/json")
                .send_json(json!(&self.bodybuild(msg)));
        if resp.error() {
            return Err("send error".to_owned());
        }
        Ok(())
    }

    fn bodybuild(&self, message: &str) -> Self::body {
        json!({
            "text": "server crash",
            "attachments":[
                {
                    "mrkdwn_in": ["text"],
                    "color": "#DD2248",
                    "text": message,
                }
            ]}).as_str().unwrap().to_owned()
    
    }
}

依赖项

~2–2.7MB
~76K SLoC