Skip to content
Snippets Groups Projects
Commit ce6416f5 authored by Christian Boulanger's avatar Christian Boulanger
Browse files

Added Rocket.Chat API experiment

parent efca37bf
No related branches found
No related tags found
No related merge requests found
../.idea
.*
!.gitignore
!.env.dist
\ No newline at end of file
ROCKETCHAT_USER_ID=''
ROCKETCHAT_API_KEY=''
\ No newline at end of file
.*
!.gitignore
!.env.dist
\ No newline at end of file
%% Cell type:code id:initial_id tags:
``` python
import requests
import os
from dotenv import load_dotenv
from json import JSONDecodeError
load_dotenv()
def call_rocket_chat_api(command, **params):
url = f'https://chat.gwdg.de/api/v1/{command}'
headers = {
'X-Auth-Token': os.getenv('ROCKETCHAT_API_KEY'),
'X-User-Id': os.getenv('ROCKETCHAT_USER_ID')
}
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
try:
return response.json()
except JSONDecodeError:
raise RuntimeError(f'Cannot parse response from {response.url}.')
teams = call_rocket_chat_api("teams.list").get('teams')
[(t.get('name'), t.get('_id'), t.get('roomId')) for t in teams]
```
%% Output
[('Note-to-self', '6123583208648d50d46136e7', 'Grib6LkDEh8BjXm5A'),
('SLT-Team', '621f3257dda518ec820fec0f', 'DLX2YQYzmwSefCFSs'),
('Arbeitsaufträge-SHK-Abt-3',
'6231a585396730692a6254af',
'h2F4wES7ALisyjFeS'),
('Machine-Learning-Team', '6243f60f3bb97e25a63651be', 'sibAdkwHQzqJChxH9'),
('mpilhlt-DH', '6246b44404cc8d8ba31517c2', 'yT4DM9pje2Cbnz7w3'),
('reference-extraction-workshops-2023',
'636a209aa7ef138927c64671',
'CXECckP3iJcT7Wzvm'),
('Literaturverwaltung', '642ab2edd4f482c368a7e364', 'ALzasJ7iwLpNzE34B'),
('Arbeitsgruppe-Gleichstellungsbericht',
'6492f49d7821ad51fcbbef97',
'6492f49e7821ad51fcbbef99'),
('mpilhlt-Auer-Doctoral-Students-plus-Coordinator',
'64b50afdf6d0cde3515015c3',
'64b50afdf6d0cde3515015c5'),
('DH-Projekte-Abt-Auer',
'655cd77374c1eb212615898e',
'655cd77374c1eb2126158990')]
%% Cell type:markdown id:894f002bf39fcb04 tags:
%% Cell type:code id:709f6d8c845a0056 tags:
``` python
msgs = call_rocket_chat_api('channels.history', roomId='sibAdkwHQzqJChxH9').get('messages')
```
%% Output
---------------------------------------------------------------------------
HTTPError Traceback (most recent call last)
Cell In[29], line 1
----> 1 msgs = call_rocket_chat_api('channels.history', roomId='6243f60f3bb97e25a63651be').get('messages')
Cell In[27], line 15, in call_rocket_chat_api(command, **params)
10 headers = {
11 'X-Auth-Token': os.getenv('ROCKETCHAT_API_KEY'),
12 'X-User-Id': os.getenv('ROCKETCHAT_USER_ID')
13 }
14 response = requests.get(url, headers=headers, params=params)
---> 15 response.raise_for_status()
16 try:
17 return response.json()
File ~\anaconda3\lib\site-packages\requests\models.py:1021, in Response.raise_for_status(self)
1016 http_error_msg = (
1017 f"{self.status_code} Server Error: {reason} for url: {self.url}"
1018 )
1020 if http_error_msg:
-> 1021 raise HTTPError(http_error_msg, response=self)
HTTPError: 400 Client Error: Bad Request for url: https://chat.gwdg.de/api/v1/channels.history?roomId=6243f60f3bb97e25a63651be
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment