-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.py
211 lines (159 loc) · 6.01 KB
/
program.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from dotenv import load_dotenv
import os
import requests
import json
import websocket
import yaml
import apprise
load_dotenv()
host = os.environ["GOTIFY_HOST"]
token = os.environ["GOTIFY_TOKEN"]
configPath = os.environ["CONF_FILE"] if "CONF_FILE" in os.environ else '/etc/gotify2apprise/config.yaml'
defaultTitleTemplate = os.environ["TITLE_TEMPLATE"] if "TITLE_TEMPLATE" in os.environ else "$title"
defaultMessageTemplate = os.environ["MESSAGE_TEMPLATE"] if "MESSAGE_TEMPLATE" in os.environ else "$message"
with open(configPath, 'r') as f:
configData = yaml.safe_load(f)
def getGotifyApps():
headers = { 'X-Gotify-Key': str(token) }
try:
response = requests.get("http://" + str(host) + "/application", headers=headers)
if response.status_code != 200:
return {}
result = {}
for app in response.json():
result[app["id"]] = app
return result
except Exception as e:
print(e)
def isCorrectPriority(priority, receiver):
if "priorities" in receiver:
prioritiesList = []
for priorityValue in receiver["priorities"]:
if isinstance(priorityValue, int):
prioritiesList.append(priority)
if isinstance(priorityValue, str):
match priorityValue:
case "info":
prioritiesList += [0, 1, 2, 3]
case "warn":
prioritiesList += [4, 5, 6, 7]
case "crit":
prioritiesList += [8, 9, 10]
if not priority in prioritiesList:
return False
if "minPriority" in receiver:
minPriority = receiver["minPriority"]
minPriorityValue = 0
if isinstance(minPriority, int):
minPriorityValue = minPriority
if isinstance(minPriority, str):
match minPriority:
case "info":
minPriorityValue = 0
case "warn":
minPriorityValue = 4
case "crit":
minPriorityValue = 8
if minPriorityValue > priority:
return False
return True
def getReceivers(appId, priority):
apps = getGotifyApps()
if not appId in apps:
return []
app = apps[appId]
if not "applications" in configData:
return []
result = []
for confApp in configData["applications"]:
if not "tokens" in confApp or not "receivers" in confApp:
continue
tokens = confApp["tokens"]
if "all" in tokens or app["token"] in tokens:
receivers = confApp["receivers"]
for receiver in receivers:
try:
if isCorrectPriority(priority, receiver) and "urls" in receiver:
result.append(receiver)
except Exception as e:
print(e)
return result
def getPriorityString(priority):
if priority < 4:
return "info"
if priority < 8:
return "warn"
return "crit"
def getTemplateText(msgData, receiver, template):
result = template
priority = msgData["priority"]
appId = msgData["appid"]
title = msgData['title']
message = msgData['message']
priorityStr = getPriorityString(priority)
result = result.replace("$priorityStr", priorityStr)
result = result.replace("$priority", str(priority))
result = result.replace("$appid", str(appId))
result = result.replace("$title", title)
result = result.replace("$message", message)
return result
def getTitle(msgData, receiver):
template = defaultTitleTemplate
if "titleTemplate" in receiver:
try:
template = receiver["titleTemplate"]
except Exception as e:
print(e)
return getTemplateText(msgData, receiver, template)
def getMessage(msgData, receiver):
template = defaultMessageTemplate
if "messageTemplate" in receiver:
try:
template = receiver["messageTemplate"]
except Exception as e:
print(e)
return getTemplateText(msgData, receiver, template)
def getNotifyType(priority):
type = apprise.NotifyType.INFO
if priority >= 4 and priority < 8:
type = apprise.NotifyType.WARNING
elif priority >= 8:
type = apprise.NotifyType.FAILURE
return type
def onNotify(ws, msg):
print("msg: ", msg)
try:
msgData = json.loads(msg)
appId = msgData["appid"]
priority = msgData["priority"]
receivers = getReceivers(appId, priority)
if len(receivers) <= 0:
print("No receivers found for this message, skip")
return
for receiver in receivers:
for url in receiver["urls"]:
try:
appriseInstance = apprise.Apprise()
appriseInstance.add(url)
appriseInstance.notify(title=getTitle(msgData, receiver),
body=getMessage(msgData, receiver),
notify_type=getNotifyType(priority))
except Exception as e:
print(e)
except Exception as e:
print(e)
def onError(ws, err):
print(err)
def onClose(ws, code, msg):
print("Connection closed with message '" + str(msg) + "' and code " + str(code))
def onOpen(ws):
print("Gotify websocket connected")
applications = getGotifyApps()
if __name__ == "__main__":
print("Gotify To Apprise start...")
wsApp = websocket.WebSocketApp("ws://" + str(host) + "/stream", header={"X-Gotify-Key": str(token)},
on_open=onOpen,
on_message=onNotify,
on_error=onError,
on_close=onClose)
wsApp.run_forever()