good-notifier 0.1.102

A good notifier for telegram and slack, and creating json logs
Documentation
use serde::Serialize;

#[derive(Debug, Clone)]
pub struct SlackNotifier {
    pub token: String,
    pub channel: String,
}

#[derive(Serialize)]
struct SlackMessage {
    pub channel: String,
    pub text: String,
}

impl SlackNotifier {
    pub fn new(token: String, channel: String) -> Self {
        SlackNotifier { token, channel }
    }

    // barebones send slack message
    pub async fn send_message(&self, message: String) -> Result<(), anyhow::Error> {
        let req = SlackMessage {
            channel: self.channel.clone(),
            text: message,
        };

        let client = reqwest::Client::new();

        let url = "https://slack.com/api/chat.postMessage";
        let res = client
            .post(url)
            .header("Content-type", "application/json;charset=utf-8")
            .header("Authorization", format!("Bearer {}", self.token))
            .json(&req)
            .send()
            .await?
            .json::<serde_json::Value>()
            .await?;

        if res["ok"] != true {
            return Err(anyhow::anyhow!("Slack API error: {}", res["error"]));
        }

        Ok(())
    }
}