slack_api_client/
message.rs

1use crate::client::SlackClient;
2
3pub enum CreateMessage {
4    /// https://api.slack.com/methods/chat.postMessage#arg_attachments
5    #[allow(dead_code)]
6    Attachments(serde_json::Value),
7    /// https://api.slack.com/methods/chat.postMessage#arg_blocks
8    Blocks(serde_json::Value),
9    /// https://api.slack.com/methods/chat.postMessage#arg_text
10    Text(String),
11}
12
13impl CreateMessage {
14    fn key_value(self) -> (&'static str, serde_json::Value) {
15        match self {
16            CreateMessage::Attachments(value) => ("attachments", value),
17            CreateMessage::Blocks(value) => ("blocks", value),
18            CreateMessage::Text(value) => ("text", serde_json::Value::String(value)),
19        }
20    }
21
22    pub fn to_request(self) -> serde_json::Value {
23        let (message_type, value) = self.key_value();
24
25        serde_json::json!({
26            //"response_type": "in_channel",
27            message_type: value,
28        })
29    }
30
31    pub async fn send_to_channel(
32        self,
33        client: &SlackClient,
34        channel: String,
35    ) -> Result<reqwest::Response, reqwest::Error> {
36        let mut request = self.to_request();
37        request["channel"] = serde_json::Value::String(channel);
38        client.send_message(&request, None).await
39    }
40
41    pub async fn send_response_url(
42        self,
43        client: &SlackClient,
44        response_url: &str,
45    ) -> Result<reqwest::Response, reqwest::Error> {
46        let request = self.to_request();
47        // request["response_type"] = serde_json::Value::String("in_channel");
48        client.send_message(&request, Some(response_url)).await
49    }
50}