Events, CurrentScan, pluginsState, APPRISE work

This commit is contained in:
Jokob-sk
2023-08-13 09:26:30 +10:00
parent 2414498846
commit 11f341366b
6 changed files with 68 additions and 24 deletions

View File

@@ -124,6 +124,7 @@ def importConfigs (db):
conf.APPRISE_HOST = ccd('APPRISE_HOST', '' , c_d, 'Apprise host URL', 'text', '', 'Apprise')
conf.APPRISE_URL = ccd('APPRISE_URL', '' , c_d, 'Apprise notification URL', 'text', '', 'Apprise')
conf.APPRISE_PAYLOAD = ccd('APPRISE_PAYLOAD', 'html' , c_d, 'Payload type', 'text.select', "['html', 'text']", 'Apprise')
conf.APPRISE_SIZE = ccd('APPRISE_SIZE', 1024 , c_d, 'Payload size', 'integer', '', 'Apprise')
# NTFY
conf.REPORT_NTFY = ccd('REPORT_NTFY', False , c_d, 'Enable NTFY', 'boolean', '', 'NTFY', ['test'])

View File

@@ -23,6 +23,7 @@ class plugins_state:
def run_plugin_scripts(db, runType, pluginsState = None):
if pluginsState == None:
mylog('debug', ['[Plugins] pluginsState initialized '])
pluginsState = plugins_state()
# Header
@@ -615,8 +616,6 @@ def process_plugin_events(db, plugin, pluginsState):
db.commitDB()
return pluginsState

View File

@@ -18,34 +18,45 @@ def send(msg: noti_struc):
html = msg.html
text = msg.text
# Define Apprise compatible payload (https://github.com/caronc/apprise-api#stateless-solution)
payload = html
payloadData = ''
if conf.APPRISE_PAYLOAD == 'text':
payload = text
# limit = 1024 * 1024 # 1MB limit (1024 bytes * 1024 bytes = 1MB)
limit = conf.APPRISE_SIZE
# truncate size
if conf.APPRISE_PAYLOAD == 'html':
if len(msg.html) > limit:
payloadData = msg.html[:limit] + " <h1> (text was truncated)</h1>"
else:
payloadData = msg.html
if conf.APPRISE_PAYLOAD == 'text':
if len(msg.text) > limit:
payloadData = msg.text[:limit] + " (text was truncated)"
else:
payloadData = msg.text
# Define Apprise compatible payload (https://github.com/caronc/apprise-api#stateless-solution)
# payload = html
# if conf.APPRISE_PAYLOAD == 'text':
# payload = text
_json_payload = {
"urls": conf.APPRISE_URL,
"title": "Pi.Alert Notifications",
"format": conf.APPRISE_PAYLOAD,
"body": payload
"body": payloadData
}
try:
# Construct the curl command with input from the JSON payload
curl_command = [
"curl", "-i", "-X", "POST", "-H", "Content-Type:application/json",
conf.APPRISE_HOST
]
# Run the curl command with the JSON payload as input
completed_process = subprocess.run(
curl_command, input=json.dumps(_json_payload),
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True
)
# try runnning a subprocess
p = subprocess.Popen(["curl","-i","-X", "POST" ,"-H", "Content-Type:application/json" ,"-d", json.dumps(_json_payload), conf.APPRISE_HOST], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = p.communicate()
# write stdout and stderr into .log files for debugging if needed
logResult (stdout, stderr) # TO-DO should be changed to mylog
# Log the stdout and stderr
logResult(completed_process.stdout, completed_process.stderr) # TO-DO should be changed to mylog
mylog('debug', [stdout, stderr]) # TO-DO should be changed to mylog
except subprocess.CalledProcessError as e:
# An error occurred, handle it
mylog('none', [e.output])