From 94365d2cc9efd761d4f63a9879af5654a4fdee6e Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Tue, 31 Jan 2023 22:08:35 +1100 Subject: [PATCH] Apprise payload type setting --- back/pialert.py | 48 +++++++++++++++----------- docker-compose.yml | 6 ++-- front/php/templates/language/en_us.php | 6 ++-- front/settings.php | 2 +- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/back/pialert.py b/back/pialert.py index 7fcbf133..5a86bc2a 100755 --- a/back/pialert.py +++ b/back/pialert.py @@ -275,7 +275,7 @@ def importConfig (): # Webhooks global REPORT_WEBHOOK, WEBHOOK_URL, WEBHOOK_PAYLOAD, WEBHOOK_REQUEST_METHOD # Apprise - global REPORT_APPRISE, APPRISE_HOST, APPRISE_URL + global REPORT_APPRISE, APPRISE_HOST, APPRISE_URL, APPRISE_PAYLOAD # NTFY global REPORT_NTFY, NTFY_HOST, NTFY_TOPIC, NTFY_USER, NTFY_PASSWORD # PUSHSAFER @@ -342,6 +342,7 @@ def importConfig (): REPORT_APPRISE = ccd('REPORT_APPRISE', False , c_d, 'Enable Apprise', 'boolean', '', 'Apprise', ['test']) APPRISE_HOST = ccd('APPRISE_HOST', '' , c_d, 'Apprise host URL', 'text', '', 'Apprise') APPRISE_URL = ccd('APPRISE_URL', '' , c_d, 'Apprise notification URL', 'text', '', 'Apprise') + APPRISE_PAYLOAD = ccd('APPRISE_PAYLOAD', 'html' , c_d, 'Payload type', 'selecttext', "['html', 'text']", 'Apprise') # NTFY REPORT_NTFY = ccd('REPORT_NTFY', False , c_d, 'Enable NTFY', 'boolean', '', 'NTFY', ['test']) @@ -451,7 +452,7 @@ last_internet_IP_scan = now_minus_24h last_API_update = now_minus_24h last_run = now_minus_24h last_cleanup = now_minus_24h -last_update_vendors = time_started - datetime.timedelta(days = 6) # update vendors 24h after first run and than once a week +last_update_vendors = time_started - datetime.timedelta(days = 6) # update vendors 24h after first run and then once a week # indicates, if a new version is available newVersionAvailable = False @@ -2138,10 +2139,11 @@ def send_notifications (): deviceUrl = REPORT_DASHBOARD_URL + '/deviceDetails.php?mac=' table_attributes = {"style" : "border-collapse: collapse; font-size: 12px; color:#70707", "width" : "100%", "cellspacing" : 0, "cellpadding" : "3px", "bordercolor" : "#C0C0C0", "border":"1"} - headerProps = "width='120px' style='color:blue; font-size: 12px;' bgcolor='#909090' " + headerProps = "width='120px' style='color:blue; font-size: 16px;' bgcolor='#909090' " thProps = "width='120px' style='color:#F0F0F0' bgcolor='#909090' " build_direction = "TOP_TO_BOTTOM" + text_line = '{}\t\t{}\n' # Reporting section file_print(' Check if something to report') @@ -2205,11 +2207,10 @@ def send_notifications (): headers = ["MAC", "Datetime", "IP", "Event Type", "Additional info"] # prepare text-only message - text_line = '{}\t{}\n' - for device in json_string["data"]: for header in headers: text += text_line.format ( header + ': ', device[header]) + text += '\n' # Format HTML table headers for header in headers: @@ -2238,12 +2239,12 @@ def send_notifications (): headers = ["MAC", "Datetime", "IP", "Event Type", "Device name", "Comments"] - # prepare text-only message - text_line = '{}\t{}\n' + # prepare text-only message text = "" for device in json_string["data"]: for header in headers: - text += text_line.format ( header + ': ', device[header]) + text += text_line.format ( header + ': ', device[header]) + text += '\n' # Format HTML table headers for header in headers: @@ -2273,12 +2274,12 @@ def send_notifications (): headers = ["MAC", "Datetime", "IP", "Event Type", "Device name", "Comments"] - # prepare text-only message - text_line = '{}\t{}\n' + # prepare text-only message text = "" for device in json_string["data"]: for header in headers: - text += text_line.format ( header + ': ', device[header]) + text += text_line.format ( header + ': ', device[header]) + text += '\n' # Format HTML table headers for header in headers: @@ -2308,12 +2309,12 @@ def send_notifications (): headers = ["MAC", "Datetime", "IP", "Event Type", "Device name", "Comments"] - # prepare text-only message - text_line = '{}\t{}\n' + # prepare text-only message text = "" for device in json_string["data"]: for header in headers: - text += text_line.format ( header + ': ', device[header]) + text += text_line.format ( header + ': ', device[header]) + text += '\n' # Format HTML table headers for header in headers: @@ -2375,7 +2376,7 @@ def send_notifications (): if REPORT_APPRISE and check_config('apprise'): updateState("Send: Apprise") file_print(' Sending report by Apprise') - send_apprise (mail_html) + send_apprise (mail_html, mail_text) else : file_print(' Skip Apprise') if REPORT_WEBHOOK and check_config('webhook'): @@ -2679,17 +2680,22 @@ def send_webhook (_json, _html): file_print(e.output) #------------------------------------------------------------------------------- -def send_apprise (html): +def send_apprise (html, text): #Define Apprise compatible payload (https://github.com/caronc/apprise-api#stateless-solution) + payload = html + + if APPRISE_PAYLOAD == 'text': + payload = text + _json_payload={ "urls": APPRISE_URL, - "title": "Pi.Alert Notifications", - "format": "html", - "body": html + "title": "Pi.Alert Notifications", + "format": APPRISE_PAYLOAD, + "body": payload } try: - # try runnning a subprocess + # try runnning a subprocess p = subprocess.Popen(["curl","-i","-X", "POST" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), APPRISE_HOST], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) stdout, stderr = p.communicate() # write stdout and stderr into .log files for debugging if needed @@ -3379,7 +3385,7 @@ def handle_test(testType): if testType == 'REPORT_WEBHOOK': send_webhook (sample_json_payload, sample_txt) if testType == 'REPORT_APPRISE': - send_apprise (sample_html) + send_apprise (sample_html, sample_txt) if testType == 'REPORT_NTFY': send_ntfy (sample_txt) if testType == 'REPORT_PUSHSAFER': diff --git a/docker-compose.yml b/docker-compose.yml index e60008d8..db6d71f4 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,13 +9,11 @@ services: volumes: - ${APP_DATA_LOCATION}/pialert/config:/home/pi/pialert/config # - ${APP_DATA_LOCATION}/pialert/db/pialert.db:/home/pi/pialert/db/pialert.db - - ${APP_DATA_LOCATION}/pialert/db2:/home/pi/pialert/db + - ${APP_DATA_LOCATION}/pialert/db:/home/pi/pialert/db # (optional) useful for debugging if you have issues setting up the container - ${LOGS_LOCATION}:/home/pi/pialert/front/log # DELETE START anyone trying to use this file: comment out / delete BELOW lines, they are only for development purposes - - ${DEV_LOCATION}/back/pialert.py:/home/pi/pialert/back/pialert.py - - ${DEV_LOCATION}/back/update_vendors.sh:/home/pi/pialert/back/update_vendors.sh - - ${DEV_LOCATION}/back/report_template_new_version.html:/home/pi/pialert/back/report_template_new_version.html + - ${DEV_LOCATION}/back:/home/pi/pialert/back - ${DEV_LOCATION}/pholus:/home/pi/pialert/pholus - ${DEV_LOCATION}/dockerfiles:/home/pi/pialert/dockerfiles - ${APP_DATA_LOCATION}/pialert/php.ini:/etc/php/7.4/fpm/php.ini diff --git a/front/php/templates/language/en_us.php b/front/php/templates/language/en_us.php index 139c6371..ec00de8b 100755 --- a/front/php/templates/language/en_us.php +++ b/front/php/templates/language/en_us.php @@ -175,7 +175,7 @@ $lang['en_us'] = array( 'DevDetail_MainInfo_Owner' => 'Owner', 'DevDetail_MainInfo_Type' => 'Type', 'DevDetail_Icon' => 'Icon', -'DevDetail_Icon_Descr' => 'Enter a font awesome icon name without the fa- prefix.', +'DevDetail_Icon_Descr' => 'Enter a font awesome icon name without the fa- prefix or with complete class, e.g.: fa fa-brands fa-apple.', 'DevDetail_MainInfo_Vendor' => 'Vendor', 'DevDetail_MainInfo_Favorite' => 'Favorite', 'DevDetail_MainInfo_Group' => 'Group', @@ -555,7 +555,7 @@ the arp-scan will take hours to complete instead of seconds. 'APPRISE_HOST_name' => 'Apprise host URL', 'APPRISE_HOST_description' => 'Apprise host URL starting with http:// or https://. (don\'t forget to include /notify at the end)', 'APPRISE_URL_name' => 'Apprise notification URL', -'APPRISE_URL_description' => 'Apprise notification target URL.', +'APPRISE_URL_description' => 'Apprise notification target URL. For example for Telegram it would be tgram://{bot_token}/{chat_id}.', // NTFY 'NTFY_settings_group' => ' NTFY', @@ -576,6 +576,8 @@ the arp-scan will take hours to complete instead of seconds. 'REPORT_PUSHSAFER_description' => 'Enable sending notifications via Pushsafer.', 'PUSHSAFER_TOKEN_name' => 'Pushsafer token', 'PUSHSAFER_TOKEN_description' => 'Your secret Pushsafer API key (token).', +'APPRISE_PAYLOAD_name' => 'Payload type', +'APPRISE_PAYLOAD_description' => 'Select the payoad type sent to Apprise. For example html works well with emails, text with chat apps, such as Telegram.', // MQTT diff --git a/front/settings.php b/front/settings.php index bd3078d8..fe99a779 100755 --- a/front/settings.php +++ b/front/settings.php @@ -285,7 +285,7 @@ while ($row = $result -> fetchArray (SQLITE3_ASSOC)) {