Feature/slack_notify#134 (#139)
* add slack notify * cargo fmt --all * slack-hook version up * change bot name
This commit is contained in:
1
src/notify/mod.rs
Normal file
1
src/notify/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod slack;
|
||||
57
src/notify/slack.rs
Normal file
57
src/notify/slack.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
extern crate slack_hook;
|
||||
use dotenv::dotenv;
|
||||
use slack_hook::{PayloadBuilder, Slack};
|
||||
use std::env;
|
||||
|
||||
pub struct SlackNotify {}
|
||||
|
||||
impl SlackNotify {
|
||||
// Check if Slack is configured.
|
||||
pub fn check_setting() -> bool {
|
||||
dotenv().ok();
|
||||
if env::var("CHANNEL").is_err() {
|
||||
eprintln!("Channel not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
if env::var("WEBHOOK_URL").is_err() {
|
||||
eprintln!("WEBHOOK_URL not found");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// send message to slack.
|
||||
pub fn notify(msg: String) -> Result<(), String> {
|
||||
dotenv().ok();
|
||||
if !SlackNotify::check_setting() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let channel = env::var("CHANNEL").expect("CHANNEL is not found");
|
||||
let webhook_url = env::var("WEBHOOK_URL").expect("WEBHOOK_URL is not found");
|
||||
let ret = SlackNotify::_send_to_slack(msg, &channel, &webhook_url);
|
||||
if ret.is_ok() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Slack Notification Failed.".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
fn _send_to_slack(
|
||||
msg: String,
|
||||
channel: &str,
|
||||
webhook_url: &str,
|
||||
) -> Result<(), slack_hook::Error> {
|
||||
let slack = Slack::new(webhook_url).unwrap();
|
||||
let p = PayloadBuilder::new()
|
||||
.text(msg)
|
||||
.channel(channel)
|
||||
.username("Lagotto Notify Bot")
|
||||
.icon_emoji(":scream:")
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
slack.send(&p)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user