mirror of
https://github.com/jokob-sk/NetAlertX.git
synced 2026-04-15 22:51:37 -07:00
NTFY token support
This commit is contained in:
@@ -408,6 +408,34 @@
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"function": "TOKEN",
|
||||||
|
"type": {
|
||||||
|
"dataType": "string",
|
||||||
|
"elements": [
|
||||||
|
{
|
||||||
|
"elementType": "input",
|
||||||
|
"elementOptions": [],
|
||||||
|
"transformers": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default_value": "",
|
||||||
|
"options": [],
|
||||||
|
"localized": ["name", "description"],
|
||||||
|
"name": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "NTFY token"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": [
|
||||||
|
{
|
||||||
|
"language_code": "en_us",
|
||||||
|
"string": "Enter a token if authentication is enabled for hosting an instance. If a token is provided, the username and password will be ignored."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"function": "USER",
|
"function": "USER",
|
||||||
"type": {
|
"type": {
|
||||||
|
|||||||
@@ -94,7 +94,12 @@ def send(html, text):
|
|||||||
response_text = ''
|
response_text = ''
|
||||||
response_status_code = ''
|
response_status_code = ''
|
||||||
|
|
||||||
|
# settings
|
||||||
|
token = get_setting_value('NTFY_TOKEN')
|
||||||
|
user = get_setting_value('NTFY_USER')
|
||||||
|
pwd = get_setting_value('NTFY_PASSWORD')
|
||||||
|
|
||||||
|
# prepare request headers
|
||||||
headers = {
|
headers = {
|
||||||
"Title": "NetAlertX Notification",
|
"Title": "NetAlertX Notification",
|
||||||
"Actions": "view, Open Dashboard, "+ get_setting_value('REPORT_DASHBOARD_URL'),
|
"Actions": "view, Open Dashboard, "+ get_setting_value('REPORT_DASHBOARD_URL'),
|
||||||
@@ -102,15 +107,16 @@ def send(html, text):
|
|||||||
"Tags": "warning"
|
"Tags": "warning"
|
||||||
}
|
}
|
||||||
|
|
||||||
# if username and password are set generate hash and update header
|
# if token or username and password are set generate hash and update header
|
||||||
if get_setting_value('NTFY_USER') != "" and get_setting_value('NTFY_PASSWORD') != "":
|
if token != '':
|
||||||
|
headers["Authorization"] = "Bearer {}".format(token)
|
||||||
|
elif user != "" and pwd != "":
|
||||||
# Generate hash for basic auth
|
# Generate hash for basic auth
|
||||||
# usernamepassword = "{}:{}".format(get_setting_value('NTFY_USER'),get_setting_value('NTFY_PASSWORD'))
|
basichash = b64encode(bytes(user + ':' + pwd, "utf-8")).decode("ascii")
|
||||||
basichash = b64encode(bytes(get_setting_value('NTFY_USER') + ':' + get_setting_value('NTFY_PASSWORD'), "utf-8")).decode("ascii")
|
|
||||||
|
|
||||||
# add authorization header with hash
|
# add authorization header with hash
|
||||||
headers["Authorization"] = "Basic {}".format(basichash)
|
headers["Authorization"] = "Basic {}".format(basichash)
|
||||||
|
|
||||||
|
# call NTFY service
|
||||||
try:
|
try:
|
||||||
response = requests.post("{}/{}".format( get_setting_value('NTFY_HOST'),
|
response = requests.post("{}/{}".format( get_setting_value('NTFY_HOST'),
|
||||||
get_setting_value('NTFY_TOPIC')),
|
get_setting_value('NTFY_TOPIC')),
|
||||||
@@ -119,8 +125,6 @@ def send(html, text):
|
|||||||
|
|
||||||
response_status_code = response.status_code
|
response_status_code = response.status_code
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Check if the request was successful (status code 200)
|
# Check if the request was successful (status code 200)
|
||||||
if response_status_code == 200:
|
if response_status_code == 200:
|
||||||
response_text = response.text # This captures the response body/message
|
response_text = response.text # This captures the response body/message
|
||||||
|
|||||||
Reference in New Issue
Block a user