Merge pull request #103 from berkobob/webhook_text

Webhook text & a new INCLUDED_SECTIONS config option
This commit is contained in:
jokob-sk
2022-10-22 08:47:21 +11:00
committed by GitHub
2 changed files with 49 additions and 6 deletions

View File

@@ -54,6 +54,13 @@ def main ():
global log_timestamp
global sql_connection
global sql
global includedSections
# Which sections to include. Include everything by default
try:
includedSections = INCLUDED_SECTIONS
except NameError:
includedSections = ['internet', 'new_devices', 'down_devices', 'events']
# Empty stdout and stderr .log files for debugging if needed
write_file (LOG_PATH + '/stderr.log', '')
@@ -1302,7 +1309,7 @@ def email_reporting ():
for eventAlert in sql :
mail_section_Internet = True
mail_section_Internet = 'internet' in includedSections
# collect "internet" (IP changes) for the webhook json
json_internet = add_json_list (eventAlert, json_internet)
@@ -1334,7 +1341,7 @@ def email_reporting ():
ORDER BY eve_DateTime""")
for eventAlert in sql :
mail_section_new_devices = True
mail_section_new_devices = 'new_devices' in includedSections
# collect "new_devices" for the webhook json
json_new_devices = add_json_list (eventAlert, json_new_devices)
@@ -1365,7 +1372,7 @@ def email_reporting ():
ORDER BY eve_DateTime""")
for eventAlert in sql :
mail_section_devices_down = True
mail_section_devices_down = 'down_devices' in includedSections
# collect "down_devices" for the webhook json
json_down_devices = add_json_list (eventAlert, json_down_devices)
@@ -1398,7 +1405,7 @@ def email_reporting ():
ORDER BY eve_DateTime""")
for eventAlert in sql :
mail_section_events = True
mail_section_events = 'events' in includedSections
# collect "events" for the webhook json
json_events = add_json_list (eventAlert, json_events)
@@ -1619,9 +1626,11 @@ def send_webhook (_json, _html):
payloadData = _json
if webhookPayload == 'html':
payloadData = _html
if webhookPayload == 'text':
payloadData = to_text(_json)
#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 +1825,37 @@ def logResult (stdout, stderr):
if stdout != None:
append_file_binary (LOG_PATH + '/stdout.log', stdout)
#-------------------------------------------------------------------------------
def to_text(_json):
payloadData = ""
if len(_json['internet']) > 0 and 'internet' in includedSections:
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 'new_devices' in includedSections:
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 'down_devices' in includedSections:
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 'events' in includedSections:
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

View File

@@ -15,6 +15,7 @@ PRINT_LOG = False
TIMEZONE = 'Europe/Berlin'
PIALERT_WEB_PROTECTION = False
PIALERT_WEB_PASSWORD = '8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92'
INCLUDED_SECTIONS = ['internet', 'new_devices', 'down_devices', 'events']
# EMAIL settings
# ----------------------
@@ -36,10 +37,12 @@ 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...
# set event level to 0 for no message, 1 for Internet changes only, 2 for new devices and 1, 3 for down devices and 2 and 4 for events and 3. i.e. everything
EVENT_LEVEL = 4
# Apprise settings
#-----------------------