slack_api_client/
client.rs

1use crate::user;
2use reqwest::header;
3
4pub struct SlackClient {
5    pub client: reqwest::Client,
6}
7
8impl SlackClient {
9    pub fn new(bearer_token: String) -> Self {
10        let mut headers = header::HeaderMap::new();
11        headers.insert(
12            header::AUTHORIZATION,
13            header::HeaderValue::from_str(format!("Bearer {}", bearer_token).as_str())
14                .expect("failed to add Bearer token for slack client"),
15        );
16
17        let client = reqwest::ClientBuilder::new()
18            .default_headers(headers)
19            .build()
20            .expect("failed to build reqwest client");
21
22        SlackClient { client }
23    }
24
25    /// https://api.slack.com/methods/conversations.list
26    #[allow(dead_code)]
27    pub async fn get_conversation_list(&self) -> Result<String, reqwest::Error> {
28        self.client
29            .get("https://slack.com/api/conversations.list")
30            .send()
31            .await?
32            .text()
33            .await
34    }
35
36    /// https://api.slack.com/methods/chat.postMessage
37    /// errors: https://api.slack.com/methods/chat.postMessage#errors
38    pub async fn send_message(
39        &self,
40        payload: &serde_json::Value,
41        // w/ slack interactions/commands, a message can be sent to a provided response_url
42        // https://api.slack.com/interactivity/handling#message_responses
43        response_url: Option<&str>,
44    ) -> Result<reqwest::Response, reqwest::Error> {
45        let response = self
46            .client
47            .post(response_url.unwrap_or("https://slack.com/api/chat.postMessage"))
48            .header("Content-Type", "application/json; charset=utf-8")
49            .json(payload)
50            .send()
51            .await?;
52
53        log::info!("{} {}", response.status(), response.url());
54
55        Ok(response)
56    }
57
58    /// https://api.slack.com/methods/chat.delete
59    pub async fn delete_message(
60        &self,
61        channel_id: &str,
62        message_ts: &str,
63    ) -> Result<reqwest::Response, reqwest::Error> {
64        let response = self
65            .client
66            .post("https://slack.com/api/chat.delete")
67            .query(&[("channel", channel_id), ("ts", message_ts)])
68            .send()
69            .await?;
70
71        log::info!("{} {}", response.status(), response.url());
72
73        Ok(response)
74    }
75
76    pub async fn get_user_profile(&self, user_id: &str) -> GetUserProfileResult {
77        let result = self
78            .client
79            .post("https://slack.com/api/users.profile.get")
80            .query(&[("user", user_id)])
81            .send()
82            .await;
83
84        GetUserProfileResult {
85            result: result.inspect(|r| log::info!("{} {}", r.status(), r.url())),
86        }
87    }
88}
89
90pub struct GetUserProfileResult {
91    pub result: Result<reqwest::Response, reqwest::Error>,
92}
93
94impl GetUserProfileResult {
95    pub async fn body(self) -> Result<user::GetUserProfileResponse, reqwest::Error> {
96        self.result?.json().await
97    }
98}