slack_rust/conversations/
replies.rs1use crate::chat::message::Message;
2use crate::error::Error;
3use crate::http_client::{get_slack_url, ResponseMetadata, SlackWebAPIClient};
4use serde::{Deserialize, Serialize};
5use serde_with::skip_serializing_none;
6
7#[skip_serializing_none]
8#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
9pub struct RepliesRequest {
10 pub channel: String,
11 pub ts: String,
12 pub cursor: Option<String>,
13 pub inclusive: Option<bool>,
14 pub latest: Option<String>,
15 pub limit: Option<i32>,
16 pub oldest: Option<String>,
17}
18
19#[skip_serializing_none]
20#[derive(Deserialize, Serialize, Debug, Default, PartialEq)]
21pub struct RepliesResponse {
22 pub ok: bool,
23 pub error: Option<String>,
24 pub response_metadata: Option<ResponseMetadata>,
25 pub messages: Option<Vec<Message>>,
26 pub has_more: Option<bool>,
27}
28
29pub async fn replies<T>(
30 client: &T,
31 param: &RepliesRequest,
32 bot_token: &str,
33) -> Result<RepliesResponse, Error>
34where
35 T: SlackWebAPIClient,
36{
37 let url = get_slack_url("conversations.replies");
38 let json = serde_json::to_string(¶m)?;
39
40 client
41 .post_json(&url, &json, bot_token)
42 .await
43 .and_then(|result| {
44 serde_json::from_str::<RepliesResponse>(&result).map_err(Error::SerdeJsonError)
45 })
46}
47
48#[cfg(test)]
49mod test {
50 use super::*;
51 use crate::http_client::MockSlackWebAPIClient;
52
53 #[test]
54 fn convert_request() {
55 let request = RepliesRequest {
56 channel: "C1234567890".to_string(),
57 ts: "1234567890.123456".to_string(),
58 cursor: Some("dXNlcjpVMDYxTkZUVDI=".to_string()),
59 inclusive: Some(true),
60 latest: Some("1234567890.123456".to_string()),
61 limit: Some(20),
62 oldest: Some("1234567890.123456".to_string()),
63 };
64 let json = r##"{
65 "channel": "C1234567890",
66 "ts": "1234567890.123456",
67 "cursor": "dXNlcjpVMDYxTkZUVDI=",
68 "inclusive": true,
69 "latest": "1234567890.123456",
70 "limit": 20,
71 "oldest": "1234567890.123456"
72}"##;
73
74 let j = serde_json::to_string_pretty(&request).unwrap();
75 assert_eq!(json, j);
76
77 let s = serde_json::from_str::<RepliesRequest>(json).unwrap();
78 assert_eq!(request, s);
79 }
80
81 #[test]
82 fn convert_response() {
83 let response = RepliesResponse {
84 ok: true,
85 messages: Some(vec![
86 Message {
87 type_file: Some("message".to_string()),
88 text: Some("island".to_string()),
89 user: Some("U061F7AUR".to_string()),
90 ts: Some("1482960137.003543".to_string()),
91 ..Default::default()
92 },
93 Message {
94 type_file: Some("message".to_string()),
95 text: Some("island".to_string()),
96 user: Some("U061F7AUR".to_string()),
97 ts: Some("1482960137.003543".to_string()),
98 ..Default::default()
99 },
100 Message {
101 type_file: Some("message".to_string()),
102 text: Some("island".to_string()),
103 user: Some("U061F7AUR".to_string()),
104 ts: Some("1482960137.003543".to_string()),
105 ..Default::default()
106 },
107 ]),
108 has_more: Some(true),
109 ..Default::default()
110 };
111 let json = r##"{
112 "ok": true,
113 "messages": [
114 {
115 "type": "message",
116 "text": "island",
117 "user": "U061F7AUR",
118 "ts": "1482960137.003543"
119 },
120 {
121 "type": "message",
122 "text": "island",
123 "user": "U061F7AUR",
124 "ts": "1482960137.003543"
125 },
126 {
127 "type": "message",
128 "text": "island",
129 "user": "U061F7AUR",
130 "ts": "1482960137.003543"
131 }
132 ],
133 "has_more": true
134}"##;
135
136 let j = serde_json::to_string_pretty(&response).unwrap();
137 assert_eq!(json, j);
138
139 let s = serde_json::from_str::<RepliesResponse>(json).unwrap();
140 assert_eq!(response, s);
141 }
142
143 #[async_std::test]
144 async fn test_replies() {
145 let param = RepliesRequest {
146 channel: "C1234567890".to_string(),
147 ts: "1234567890.123456".to_string(),
148 cursor: Some("dXNlcjpVMDYxTkZUVDI=".to_string()),
149 inclusive: Some(true),
150 latest: Some("1234567890.123456".to_string()),
151 limit: Some(20),
152 oldest: Some("1234567890.123456".to_string()),
153 };
154 let mut mock = MockSlackWebAPIClient::new();
155 mock.expect_post_json().returning(|_, _, _| {
156 Ok(r##"{
157 "ok": true,
158 "messages": [
159 {
160 "type": "message",
161 "text": "island",
162 "user": "U061F7AUR",
163 "ts": "1482960137.003543"
164 },
165 {
166 "type": "message",
167 "text": "island",
168 "user": "U061F7AUR",
169 "ts": "1482960137.003543"
170 },
171 {
172 "type": "message",
173 "text": "island",
174 "user": "U061F7AUR",
175 "ts": "1482960137.003543"
176 }
177 ],
178 "has_more": true
179}"##
180 .to_string())
181 });
182
183 let response = replies(&mock, ¶m, &"test_token".to_string())
184 .await
185 .unwrap();
186 let expect = RepliesResponse {
187 ok: true,
188 messages: Some(vec![
189 Message {
190 type_file: Some("message".to_string()),
191 text: Some("island".to_string()),
192 user: Some("U061F7AUR".to_string()),
193 ts: Some("1482960137.003543".to_string()),
194 ..Default::default()
195 },
196 Message {
197 type_file: Some("message".to_string()),
198 text: Some("island".to_string()),
199 user: Some("U061F7AUR".to_string()),
200 ts: Some("1482960137.003543".to_string()),
201 ..Default::default()
202 },
203 Message {
204 type_file: Some("message".to_string()),
205 text: Some("island".to_string()),
206 user: Some("U061F7AUR".to_string()),
207 ts: Some("1482960137.003543".to_string()),
208 ..Default::default()
209 },
210 ]),
211 has_more: Some(true),
212 ..Default::default()
213 };
214
215 assert_eq!(expect, response);
216 }
217}