Feature/slack_notify#134 (#139)

* add slack notify

* cargo fmt --all

* slack-hook version up

* change bot name
This commit is contained in:
nishikawaakira
2021-09-30 20:43:50 +09:00
committed by GitHub
parent 76103d31f3
commit 23c60fa8ff
9 changed files with 1193 additions and 62 deletions

1
src/notify/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod slack;

57
src/notify/slack.rs Normal file
View 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)
}
}