OVH-server-tracker/src/notifications.py

96 lines
2.9 KiB
Python
Raw Normal View History

2020-12-09 16:20:59 +01:00
#!/usr/bin/env python3
# coding: utf-8
# -*- coding: utf-8 -*-
import apprise
from src.logs import Logger
2020-12-09 19:32:27 +01:00
from src.settings import Settings
2020-12-09 16:20:59 +01:00
class Notifications:
def __init__(self, config):
2020-12-09 19:32:27 +01:00
self.logger = Logger(config["log"]["level"], config["log"]["path"])
2020-12-09 16:20:59 +01:00
self.config = config["notifications"]
self.apprise = apprise.Apprise()
self.allowedNotifyChannel = ["email", "discord", "matrix", "xmpp"]
self._notifyChannel = {
2020-12-09 19:32:27 +01:00
"email": self._addEmail,
"discord": self._addDiscord,
"xmpp": self._addXmpp,
"matrix": self._addMatrix,
2020-12-09 16:20:59 +01:00
}
self.setNotificationsChannel()
def setNotificationsChannel(self):
for key, val in self.config.items():
if key not in self.allowedNotifyChannel:
self.logger.error(
"{} is not possible".format(self.allowedNotifyChannel)
)
else:
self._notifyChannel[key]()
return True
2020-12-11 14:37:31 +01:00
def run(self, message):
2020-12-09 19:32:27 +01:00
refOfServer = Settings().get("config/refToName.yml")
self.apprise.notify(
2020-12-11 13:11:28 +01:00
body="",
2020-12-11 14:37:31 +01:00
title="{}".format(message),
2020-12-09 19:32:27 +01:00
notify_type=apprise.NotifyType.INFO,
)
2020-12-09 16:20:59 +01:00
def _addEmail(self):
if self.config["email"]["secure"] is True:
mailto = "mailtos"
2020-12-09 19:32:27 +01:00
mode = self.config["email"]["mode"]
2020-12-09 16:20:59 +01:00
else:
mailto = "mailto"
2020-12-09 19:32:27 +01:00
mode = ""
2020-12-09 16:20:59 +01:00
self.apprise.add(
2020-12-09 19:32:27 +01:00
"{}://{}:{}@{}:{}?from={}&name=OVHTracker&to={}?mode={}".format(
2020-12-09 16:20:59 +01:00
mailto,
self.config["email"]["user"],
self.config["email"]["password"],
self.config["email"]["host"],
self.config["email"]["port"],
2020-12-09 19:32:27 +01:00
self.config["email"]["from"],
self.config["email"]["to"],
mode,
2020-12-09 16:20:59 +01:00
)
)
def _addXmpp(self):
2020-12-09 19:32:27 +01:00
if self.config["xmpp"]["secure"] is True:
xmpp = "xmpps"
else:
xmpp = "xmpp"
self.apprise.add(
"{}://{}:{}@{}:{}?to={}".format(
xmpp,
self.config["xmpp"]["user"],
self.config["xmpp"]["password"],
self.config["xmpp"]["host"],
self.config["xmpp"]["port"],
self.config["xmpp"]["to"],
)
)
def _addDiscord(self):
2020-12-11 13:11:28 +01:00
self.apprise.add(
"discord://{}/{}/".format(
self.config["discord"]["webhookid"],
self.config["discord"]["webhooktoken"],
)
)
2020-12-09 16:20:59 +01:00
def _addMatrix(self):
2020-12-09 20:34:00 +01:00
self.apprise.add(
"matrixs://{}:{}@{}/!{}?webhook=matrix".format(
self.config["matrix"]["user"],
self.config["matrix"]["token"],
self.config["matrix"]["host"],
self.config["matrix"]["room_id"],
)
)