1use serde::Serialize;
2
3#[derive(Debug, Clone)]
4pub struct SlackNotifier {
5 pub token: String,
6 pub channel: String,
7}
8
9#[derive(Serialize)]
10struct SlackMessage {
11 pub channel: String,
12 pub text: String,
13}
14
15impl SlackNotifier {
16 pub fn new(token: String, channel: String) -> Self {
17 SlackNotifier { token, channel }
18 }
19
20 pub async fn send_message(&self, message: String) -> Result<(), anyhow::Error> {
22 let req = SlackMessage {
23 channel: self.channel.clone(),
24 text: message,
25 };
26
27 let client = reqwest::Client::new();
28
29 let url = "https://slack.com/api/chat.postMessage";
30 let res = client
31 .post(url)
32 .header("Content-type", "application/json;charset=utf-8")
33 .header("Authorization", format!("Bearer {}", self.token))
34 .json(&req)
35 .send()
36 .await?
37 .json::<serde_json::Value>()
38 .await?;
39
40 if res["ok"] != true {
41 return Err(anyhow::anyhow!("Slack API error: {}", res["error"]));
42 }
43
44 Ok(())
45 }
46}