wip: ntfy & smtp

This commit is contained in:
2024-02-16 18:54:42 +01:00
parent c0561634f5
commit 9816036d90
2 changed files with 38 additions and 17 deletions

View File

@@ -1,19 +1,22 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
import argparse
import subprocess import subprocess
import os import os
import requests import requests
from functools import wraps
import smtphelper import smtphelper
import json
HTTP_NOT_FOUND = 404 HTTP_NOT_FOUND = 404
DISPATCH_SERVER = None
AUTH = None AUTH = None
def debug_send(uuid, data, fail_it=False): def debug_send(uuid, data, fail_it=False):
'''Dummy function to print and ack a dispatch for debugging''' '''Dummy function to print and ack a dispatch for debugging'''
print(json.dumps(data, intent=2)) print(json.dumps(data, indent=2))
if fail_it: if fail_it:
report_failed_dispatch(uuid, "Dummy Error for Debugging") report_failed_dispatch(uuid, "Dummy Error for Debugging")
else: else:
@@ -31,19 +34,37 @@ def ntfy_send(dispatch_uuid, user_topic, message, ntfy_push_target, ntfy_user, n
'''Send message via NTFY topic''' '''Send message via NTFY topic'''
try: try:
r = requests.post(ntfy_push_target, auth=(ntfy_user, ntfy_pass) , json=payload)
# build message #
payload = {
"topic" : user_topic or "test", #TODO fix topic
"message" : message,
"title" : "Atlantis Notify",
#"tags" : [],
#"priority" : 4,
#"attach" : None,
#"click" : None,
#"actions" : []
}
# send #
r = requests.post(ntfy_push_target, auth=(ntfy_user, ntfy_pass), json=payload)
print(r.status_code, r.text, payload)
r.raise_for_status() r.raise_for_status()
confirm_dispatch(uuid)
# talk to dispatch #
confirm_dispatch(dispatch_uuid)
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
report_failed_dispatch(uuid, str(e)) report_failed_dispatch(dispatch_uuid, str(e))
except requests.exceptions.ConnectionError as e: except requests.exceptions.ConnectionError as e:
report_failed_dispatch(uuid, str(e)) report_failed_dispatch(dispatch_uuid, str(e))
def report_failed_dispatch(uuid, error): def report_failed_dispatch(uuid, error):
'''Inform the server that the dispatch has failed''' '''Inform the server that the dispatch has failed'''
response = requests.post(args.dispatch_target + "/report-dispatch-failed", payload = [{ "uuid" : uuid, "error" : error }]
json={ "uuid" : uuid, "error" : error }) response = requests.post(DISPATCH_SERVER + "/report-dispatch-failed", json=payload, auth=AUTH)
if response.status_code not in [200, 204]: if response.status_code not in [200, 204]:
print("Failed to report back failed dispatch for {} ({})".format( print("Failed to report back failed dispatch for {} ({})".format(
@@ -52,8 +73,8 @@ def report_failed_dispatch(uuid, error):
def confirm_dispatch(uuid): def confirm_dispatch(uuid):
'''Confirm to server that message has been dispatched and can be removed''' '''Confirm to server that message has been dispatched and can be removed'''
response = requests.post(target + "/confirm-dispatch", json=[{ "uuid" : uuid }], payload = [{ "uuid" : uuid }]
auth=(args.user, args.password)) response = requests.post(DISPATCH_SERVER + "/confirm-dispatch", json=payload, auth=AUTH)
if response.status_code not in [200, 204]: if response.status_code not in [200, 204]:
print("Failed to confirm dispatch with server for {} ({})".format( print("Failed to confirm dispatch with server for {} ({})".format(
@@ -79,7 +100,8 @@ if __name__ == "__main__":
args = parser.parse_args() args = parser.parse_args()
# set authentication # # set dispatch server & authentication #
DISPATCH_SERVER = args.dispatch_server
AUTH = (args.dispatch_user, args.dispatch_password) AUTH = (args.dispatch_user, args.dispatch_password)
dispatch_server = args.dispatch_server or os.environ.get("DISPATCH_SERVER") dispatch_server = args.dispatch_server or os.environ.get("DISPATCH_SERVER")
@@ -95,8 +117,7 @@ if __name__ == "__main__":
smtp_pass = args.smtp_pass or os.environ.get("SMTP_PASS") smtp_pass = args.smtp_pass or os.environ.get("SMTP_PASS")
# request dispatches # # request dispatches #
response = requests.get(args.target + "/get-dispatch".format(args.method), response = requests.get(args.dispatch_server + "/get-dispatch?method=all", auth=AUTH)
auth=(args.user, args.password))
# check status # # check status #
if response.status_code == HTTP_NOT_FOUND: if response.status_code == HTTP_NOT_FOUND:
@@ -114,8 +135,8 @@ if __name__ == "__main__":
# iterate over dispatch requests # # iterate over dispatch requests #
for entry in response.json(): for entry in response.json():
user = entry["person"] user = entry["username"]
dispatch_uuid = entry["uid"] dispatch_uuid = entry["uuid"]
method = entry["method"] method = entry["method"]
message = entry["message"] message = entry["message"]
@@ -132,9 +153,9 @@ if __name__ == "__main__":
elif method == "email": elif method == "email":
email_send(dispatch_uuid, email_address, message, smtp_target, smtp_user, smtp_pass) email_send(dispatch_uuid, email_address, message, smtp_target, smtp_user, smtp_pass)
elif method == "debug": elif method == "debug":
debug_send(uuid, entry) debug_send(dispatch_uuid, entry)
elif method == "debug-fail": elif method == "debug-fail":
debug_send(uuid, entry, fail_it=True) debug_send(dispatch_uuid, entry, fail_it=True)
else: else:
print("Unsupported dispatch method {}".format(entry["method"]), sys=sys.stderr) print("Unsupported dispatch method {}".format(entry["method"]), sys=sys.stderr)
continue continue