Files
SneakyMon/app/utils/gotify_api.py

59 lines
1.9 KiB
Python

import requests
class GotifyNotifier:
def __init__(self, server_url:str, api_token:str):
self.base_url = server_url
self.token = api_token
pass
def gotify(self, title:str="Test", markdown:str="testing msg",priority:int=5, image_url=None):
"""
Send a message to a Gotify server using a Bearer token.
Args:
server_url (str): Base URL of the Gotify server (e.g., http://10.10.20.8:8080).
token (str): Gotify app token for authentication.
title (str): Title of the message.
message (str): Body of the message.
priority (int, optional): Message priority (1-10). Defaults to 5.
Returns:
bool: True if the message was sent successfully, False otherwise.
"""
server_url = self.base_url
token = self.token
if not server_url or not token:
print("[!] Missing GOTIFY_URL or GOTIFY_TOKEN not set")
return False
url = f"{server_url.rstrip('/')}/message"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
payload = {
"title": title,
"message": markdown,
"priority": priority
}
if image_url is not None:
extras = {
"client::display": {"contentType": "text/markdown"},
"client::notification": {"bigImageUrl": image_url}
}
payload.update({"extras":extras})
try:
response = requests.post(url, headers=headers, json=payload, timeout=10)
response.raise_for_status()
return True
except requests.RequestException as e:
print(f"[!] Failed to send Gotify message: {e}")
print(payload)
return False