Skip to content
Snippets Groups Projects
main.py 1.94 KiB
Newer Older
Guilhem Saurel's avatar
Guilhem Saurel committed
#!/usr/bin/env python3
Guilhem Saurel's avatar
Guilhem Saurel committed
"""
wifi-with-matrix script.
Bridge between https://code.ffdn.org/FFDN/wifi-with-me & a matrix room
"""

import os
Guilhem Saurel's avatar
Guilhem Saurel committed
from http.server import BaseHTTPRequestHandler, HTTPServer

from matrix_client.client import MatrixClient

Guilhem Saurel's avatar
Guilhem Saurel committed
SERVER_ADDRESS = ('', int(os.environ.get('PORT', 4785)))
MATRIX_URL = os.environ.get('MATRIX_URL', 'https://matrix.org')
MATRIX_ID = os.environ.get('MATRIX_ID', 'wwm')
MATRIX_PW = os.environ['MATRIX_PW']
ROOM_ID = os.environ['ROOM_ID']
API_KEY = os.environ['API_KEY']


class WWMBotServer(HTTPServer):
    """
    an HTTPServer that also contain a matrix client
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
Guilhem Saurel's avatar
Guilhem Saurel committed
        client = MatrixClient(MATRIX_URL)
        client.login(username=MATRIX_ID, password=MATRIX_PW)
        self.room = client.get_rooms()[ROOM_ID]
class WWMBotForwarder(BaseHTTPRequestHandler):
Guilhem Saurel's avatar
Guilhem Saurel committed
    """
    Class given to the server, st. it knows what to do with a request.
    This one handles the HTTP request, and forwards it to the matrix room.
    """

    def do_POST(self):
        """
        main method, get a json dict from wifi-with-me, send a message to a matrix room
        """
        length = int(self.headers.get('Content-Length'))
        data = json.loads(self.rfile.read(length).decode())
Guilhem Saurel's avatar
Guilhem Saurel committed
        status = 'I need a json dict with name, url, key'
        if all(key in data for key in ['name', 'url', 'key']):
            status = 'wrong key'
            if data['key'] == API_KEY:
                status = 'OK'
                self.server.room.send_text(f'Nouvelle demande de {data["name"]}: {data["url"]}')

        self.send_response(200 if status == 'OK' else 401)
Guilhem Saurel's avatar
Guilhem Saurel committed
        self.send_header('Content-Type', 'application/json')
        self.end_headers()
Guilhem Saurel's avatar
Guilhem Saurel committed
        self.wfile.write(b"{'status': %a}" % status)
Guilhem Saurel's avatar
Guilhem Saurel committed


if __name__ == '__main__':
Guilhem Saurel's avatar
Guilhem Saurel committed
    print('Wifi-With-Matrix bridge starting…')
    WWMBotServer(SERVER_ADDRESS, WWMBotForwarder).serve_forever()