40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import requests
|
|
import subprocess
|
|
|
|
|
|
class Gotify:
|
|
def __init__(self, token: str):
|
|
self._token = token
|
|
self._url = f"https://gotify.lennartalff.net/message?token={self._token}"
|
|
|
|
def send(self, priority: int, title: str, text: str):
|
|
return requests.post(
|
|
self._url,
|
|
json={
|
|
"message": text,
|
|
"priority": priority,
|
|
"title": title,
|
|
},
|
|
)
|
|
|
|
def send_info(self, title="", text=""):
|
|
return self.send(priority=5, title=title, text=text)
|
|
|
|
def send_error(self, title="", text=""):
|
|
return self.send(priority=10, title=f"❗💀❗ {title}", text=text)
|
|
|
|
def send_subprocess_error(self, title, error: subprocess.CalledProcessError):
|
|
text = f"stdout:\n{error.stdout}\nstderr:\n{error.stderr}"
|
|
return self.send_error(title=title, text=text)
|
|
|
|
def send_backup_successful(self, borg_result: subprocess.CompletedProcess):
|
|
return self.send_success("Backup completed", borg_result)
|
|
|
|
def send_success(self, title, process_result: subprocess.CompletedProcess):
|
|
text = "\n".join([process_result.stdout, process_result.stderr])
|
|
return self.send_info(f"✅ {title}", text)
|
|
|
|
def send_warning(self, title, text):
|
|
return self.send(priority=5, title=f"❗ {title}", text=text)
|
|
|
|
|