~jae/gh-bot

a54fd1f191fff72b75ff78b091e407557dbdc068 — Jae Lo Presti (DN0) 2 years ago 931edff
Matrix connector: allow selection of protocol via ENV variable
2 files changed, 8 insertions(+), 6 deletions(-)

M utils/message.py
M utils/roomutil.py
M utils/message.py => utils/message.py +5 -4
@@ 8,6 8,7 @@ import markdown
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'):


@@ 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}')


@@ 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}')


@@ 47,7 48,7 @@ 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}')


@@ 55,7 56,7 @@ def send_to_matrix(message=None):

    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}')

M utils/roomutil.py => utils/roomutil.py +3 -2
@@ 10,6 10,7 @@ room_log = logging.getLogger('roomutil')
# 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():


@@ 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')


@@ 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}')