From a91d8eebf40c81b5fe43d01c4753994757573324 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Sun, 17 Mar 2024 18:17:36 +0100 Subject: [PATCH] add webhook --- Cargo.lock | 28 +++++++++++++++++++++++++++- Cargo.toml | 2 +- docs/config.json.md | 1 + src/config.rs | 10 ++++++++++ src/notification.rs | 18 +++++++++++++++++- 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 49ff285..15409e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -282,7 +282,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" dependencies = [ - "hermit-abi", + "hermit-abi 0.1.19", "libc", "winapi", ] @@ -601,6 +601,12 @@ version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + [[package]] name = "futures-sink" version = "0.3.28" @@ -620,9 +626,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ "futures-core", + "futures-io", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -692,6 +701,12 @@ dependencies = [ "libc", ] +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "http" version = "0.2.9" @@ -1021,6 +1036,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi 0.3.9", + "libc", +] + [[package]] name = "object" version = "0.32.1" @@ -1559,6 +1584,7 @@ dependencies = [ "bytes", "libc", "mio", + "num_cpus", "parking_lot", "pin-project-lite", "signal-hook-registry", diff --git a/Cargo.toml b/Cargo.toml index bd28bd5..0c7551c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ gnupg-rs = "0.1.0" web-base = "0.2" serde = {version = "1.0.147", features = ["derive"] } serde_json = "1.0.87" -reqwest = "0.11" +reqwest = { version = "0.11", features = ["blocking", "json"] } maud = "0.24.0" tokio = { version = "1", features = ["sync"] } diff --git a/docs/config.json.md b/docs/config.json.md index f6a188e..bb7293a 100644 --- a/docs/config.json.md +++ b/docs/config.json.md @@ -6,6 +6,7 @@ - `secret_key` : Secret Key for Flask - `xmr_address` : Monero Receive Address - `notify` : Notification Object +- - `webhook`: Webhook Notification - - `gotify`: Gotify Object - - - `token` : Gotify Token - - - `host` : Gotify Host/Domain name diff --git a/src/config.rs b/src/config.rs index b22d422..1b0d934 100644 --- a/src/config.rs +++ b/src/config.rs @@ -77,4 +77,14 @@ impl Config { let token = settings.get("token")?.as_str()?.to_string(); Some(GotifySettings { host, token }) } + + pub fn webhook_config(&self) -> Option { + Some( + self.root + .get("notify")? + .get("webhook")? + .as_str()? + .to_string(), + ) + } } diff --git a/src/notification.rs b/src/notification.rs index 5b205d7..9c43d08 100644 --- a/src/notification.rs +++ b/src/notification.rs @@ -1,12 +1,28 @@ use crate::config; use crate::config::Config; -use actix_web::web::Data; +use actix_web::web::{self, Data}; use log::info; pub async fn notify(msg: &str, title: &str, config: Data) { if let Some(gotify) = config.gotify_config() { gotify_notification(msg, title, gotify).await; } + if let Some(webhook) = config.webhook_config() { + info!("Sending webhook notification"); + let request = + serde_json::json!({ + "instance": config.name(), + "title": title, + "msg": msg + }); + + let client = reqwest::blocking::Client::new(); + client + .post(webhook) + .json(&request) + .send() + .expect("Failed to send webhook request"); + } } async fn gotify_notification(msg: &str, title: &str, config: config::GotifySettings) {