-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrocket.py
More file actions
462 lines (376 loc) · 15.2 KB
/
rocket.py
File metadata and controls
462 lines (376 loc) · 15.2 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst
"""This library collects some Rocket.Chat related functions that are
needed by more than one Rocket.Chat plugin.
Typical use-case:
```python
credentials = lib.base.coe(lib.rocket.get_token(
args.URL,
args.USERNAME,
args.PASSWORD,
insecure=args.INSECURE,
no_proxy=args.NO_PROXY,
timeout=args.TIMEOUT,
))
auth_token, user_id = credentials.split(':')
result = lib.base.coe(lib.rocket.get_stats(
args.URL,
auth_token,
user_id,
insecure=args.INSECURE,
no_proxy=args.NO_PROXY,
timeout=args.TIMEOUT,
))
```
"""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2025070901'
from . import url
def _flatten_params(params):
"""
Recursively flatten a dictionary of parameters into a URL-style query string.
This helper function takes a dictionary where values may be primitives (e.g., strings, numbers)
or nested dictionaries, and converts it into a single query string. Nested dictionaries are
rendered inside braces `{}` with their own `key=value` pairs joined by `&`.
### Parameters
- **params** (`dict`): Mapping of parameter names to values. Values can be:
- Primitive types (`str`, `int`, etc.), rendered as `key=value`.
- Nested `dict`, rendered as `key={inner_key1=inner_val1&inner_key2=inner_val2}`.
### Returns
- **str**: A query string with `&`-separated `key=value` pairs. Nested dicts are enclosed in
`{}`.
### Example
>>> params = {
... 'key1': 'value1',
... 'key2': 'value2',
... 'key3': {
... 'subkey1': 'subvalue1',
... 'subkey2': 'subvalue2',
... }
... }
>>> _flatten_params(params)
'key1=value1&key2=value2&key3={subkey1=subvalue1&subkey2=subvalue2}'
"""
parts = []
for key, val in params.items():
if isinstance(val, dict):
inner = _flatten_params(val)
parts.append(f"{key}={{{inner}}}")
else:
parts.append(f"{key}={val}")
return '&'.join(parts)
def get_groups_history(
rc_url, auth_token, user_id,
room_id=None, params={},
insecure=False, no_proxy=False, timeout=3
):
"""
Retrieve message history for a private group via Rocket.Chat's `groups.history` API.
This function constructs the correct endpoint URL for `groups.history`, injects the required
`roomId` into the query parameters along with any additional options, attaches the
authentication headers (`X-Auth-Token` and `X-User-Id`), and performs a GET request to fetch
the message history.
Equivalent to:
```bash
curl -H "X-Auth-Token: <auth_token>" \
-H "X-User-Id: <user_id>" \
"https://chat.example.com/api/v1/groups.history?roomId=<roomId>&count=20&offset=0"
```
### Parameters
- **rc_url** (`str`): Rocket.Chat base URL or full endpoint URL. If it does not already end with
`/groups.history`, any trailing slashes will be stripped and `/groups.history` appended.
- **auth_token** (`str`): Authentication token from login (for the `X-Auth-Token` header).
- **user_id** (`str`): User ID from login (for the `X-User-Id` header).
- **room_id** (`str`): ID of the private group whose history you want to fetch. Required.
- **params** (`dict`, optional): Additional query parameters for pagination and date filtering,
such as:
- `count` (`int`): Number of messages to return.
- `offset` (`int`): Number of messages to skip.
- `oldest` (`str`): ISO8601 timestamp for the earliest message.
- `latest` (`str`): ISO8601 timestamp for the latest message.
Defaults to `{}`.
- **insecure** (`bool`, optional): Allow insecure SSL connections. Defaults to `False`.
- **no_proxy** (`bool`, optional): Bypass proxy settings. Defaults to `False`.
- **timeout** (`int`, optional): Request timeout in seconds. Defaults to `3`.
### Returns
- **tuple** (`bool`, `dict` or `str`):
- On success: `(True, response_json)` where `response_json` contains the history payload.
- On failure: `(False, 'Error getting groups.history: <error message>')`.
### Example
>>> success, history = get_groups_history(
... 'https://chat.example.com',
... 'authTokenHere',
... 'userIdHere',
... room_id='ABC123',
... params={'count': 50, 'offset': 0}
... )
>>> if success:
... for msg in history.get('messages', []):
... print(msg['u']['username'], msg['msg'])
"""
if not rc_url.endswith('/groups.history'):
rc_url = rc_url.rstrip('/') + '/groups.history'
params.update({'roomId': room_id}) # add the required room_id to the parameter list
query = _flatten_params(params)
rc_url = rc_url + f'{"?" + query if query else ""}'
headers = {
'X-Auth-Token': auth_token,
'X-User-Id': user_id,
}
success, result = url.fetch_json(
rc_url,
header=headers,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success or not result:
return False, f'Error getting groups.history: {result}'
return True, result
def get_rooms_get(rc_url, auth_token, user_id, insecure=False, no_proxy=False, timeout=3):
"""
Retrieve the list of chat rooms accessible to the authenticated user via Rocket.Chat's
`rooms.get` API.
This function constructs the correct endpoint URL for `rooms.get`, attaches the required
authentication headers (`X-Auth-Token` and `X-User-Id`), and performs a GET request to fetch
metadata about all chat rooms that the user can access.
Equivalent to:
```bash
curl -H "X-Auth-Token: <auth_token>" \
-H "X-User-Id: <user_id>" \
https://chat.example.com/api/v1/rooms.get
```
### Parameters
- **rc_url** (`str`): Rocket.Chat base URL or full endpoint URL. If it does not already end with
`/rooms.get`, any trailing slashes will be stripped and `/rooms.get` appended.
- **auth_token** (`str`): Authentication token obtained from `login` (for the `X-Auth-Token`
header).
- **user_id** (`str`): User ID obtained from `login` (for the `X-User-Id` header).
- **insecure** (`bool`, optional): Allow insecure SSL connections. Defaults to `False`.
- **no_proxy** (`bool`, optional): Bypass proxy settings. Defaults to `False`.
- **timeout** (`int`, optional): Request timeout in seconds. Defaults to `3`.
### Returns
- **tuple** (`bool`, `dict` or `str`):
- On success: `(True, response_json)` where `response_json` is the parsed JSON result from
the API.
- On failure: `(False, 'Error getting rooms.get: <error message>')`.
### Example
>>> success, rooms = get_rooms_get(
... 'https://chat.example.com',
... 'authTokenHere',
... 'userIdHere'
... )
>>> if success:
... print(rooms)
"""
if not rc_url.endswith('/rooms.get'):
rc_url = rc_url.rstrip('/') + '/rooms.get'
headers = {
'X-Auth-Token': auth_token,
'X-User-Id': user_id,
}
success, result = url.fetch_json(
rc_url,
header=headers,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success or not result:
return False, f'Error getting rooms.get: {result}'
return True, result
def get_rooms_info(
rc_url, auth_token, user_id,
room_id=None, room_name=None,
insecure=False, no_proxy=False, timeout=3
):
"""
Retrieve detailed information about a specific Rocket.Chat room via the `rooms.info` API.
This function constructs the correct endpoint URL for `rooms.info`, optionally appending
a `roomId` or `roomName` query parameter, attaches the required authentication headers
(`X-Auth-Token` and `X-User-Id`), and performs a GET request to fetch metadata for the
specified room.
Equivalent to:
```bash
curl -H "X-Auth-Token: <auth_token>" \
-H "X-User-Id: <user_id>" \
"https://chat.example.com/api/v1/rooms.info?roomId=<roomId>&roomName=<roomName>"
```
### Parameters
- **rc_url** (`str`): Rocket.Chat base URL or full endpoint URL. If it does not already end with
`/rooms.info`, any trailing slashes will be stripped and `/rooms.info` appended.
- **auth_token** (`str`): Authentication token obtained from login (for the `X-Auth-Token`
header).
- **user_id** (`str`): User ID obtained from login (for the `X-User-Id` header).
- **room_id** (`str`, optional): ID of the room to fetch info for. Defaults to `None`.
- **room_name** (`str`, optional): Name (alias) of the room to fetch info for. Defaults to
`None`.
At least one of `room_id` or `room_name` should be provided.
- **insecure** (`bool`, optional): Allow insecure SSL connections. Defaults to `False`.
- **no_proxy** (`bool`, optional): Bypass proxy settings. Defaults to `False`.
- **timeout** (`int`, optional): Request timeout in seconds. Defaults to `3`.
### Returns
- **tuple** (`bool`, `dict` or `str`):
- On success: `(True, response_json)` where `response_json` is the parsed JSON result from
the API.
- On failure: `(False, 'Error getting rooms.info: <error message>')`.
### Example
>>> success, info = get_rooms_info(
... 'https://chat.example.com',
... 'authTokenHere',
... 'userIdHere',
... room_id='GENERAL'
... )
>>> if success:
... print(info)
"""
if not rc_url.endswith('/rooms.info'):
rc_url = rc_url.rstrip('/') + '/rooms.info'
params = {
'roomId': room_id,
'roomName': room_name,
}
query = _flatten_params(params)
rc_url = rc_url + f'{"?" + query if query else ""}'
headers = {
'X-Auth-Token': auth_token,
'X-User-Id': user_id,
}
success, result = url.fetch_json(
rc_url,
header=headers,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success or not result:
return False, f'Error getting rooms.info: {result}'
return True, result
def get_stats(rc_url, auth_token, user_id, insecure=False, no_proxy=False, timeout=3):
"""
Retrieve Rocket.Chat statistics using an API token.
This function calls the `api/v1/statistics` endpoint to retrieve server stats after
authentication.
Equivalent to:
```bash
# https://rocket.chat/docs/developer-guides/rest-api/miscellaneous/statistics/
curl -H "X-Auth-Token: 8h2mKAwxB3AQrFSjLVKMooJyjdCFaA7W45sWlHP8IzO"
-H "X-User-Id: ew28DpvKw3R"
http://localhost:3000/api/v1/statistics
```
### Parameters
- **rc_url** (`str`): Rocket.Chat base URL.
- **auth_token** (`str`): Authentication token.
- **user_id** (`str`): User ID linked to the token.
- **insecure** (`bool`, optional): Allow insecure SSL. Defaults to `False`.
- **no_proxy** (`bool`, optional): Ignore proxy. Defaults to `False`.
- **timeout** (`int`, optional): Timeout in seconds. Defaults to `3`.
### Returns
- **tuple** (`bool`, `dict`): Success flag and stats data or error message.
### Example
>>> get_stats('https://chat.example.com', auth_token, user_id)
(True, {...})
"""
if not rc_url.endswith('/statistics'):
rc_url = rc_url.rstrip('/') + '/statistics'
headers = {
'X-Auth-Token': auth_token,
'X-User-Id': user_id,
}
success, result = url.fetch_json(
rc_url,
header=headers,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success or not result:
return False, f'Error getting statistics: {result}'
return True, result
def get_token(rc_url, user, password, insecure=False, no_proxy=False, timeout=3):
"""
Retrieve an API token from Rocket.Chat using user credentials.
This function authenticates against a Rocket.Chat instance and retrieves an `authToken` and
`userId` for future authenticated API calls.
Equivalent to:
```bash
curl -X "POST"
-d "user=admin&password=mypassword"
http://localhost:3000/api/v1/login
```
### Parameters
- **rc_url** (`str`): Rocket.Chat base URL.
- **user** (`str`): Username.
- **password** (`str`): Password.
- **insecure** (`bool`, optional): Allow insecure SSL. Defaults to `False`.
- **no_proxy** (`bool`, optional): Ignore proxy. Defaults to `False`.
- **timeout** (`int`, optional): Timeout in seconds. Defaults to `3`.
### Returns
- **tuple** (`bool`, `str`): Success flag and result string or error.
### Example
>>> get_token('https://chat.example.com', 'admin', 'mypassword')
(True, 'authToken:userId')
"""
if not rc_url.endswith('/login'):
rc_url = rc_url.rstrip('/') + '/login'
data = {'user': user, 'password': password}
success, result = url.fetch_json(
rc_url,
data=data,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success or not result:
return False, f'Error getting token: {result}'
data = result.get('data', {})
auth_token = data.get('authToken')
user_id = data.get('userId')
if not auth_token or not user_id:
return False, 'Authentication failed or user unauthorized.'
return True, f'{auth_token}:{user_id}'
def send2webhook(rc_url, webhook, data, insecure=False, no_proxy=False, timeout=3):
"""
Send a JSON payload to a Rocket.Chat incoming webhook.
This function posts `data` to a Rocket.Chat incoming webhook endpoint, constructing the full URL
from the provided `rc_url` and `webhook` ID or token.
Equivalent to:
```bash
curl -X POST \
-H 'Content-type: application/json' \
-d '{"text":"Hello"}' \
https://chat.example.com/hooks/<webhook>
```
### Parameters
- **rc_url** (`str`): Rocket.Chat base URL. May include `/api/v1`; if so, it will be stripped.
- **webhook** (`str`): Incoming webhook identifier or token (e.g. `CWaA.../Zbpj...`).
- **data** (`dict`): JSON-serializable payload to send (e.g., `{'text': 'message'}`).
- **insecure** (`bool`, optional): Allow insecure SSL connections. Defaults to `False`.
- **no_proxy** (`bool`, optional): Bypass any proxy settings. Defaults to `False`.
- **timeout** (`int`, optional): Request timeout in seconds. Defaults to `3`.
### Returns
- **tuple** (`bool`, `bool` or `str`):
- On success: `(True, True)`.
- On failure: `(False, 'Error: <error message>')`.
### Example
>>> data = { 'text': '\\n'.join(['bitte beantworten:'] + output) }
>>> send2webhook('https://chat.example.com/api/v1', 'CWaA.../Zbpj...', data)
(True, True)
"""
rc_url = rc_url.replace('/api/v1', '').rstrip('/') + '/hooks/' + webhook
success, result = url.fetch_json(
rc_url,
data=data,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success or not result:
return False, f'Error: {result}'
return True, True