From ced6190b55f10c8c828807dc2eeba694cf7cb631 Mon Sep 17 00:00:00 2001 From: "jokob.sk" Date: Wed, 17 Aug 2022 13:39:06 +1000 Subject: [PATCH] Discord compatible webhooks + bugfix of redirect after logon --- back/pialert.py | 26 +++++++++++++++++++++----- config/pialert.conf | 4 ++++ front/help_faq.php | 2 +- front/index.php | 9 +-------- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index 2c850226..b318b6ad 100644 --- a/back/pialert.py +++ b/back/pialert.py @@ -1447,7 +1447,7 @@ def email_reporting (): print (' Skip Apprise...') if REPORT_WEBHOOK : print (' Sending report by webhook...') - send_webhook (json_final) + send_webhook (json_final, mail_text) else : print (' Skip webhook...') if REPORT_NTFY : @@ -1600,7 +1600,20 @@ def SafeParseGlobalBool(boolVariable): return False #------------------------------------------------------------------------------- -def send_webhook (_json): +def send_webhook (_json, _html): + + # payload type + try: + webhookPayload = WEBHOOK_PAYLOAD + except NameError: # variable not defined, use a default + webhookPayload = 'json' + + # use data type based on specified payload type + if webhookPayload == 'json': + payloadData = _json + if webhookPayload == 'html': + payloadData = _html + #Define slack-compatible payload _json_payload={ "username": "Pi.Alert", @@ -1608,17 +1621,20 @@ def send_webhook (_json): "attachments": [{ "title": "Pi.Alert Notifications", "title_link": REPORT_DASHBOARD_URL, - "text": _json + "text": payloadData }] - } + } # Using the Slack-Compatible Webhook endpoint for Discord so that the same payload can be used for both if(WEBHOOK_URL.startswith('https://discord.com/api/webhooks/') and not WEBHOOK_URL.endswith("/slack")): _WEBHOOK_URL = f"{WEBHOOK_URL}/slack" + curlParams = ["curl","-i","-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), _WEBHOOK_URL] else: _WEBHOOK_URL = WEBHOOK_URL + curlParams = ["curl","-i","-X", "GET" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), _WEBHOOK_URL] - p = subprocess.Popen(["curl","-i","-X", "GET" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), _WEBHOOK_URL], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + # execute CURL call + p = subprocess.Popen(curlParams, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = p.communicate() diff --git a/config/pialert.conf b/config/pialert.conf index db9b22eb..0f997090 100644 --- a/config/pialert.conf +++ b/config/pialert.conf @@ -35,6 +35,10 @@ 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' +# e.g.: for discord use 'html' +WEBHOOK_PAYLOAD = 'json' # Apprise settings #----------------------- diff --git a/front/help_faq.php b/front/help_faq.php index 543e1218..d1e50626 100644 --- a/front/help_faq.php +++ b/front/help_faq.php @@ -3,7 +3,7 @@ session_start(); if ($_SESSION["login"] != 1) { - header('Location: /pialert/index.php'); + header('Location: index.php'); exit; } diff --git a/front/index.php b/front/index.php index 80f7dd3a..2d7fa911 100644 --- a/front/index.php +++ b/front/index.php @@ -77,13 +77,6 @@ if (($_SESSION["login"] == 1) || ($Pia_Password == $_COOKIE["PiAler_SaveLogin"]) if (isset($_POST['PWRemember'])) {setcookie("PiAler_SaveLogin", hash('sha256',$_POST["loginpassword"]), time()+604800);} } -// active Session or valid cookie (cookie not extends) -if (($_SESSION["login"] == 1) || ($Pia_Password == $_COOKIE["PiAler_SaveLogin"])) - { - header('Location: /pialert/devices.php'); - $_SESSION["login"] = 1; - } - // no active session, cookie not checked if ($_SESSION["login"] != 1) { @@ -181,7 +174,7 @@ if ($ENABLED_DARKMODE === True) {
- +


./reset_password.sh