slack_rust/chat/
me_message.rs

1//! Share a me message into a channel.  
2//! See: <https://api.slack.com/methods/chat.meMessage>
3
4use crate::error::Error;
5use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
6use serde::{Deserialize, Serialize};
7use serde_with::skip_serializing_none;
8
9#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
10pub struct MeMessageRequest {
11    pub channel: String,
12    pub text: String,
13}
14
15#[skip_serializing_none]
16#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
17pub struct MeMessageResponse {
18    pub ok: bool,
19    pub error: Option<String>,
20    pub response_metadata: Option<ResponseMetadata>,
21    pub channel: Option<String>,
22    pub ts: Option<String>,
23}
24
25/// Share a me message into a channel.  
26/// See: <https://api.slack.com/methods/chat.meMessage>
27pub async fn me_message<T>(
28    client: &T,
29    param: &MeMessageRequest,
30    bot_token: &str,
31) -> Result<MeMessageResponse, Error>
32where
33    T: SlackWebAPIClient,
34{
35    let url = get_slack_url("chat.meMessage");
36    let json = serde_json::to_string(&param)?;
37
38    client
39        .post_json(&url, &json, bot_token)
40        .await
41        .and_then(|result| {
42            serde_json::from_str::<MeMessageResponse>(&result).map_err(Error::SerdeJsonError)
43        })
44}
45
46#[cfg(test)]
47mod test {
48    use super::*;
49    use crate::http_client::MockSlackWebAPIClient;
50
51    #[test]
52    fn convert_request() {
53        let request = MeMessageRequest {
54            channel: "C1234567890".to_string(),
55            text: "Hello world".to_string(),
56        };
57        let json = r##"{
58  "channel": "C1234567890",
59  "text": "Hello world"
60}"##;
61
62        let j = serde_json::to_string_pretty(&request).unwrap();
63        assert_eq!(json, j);
64
65        let s = serde_json::from_str::<MeMessageRequest>(json).unwrap();
66        assert_eq!(request, s);
67    }
68
69    #[test]
70    fn convert_response() {
71        let response = MeMessageResponse {
72            ok: true,
73            channel: Some("C024BE7LR".to_string()),
74            ts: Some("1417671948.000006".to_string()),
75            ..Default::default()
76        };
77        let json = r##"{
78  "ok": true,
79  "channel": "C024BE7LR",
80  "ts": "1417671948.000006"
81}"##;
82
83        let j = serde_json::to_string_pretty(&response).unwrap();
84        assert_eq!(json, j);
85
86        let s = serde_json::from_str::<MeMessageResponse>(json).unwrap();
87        assert_eq!(response, s);
88    }
89
90    #[async_std::test]
91    async fn test_me_message() {
92        let param = MeMessageRequest {
93            channel: "C1234567890".to_string(),
94            text: "Hello world".to_string(),
95        };
96
97        let mut mock = MockSlackWebAPIClient::new();
98        mock.expect_post_json().returning(|_, _, _| {
99            Ok(r##"{
100  "ok": true,
101  "channel": "C1234567890",
102  "ts": "1417671948.000006"
103}"##
104            .to_string())
105        });
106
107        let response = me_message(&mock, &param, &"test_token".to_string())
108            .await
109            .unwrap();
110        let expect = MeMessageResponse {
111            ok: true,
112            channel: Some("C1234567890".to_string()),
113            ts: Some("1417671948.000006".to_string()),
114            ..Default::default()
115        };
116
117        assert_eq!(expect, response);
118    }
119}