1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::client::SlackClient;

pub enum CreateMessage {
    /// https://api.slack.com/methods/chat.postMessage#arg_attachments
    #[allow(dead_code)]
    Attachments(serde_json::Value),
    /// https://api.slack.com/methods/chat.postMessage#arg_blocks
    Blocks(serde_json::Value),
    /// https://api.slack.com/methods/chat.postMessage#arg_text
    Text(String),
}

impl CreateMessage {
    fn key_value(self) -> (&'static str, serde_json::Value) {
        match self {
            CreateMessage::Attachments(value) => ("attachments", value),
            CreateMessage::Blocks(value) => ("blocks", value),
            CreateMessage::Text(value) => ("text", serde_json::Value::String(value)),
        }
    }

    pub fn to_request(self) -> serde_json::Value {
        let (message_type, value) = self.key_value();

        serde_json::json!({
            //"response_type": "in_channel",
            message_type: value,
        })
    }

    pub async fn send_to_channel(
        self,
        client: &SlackClient,
        channel: String,
    ) -> Result<reqwest::Response, reqwest::Error> {
        let mut request = self.to_request();
        request["channel"] = serde_json::Value::String(channel);
        client.send_message(request.to_string(), None).await
    }

    pub async fn send_response_url(
        self,
        client: &SlackClient,
        response_url: &str,
    ) -> Result<reqwest::Response, reqwest::Error> {
        let request = self.to_request();
        // request["response_type"] = serde_json::Value::String("in_channel");
        client
            .send_message(request.to_string(), Some(response_url))
            .await
    }
}