/ src / notification.rs
notification.rs
 1  use eyre::Result;
 2  use log::info;
 3  use std::env;
 4  
 5  pub struct Notification {
 6      pub url: Option<String>,
 7      pub message: String,
 8  }
 9  
10  pub trait Sendable {
11      async fn send(&self) -> Result<()>;
12  }
13  
14  impl Sendable for Notification {
15      async fn send(&self) -> Result<()> {
16          let ntfy_disable = env::var("NTFY_DISABLE")
17              .unwrap_or("false".into())
18              .to_lowercase();
19  
20          if ntfy_disable == "true" {
21              info!("{}", self.message);
22          } else {
23              let ntfy_url = env::var("NTFY_URL").expect("Missing NTFY_URL");
24              let ntfy_topic = env::var("NTFY_TOPIC").expect("Missing NTFY_TOPIC");
25              let ntfy_token = env::var("NTFY_TOKEN").expect("Missing NTFY_TOKEN");
26  
27              let client = reqwest::Client::new();
28              client
29                  .post(format!("{}/{}", ntfy_url, ntfy_topic))
30                  .body(self.message.clone())
31                  .header("Authorization", format!("Bearer {}", ntfy_token))
32                  .header(
33                      "Actions",
34                      if self.url.is_some() {
35                          format!("view, Explorer, {}, clear=true", self.url.as_ref().unwrap())
36                      } else {
37                          "".to_string()
38                      },
39                  )
40                  .send()
41                  .await?;
42          }
43          Ok(())
44      }
45  }