-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_to_matrix.py
63 lines (55 loc) · 1.45 KB
/
send_to_matrix.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
#!/usr/bin/env python3
from matrix_client.client import MatrixClient
import sys
import logging
import json
import importlib
importlib.reload(sys)
from dotenv import load_dotenv
import os
load_dotenv()
## Variables from .env
user = os.environ.get('user')
password = os.environ.get('password')
server = os.environ.get('server')
room = os.environ.get('room')
## Matrix connection handler
client = MatrixClient(server)
token = client.login(username=user, password=password)
room = client.join_room(room)
## File handling
alert_stdin = ""
for line in sys.stdin:
alert_stdin += line
alert_json = json.loads(alert_stdin)
## Dictionary of json objects
dict_list = {
"Message":"alert_json['message']", \
"Remediation":"alert_json['remediation']",
}
## Check dict agains json values
def dict_request(value):
try:
data = eval(str(value))
return data;
except KeyError:
return()
### Build html message
html = "<html><head></head><body>"
for key,value in dict_list.items():
content = dict_request(value)
if content:
if key == 'Remediation':
content = str("⚠"+content+"<br>")
html=html+content
else:
content = str(str(content)+"<br>")
html=html+content
html=html+"</body></html>"
## Send message
room.send_html(html, msgtype="m.notice")
logging.info('Message been sended.')
## CleanUp
dict_list.clear()
logging.info('Cleaning up. Exit.')
exit()