Skip to content

Commit

Permalink
Added markdown toggle and Python logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi0n committed Nov 10, 2022
1 parent b7ebb27 commit d2462f8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
config.yaml
env
venv
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -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 .
#

##
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 8 additions & 4 deletions config.yaml.example
Original file line number Diff line number Diff line change
@@ -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
42 changes: 28 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import asyncio
import yaml
from nio import AsyncClient
Expand All @@ -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):
Expand All @@ -16,28 +19,37 @@ 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"],
message_type="m.room.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
Expand All @@ -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"))


Expand Down

0 comments on commit d2462f8

Please sign in to comment.