Add a text option for webhooks and different repoting levels
This commit is contained in:
@@ -1614,14 +1614,22 @@ def send_webhook (_json, _html):
|
||||
except NameError: # variable not defined, use a default
|
||||
webhookRequestMethod = 'GET'
|
||||
|
||||
# Event level reporting (int, 0=none, 1=internet only, 2=new devices & 1, 3=down devices & 2, 4= events & 3 i.e. everything)
|
||||
try:
|
||||
eventLevel = EVENT_LEVEL
|
||||
except NameError: # variable not defined, use a default
|
||||
eventLevel = 4 # report on everythin
|
||||
|
||||
# use data type based on specified payload type
|
||||
if webhookPayload == 'json':
|
||||
payloadData = _json
|
||||
if webhookPayload == 'html':
|
||||
payloadData = _html
|
||||
if webhookPayload == 'text':
|
||||
payloadData = to_text(_json, eventLevel)
|
||||
|
||||
#Define slack-compatible payload
|
||||
_json_payload={
|
||||
_json_payload = { "text": payloadData } if webhookPayload == 'text' else {
|
||||
"username": "Pi.Alert",
|
||||
"text": "There are new notifications",
|
||||
"attachments": [{
|
||||
@@ -1816,6 +1824,37 @@ def logResult (stdout, stderr):
|
||||
if stdout != None:
|
||||
append_file_binary (LOG_PATH + '/stdout.log', stdout)
|
||||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
def to_text(_json, eventLevel):
|
||||
payloadData = ""
|
||||
if len(_json['internet']) > 0 and eventLevel > 0:
|
||||
payloadData += "INTERNET\n"
|
||||
for event in _json['internet']:
|
||||
payloadData += event[3] + ' on ' + event[2] + '. ' + event[4] + '. New address:' + event[1] + '\n'
|
||||
|
||||
if len(_json['new_devices']) > 0 and eventLevel > 1:
|
||||
payloadData += "NEW DEVICES:\n"
|
||||
for event in _json['new_devices']:
|
||||
if event[4] is None:
|
||||
event[4] = event[11]
|
||||
payloadData += event[1] + ' - ' + event[4] + '\n'
|
||||
|
||||
if len(_json['down_devices']) > 0 and eventLevel > 2:
|
||||
write_file (LOG_PATH + '/down_devices_example.log', _json['down_devices'])
|
||||
payloadData += 'DOWN DEVICES:\n'
|
||||
for event in _json['down_devices']:
|
||||
if event[4] is None:
|
||||
event[4] = event[11]
|
||||
payloadData += event[1] + ' - ' + event[4] + '\n'
|
||||
|
||||
if len(_json['events']) > 0 and eventLevel > 3:
|
||||
payloadData += "EVENTS:\n"
|
||||
for event in _json['events']:
|
||||
if event[8] != "Internet":
|
||||
payloadData += event[8] + " on " + event[1] + " " + event[3] + " at " + event[2] + "\n"
|
||||
|
||||
return payloadData
|
||||
|
||||
#===============================================================================
|
||||
# BEGIN
|
||||
|
||||
@@ -36,10 +36,11 @@ REPORT_DASHBOARD_URL = 'http://pi.alert/'
|
||||
REPORT_WEBHOOK = False
|
||||
WEBHOOK_URL = 'http://n8n.local:5555/webhook-test/aaaaaaaa-aaaa-aaaa-aaaaa-aaaaaaaaaaaa'
|
||||
# webhook payload data format for the "body > attachements > text" attribute in https://github.com/jokob-sk/Pi.Alert/blob/main/docs/webhook_json_sample.json
|
||||
# supported values: 'json' or 'html'
|
||||
# supported values: 'json' or 'html' or 'text'
|
||||
# e.g.: for discord use 'html'
|
||||
WEBHOOK_PAYLOAD = 'json'
|
||||
WEBHOOK_REQUEST_METHOD = 'GET' # POST, GET...
|
||||
EVENT_LEVEL = 5
|
||||
|
||||
# Apprise settings
|
||||
#-----------------------
|
||||
|
||||
Reference in New Issue
Block a user