-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
96 lines (77 loc) · 2.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import asyncio
import json
import logging
from time import sleep
import requests
import yaml
from markdown import markdown
from nio import AsyncClient
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):
client = AsyncClient(config["homeserver"], config["username"])
await client.login(config["password"])
await client.sync(timeout=15)
# Format message as markdown if configured to do so
if config["markdown_format"]:
# Replace \n with \r for markdown compatibility
message = message.replace("\n", "\r")
content = {
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"body": message,
}
# Convert message content to markdown
content["formatted_body"] = markdown(message)
else:
content = {
"msgtype": "m.text",
"body": 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,
)
logging.info("Message sent successfully.")
except Exception as e:
logging.error(f"Exception while attempting to send message: {e}")
logging.error(f"Matrix message content was: {content}")
await client.close()
return
async def main():
ntfy_server = config["ntfy_server"]
ntfy_topic = config["ntfy_topic"]
resp = requests.get(f"https://{ntfy_server}/{ntfy_topic}/json", stream=True)
logging.info(f"Listening to ntfy topic: {ntfy_topic}")
while True:
try:
for line in resp.iter_lines():
if line:
# Convert to JSON
json_msg = json.loads(line)
if "message" in json_msg:
logging.info(f"Received message from ntfy: {json.dumps(json_msg, indent=4)}")
logging.info("Sending message to matrix room...")
await send_message(json_msg["message"])
else:
logging.debug(json.dumps(json_msg, indent=4))
except Exception as e:
if "Connection broken" in e:
logger.warning("Unable to connect to ntfy server, retrying in 15s...")
# Sleep so we don't bombard the server with requests while it's down
sleep(15)
else:
logging.warning(f"Something happened that I don't know how to handle: {e}")
return False
if __name__ == "__main__":
try:
asyncio.run(
main()
)
except KeyboardInterrupt:
pass