diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..a58ca7f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +config.yaml +env +venv diff --git a/Dockerfile b/Dockerfile index db91578..faaeacc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ # To build the image, run `docker build` command from the root of the # repository: # -# docker build -f docker/Dockerfile . +# docker build -f Dockerfile . # ## diff --git a/README.md b/README.md index 043d2d2..1ae6f57 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # ntfy-to-matrix-forwarder Forwards messages received in a ntfy topic to a matrix room. Uses [matrix-nio](https://github.com/poljar/matrix-nio), but does not support E2EE. +Can only forward text from ntfy, does not support attachments. diff --git a/config.yaml.example b/config.yaml.example index 3a39e1c..f91548c 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -1,6 +1,10 @@ -homeserver: https://matrix.org +homeserver: "https://matrix.org" username: "@user:matrix.org" -password: hunter2 +password: "hunter2" matrix_room: "!matrix_room_address:matrix.org" -ntfy_server: ntfy.sh -ntfy_topic: disk-alerts + +ntfy_server: "ntfy.sh" +ntfy_topic: "disk-alerts" + +markdown_forman: True +logging_level: INFO diff --git a/main.py b/main.py index 7ceedd1..07d4e23 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import logging import asyncio import yaml from nio import AsyncClient @@ -8,6 +9,8 @@ with open("config.yaml", "r") as yamlfile: config = yaml.load(yamlfile, Loader=yaml.Loader) + +logging.basicConfig(level=config["logging_level"]) async def send_message(message): @@ -16,19 +19,26 @@ async def send_message(message): await client.login(config["password"]) await client.sync(timeout=15) - # Replace two or more spaces with return carriage - message = re.sub(r"[ ]{2,}", "\r\r", message) + # Format message as markdown if configured to do so + if config["markdown_format"]: + # Replace two or more spaces with return carriage + message = re.sub(r"[ ]{2,}", "\r\r", message) - content = { - "msgtype": "m.text", - "format": "org.matrix.custom.html", - "body": message, - } + content = { + "msgtype": "m.text", + "format": "org.matrix.custom.html", + "body": message, + } - # Convert message content to markdown - content["formatted_body"] = markdown(message) + # Convert message content to markdown + content["formatted_body"] = markdown(message) + else: + content = { + "msgtype": "m.text", + "body": message, + } - # Send formatted message + # Send message to the matrix room try: await client.room_send( room_id=config["matrix_room"], @@ -36,8 +46,10 @@ async def send_message(message): content=content, ignore_unverified_devices=True, ) - except SendRetryError: - logger.exception(f"Unable to send message response to {room_id}") + logging.info("Message sent successfully.") + except Exception as e: + logging.error(f"Exception while attempting to send message: {e}") + logging.info(f"Matrix message content was: {content}") await client.close() return @@ -47,10 +59,12 @@ async def main(): ntfy_server = config["ntfy_server"] ntfy_topic = config["ntfy_topic"] resp = requests.get(f"https://{ntfy_server}/{ntfy_topic}/raw", stream=True) + logging.info(f"Listening to ntfy topic: {ntfy_topic}") + for line in resp.iter_lines(): if line: - print(line.decode("utf-8")) - print("Sending message to matrix room") + logging.info(f"Received message from ntfy: {line.decode('utf-8')}") + logging.info("Sending message to matrix room...") await send_message(line.decode("utf-8"))