Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
Matrix connector: allow selection of protocol via ENV variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jae1911 committed Apr 9, 2022
1 parent 931edff commit a54fd1f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 5 additions & 4 deletions utils/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
MATRIX_TOKEN = os.environ.get('MATRIX_TOKEN')
MATRIX_HOMESERVER = os.environ.get('MATRIX_HOMESERVER')
LOG_ALL_EVENTS = os.environ.get('LOG_ALL_EVENTS')
MATRIX_PROTO = os.environ.get('MATRIX_SERVER_PROTO') or "https"

# Method to log events to rooms
def log_event_to_rooms(event=None, webhooktype='generic'):
Expand All @@ -19,7 +20,7 @@ def log_event_to_rooms(event=None, webhooktype='generic'):
'body': event,
}

r = requests.get(f'https://{MATRIX_HOMESERVER}/_matrix/client/v3/joined_rooms?access_token={MATRIX_TOKEN}')
r = requests.get(f'{MATRIX_PROTO}://{MATRIX_HOMESERVER}/_matrix/client/v3/joined_rooms?access_token={MATRIX_TOKEN}')

if r.status_code != 200:
log.error(f'Something bad happened: {r.text}')
Expand All @@ -30,7 +31,7 @@ def log_event_to_rooms(event=None, webhooktype='generic'):

joined_rooms = json.loads(r.text)
for room in joined_rooms.get('joined_rooms'):
msg = f'https://{MATRIX_HOMESERVER}/_matrix/client/r0/rooms/{room}/send/fi.jae.webhooklog?access_token={MATRIX_TOKEN}'
msg = f'{MATRIX_PROTO}://{MATRIX_HOMESERVER}/_matrix/client/r0/rooms/{room}/send/fi.jae.webhooklog?access_token={MATRIX_TOKEN}'
r = requests.post(msg, data=json.dumps(payload))
if r.status_code != 200:
log.error(f'Something bad happened: {r.text}')
Expand All @@ -47,15 +48,15 @@ def send_to_matrix(message=None):
'formatted_body': markdown.markdown(message)
}

r = requests.get(f'https://{MATRIX_HOMESERVER}/_matrix/client/v3/joined_rooms?access_token={MATRIX_TOKEN}')
r = requests.get(f'{MATRIX_PROTO}://{MATRIX_HOMESERVER}/_matrix/client/v3/joined_rooms?access_token={MATRIX_TOKEN}')

if r.status_code != 200:
log.error(f'Something bad happened: {r.text}')
return

joined_rooms = json.loads(r.text)
for room in joined_rooms.get('joined_rooms'):
msg = f'https://{MATRIX_HOMESERVER}/_matrix/client/r0/rooms/{room}/send/m.room.message?access_token={MATRIX_TOKEN}'
msg = f'{MATRIX_PROTO}://{MATRIX_HOMESERVER}/_matrix/client/r0/rooms/{room}/send/m.room.message?access_token={MATRIX_TOKEN}'
r = requests.post(msg, data=json.dumps(payload))
if r.status_code != 200:
log.error(f'Something bad happened: {r.text}')
5 changes: 3 additions & 2 deletions utils/roomutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Env variables
MATRIX_TOKEN = os.environ.get('MATRIX_TOKEN')
MATRIX_HOMESERVER = os.environ.get('MATRIX_HOMESERVER')
MATRIX_PROTO = os.environ.get('MATRIX_SERVER_PROTO') or "https"

# Check rooms for joins
def check_matrix_rooms_for_joins():
Expand All @@ -18,7 +19,7 @@ def check_matrix_rooms_for_joins():
room_log.fatal('NO HOMESERVER ADDRESS NOR TOKEN, CANNOT PROCEED')
return

r = requests.get(f'https://{MATRIX_HOMESERVER}/_matrix/client/v3/sync?access_token={MATRIX_TOKEN}')
r = requests.get(f'{MATRIX_PROTO}://{MATRIX_HOMESERVER}/_matrix/client/v3/sync?access_token={MATRIX_TOKEN}')
if r.status_code == 200:
json_data = json.loads(r.text)
rooms = json_data.get('rooms')
Expand All @@ -28,4 +29,4 @@ def check_matrix_rooms_for_joins():
else:
for room in rooms['invite']:
room_log.info(f'Joining room {room}')
r = requests.post(f'https://{MATRIX_HOMESERVER}/_matrix/client/v3/join/{room}?access_token={MATRIX_TOKEN}')
r = requests.post(f'{MATRIX_PROTO}://{MATRIX_HOMESERVER}/_matrix/client/v3/join/{room}?access_token={MATRIX_TOKEN}')

0 comments on commit a54fd1f

Please sign in to comment.