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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#[allow(unused_imports)]
use std::collections::HashMap;
use std::convert::From;
use std::error::Error;
use std::fmt;
use serde_json;
use requests::SlackWebRequestSender;
pub fn connect<R>(client: &R, token: &str) -> Result<ConnectResponse, ConnectError<R::Error>>
where R: SlackWebRequestSender
{
let params = &[("token", token)];
let url = ::get_slack_url_for_method("rtm.connect");
client
.send(&url, ¶ms[..])
.map_err(|err| ConnectError::Client(err))
.and_then(|result| {
serde_json::from_str::<ConnectResponse>(&result)
.map_err(|e| ConnectError::MalformedResponse(e))
})
.and_then(|o| o.into())
}
#[derive(Clone, Debug, Deserialize)]
pub struct ConnectResponse {
error: Option<String>,
#[serde(default)]
ok: bool,
#[serde(rename = "self")]
pub slf: Option<ConnectResponseSelf>,
pub team: Option<ConnectResponseTeam>,
pub url: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct ConnectResponseSelf {
pub id: Option<String>,
pub name: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct ConnectResponseTeam {
pub domain: Option<String>,
pub enterprise_id: Option<String>,
pub enterprise_name: Option<String>,
pub id: Option<String>,
pub name: Option<String>,
}
impl<E: Error> Into<Result<ConnectResponse, ConnectError<E>>> for ConnectResponse {
fn into(self) -> Result<ConnectResponse, ConnectError<E>> {
if self.ok {
Ok(self)
} else {
Err(self.error
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.into())
}
}
}
#[derive(Debug)]
pub enum ConnectError<E: Error> {
NotAuthed,
InvalidAuth,
AccountInactive,
InvalidArgName,
InvalidArrayArg,
InvalidCharset,
InvalidFormData,
InvalidPostType,
MissingPostType,
TeamAddedToOrg,
RequestTimeout,
MalformedResponse(serde_json::error::Error),
Unknown(String),
Client(E),
}
impl<'a, E: Error> From<&'a str> for ConnectError<E> {
fn from(s: &'a str) -> Self {
match s {
"not_authed" => ConnectError::NotAuthed,
"invalid_auth" => ConnectError::InvalidAuth,
"account_inactive" => ConnectError::AccountInactive,
"invalid_arg_name" => ConnectError::InvalidArgName,
"invalid_array_arg" => ConnectError::InvalidArrayArg,
"invalid_charset" => ConnectError::InvalidCharset,
"invalid_form_data" => ConnectError::InvalidFormData,
"invalid_post_type" => ConnectError::InvalidPostType,
"missing_post_type" => ConnectError::MissingPostType,
"team_added_to_org" => ConnectError::TeamAddedToOrg,
"request_timeout" => ConnectError::RequestTimeout,
_ => ConnectError::Unknown(s.to_owned()),
}
}
}
impl<E: Error> fmt::Display for ConnectError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl<E: Error> Error for ConnectError<E> {
fn description(&self) -> &str {
match self {
&ConnectError::NotAuthed => "not_authed: No authentication token provided.",
&ConnectError::InvalidAuth => "invalid_auth: Invalid authentication token.",
&ConnectError::AccountInactive => "account_inactive: Authentication token is for a deleted user or team.",
&ConnectError::InvalidArgName => "invalid_arg_name: The method was passed an argument whose name falls outside the bounds of common decency. This includes very long names and names with non-alphanumeric characters other than _. If you get this error, it is typically an indication that you have made a very malformed API call.",
&ConnectError::InvalidArrayArg => "invalid_array_arg: The method was passed a PHP-style array argument (e.g. with a name like foo[7]). These are never valid with the Slack API.",
&ConnectError::InvalidCharset => "invalid_charset: The method was called via a POST request, but the charset specified in the Content-Type header was invalid. Valid charset names are: utf-8 iso-8859-1.",
&ConnectError::InvalidFormData => "invalid_form_data: The method was called via a POST request with Content-Type application/x-www-form-urlencoded or multipart/form-data, but the form data was either missing or syntactically invalid.",
&ConnectError::InvalidPostType => "invalid_post_type: The method was called via a POST request, but the specified Content-Type was invalid. Valid types are: application/x-www-form-urlencoded multipart/form-data text/plain.",
&ConnectError::MissingPostType => "missing_post_type: The method was called via a POST request and included a data payload, but the request did not include a Content-Type header.",
&ConnectError::TeamAddedToOrg => "team_added_to_org: The team associated with your request is currently undergoing migration to an Enterprise Organization. Web API and other platform operations will be intermittently unavailable until the transition is complete.",
&ConnectError::RequestTimeout => "request_timeout: The method was called via a POST request, but the POST data was either missing or truncated.",
&ConnectError::MalformedResponse(ref e) => e.description(),
&ConnectError::Unknown(ref s) => s,
&ConnectError::Client(ref inner) => inner.description(),
}
}
fn cause(&self) -> Option<&Error> {
match self {
&ConnectError::MalformedResponse(ref e) => Some(e),
&ConnectError::Client(ref inner) => Some(inner),
_ => None,
}
}
}
pub fn start<R>(client: &R,
token: &str,
request: &StartRequest)
-> Result<StartResponse, StartError<R::Error>>
where R: SlackWebRequestSender
{
let params =
vec![Some(("token", token)),
request
.no_unreads
.map(|no_unreads| ("no_unreads", if no_unreads { "1" } else { "0" })),
request
.mpim_aware
.map(|mpim_aware| ("mpim_aware", if mpim_aware { "1" } else { "0" })),
request
.no_latest
.map(|no_latest| ("no_latest", if no_latest { "1" } else { "0" }))];
let params = params.into_iter().filter_map(|x| x).collect::<Vec<_>>();
let url = ::get_slack_url_for_method("rtm.start");
client
.send(&url, ¶ms[..])
.map_err(|err| StartError::Client(err))
.and_then(|result| {
serde_json::from_str::<StartResponse>(&result)
.map_err(|e| StartError::MalformedResponse(e))
})
.and_then(|o| o.into())
}
#[derive(Clone, Default, Debug)]
pub struct StartRequest {
pub no_unreads: Option<bool>,
pub mpim_aware: Option<bool>,
pub no_latest: Option<bool>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct StartResponse {
pub bots: Option<Vec<::Bot>>,
pub channels: Option<Vec<::Channel>>,
error: Option<String>,
pub groups: Option<Vec<::Group>>,
pub ims: Option<Vec<::Im>>,
pub mpims: Option<Vec<::Mpim>>,
#[serde(default)]
ok: bool,
#[serde(rename = "self")]
pub slf: Option<::User>,
pub team: Option<::Team>,
pub url: Option<String>,
pub users: Option<Vec<::User>>,
}
impl<E: Error> Into<Result<StartResponse, StartError<E>>> for StartResponse {
fn into(self) -> Result<StartResponse, StartError<E>> {
if self.ok {
Ok(self)
} else {
Err(self.error
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.into())
}
}
}
#[derive(Debug)]
pub enum StartError<E: Error> {
MigrationInProgress,
NotAuthed,
InvalidAuth,
AccountInactive,
InvalidArgName,
InvalidArrayArg,
InvalidCharset,
InvalidFormData,
InvalidPostType,
MissingPostType,
TeamAddedToOrg,
RequestTimeout,
MalformedResponse(serde_json::error::Error),
Unknown(String),
Client(E),
}
impl<'a, E: Error> From<&'a str> for StartError<E> {
fn from(s: &'a str) -> Self {
match s {
"migration_in_progress" => StartError::MigrationInProgress,
"not_authed" => StartError::NotAuthed,
"invalid_auth" => StartError::InvalidAuth,
"account_inactive" => StartError::AccountInactive,
"invalid_arg_name" => StartError::InvalidArgName,
"invalid_array_arg" => StartError::InvalidArrayArg,
"invalid_charset" => StartError::InvalidCharset,
"invalid_form_data" => StartError::InvalidFormData,
"invalid_post_type" => StartError::InvalidPostType,
"missing_post_type" => StartError::MissingPostType,
"team_added_to_org" => StartError::TeamAddedToOrg,
"request_timeout" => StartError::RequestTimeout,
_ => StartError::Unknown(s.to_owned()),
}
}
}
impl<E: Error> fmt::Display for StartError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl<E: Error> Error for StartError<E> {
fn description(&self) -> &str {
match self {
&StartError::MigrationInProgress => "migration_in_progress: Team is being migrated between servers. See the team_migration_started event documentation for details.",
&StartError::NotAuthed => "not_authed: No authentication token provided.",
&StartError::InvalidAuth => "invalid_auth: Invalid authentication token.",
&StartError::AccountInactive => "account_inactive: Authentication token is for a deleted user or team.",
&StartError::InvalidArgName => "invalid_arg_name: The method was passed an argument whose name falls outside the bounds of common decency. This includes very long names and names with non-alphanumeric characters other than _. If you get this error, it is typically an indication that you have made a very malformed API call.",
&StartError::InvalidArrayArg => "invalid_array_arg: The method was passed a PHP-style array argument (e.g. with a name like foo[7]). These are never valid with the Slack API.",
&StartError::InvalidCharset => "invalid_charset: The method was called via a POST request, but the charset specified in the Content-Type header was invalid. Valid charset names are: utf-8 iso-8859-1.",
&StartError::InvalidFormData => "invalid_form_data: The method was called via a POST request with Content-Type application/x-www-form-urlencoded or multipart/form-data, but the form data was either missing or syntactically invalid.",
&StartError::InvalidPostType => "invalid_post_type: The method was called via a POST request, but the specified Content-Type was invalid. Valid types are: application/x-www-form-urlencoded multipart/form-data text/plain.",
&StartError::MissingPostType => "missing_post_type: The method was called via a POST request and included a data payload, but the request did not include a Content-Type header.",
&StartError::TeamAddedToOrg => "team_added_to_org: The team associated with your request is currently undergoing migration to an Enterprise Organization. Web API and other platform operations will be intermittently unavailable until the transition is complete.",
&StartError::RequestTimeout => "request_timeout: The method was called via a POST request, but the POST data was either missing or truncated.",
&StartError::MalformedResponse(ref e) => e.description(),
&StartError::Unknown(ref s) => s,
&StartError::Client(ref inner) => inner.description(),
}
}
fn cause(&self) -> Option<&Error> {
match self {
&StartError::MalformedResponse(ref e) => Some(e),
&StartError::Client(ref inner) => Some(inner),
_ => None,
}
}
}