forked from MrKrisKrisu/HISinOne-QIS-exam-notification
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifier.py
53 lines (41 loc) · 2.23 KB
/
notifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import json
import smtplib
import ssl
from enum import IntEnum
import requests
from configuration import Configuration
class ConsoleOutputMode(IntEnum):
NONE = 0
PRETTY_CHANGES = 1
JSON_ALL = 2
class Notifier:
config = None
def __init__(self, config: Configuration):
self.config = config
def notify(self, message, subject, noten):
if int(self.config.getDefault('consoleOutputMode')) == int(ConsoleOutputMode.JSON_ALL):
print(json.dumps(noten))
elif int(self.config.getDefault('consoleOutputMode')) == int(ConsoleOutputMode.PRETTY_CHANGES):
print(subject)
print(message)
if self.config.getDefault('mailSmtpHost') != '':
context = ssl.create_default_context()
if self.config.getDefaultBool('mailStartTLS'):
with smtplib.SMTP(self.config.getDefault('mailSmtpHost'), self.config.getDefault('mailSSLPort')) as server:
server.starttls(context=context)
self.sendMail(message, server, subject)
if self.config.verbose:
print("Mail wurde erfolgreich über eine StartTLS verschlüsselte Verbindung gesendet")
else:
with smtplib.SMTP_SSL(self.config.getDefault('mailSmtpHost'), self.config.getDefault('mailSSLPort'), context=context) as server:
self.sendMail(message, server, subject)
if self.config.verbose:
print("Mail wurde erfolgreich über eine SSL verschlüsselte Verbindung gesendet")
if self.config.getDefault('telegramChatId') != '':
send_text = 'https://api.telegram.org/bot' + self.config.getDefault('telegramBotToken') + '/sendMessage?chat_id=' \
+ self.config.getDefault('telegramChatId') + '&parse_mode=Markdown&text=' + '**' + subject + '**' + message
requests.get(send_text)
def sendMail(self, message, server, subject):
server.login(self.config.getDefault('mailLoginUser'), self.config.getDefault('mailLoginPassword'))
server.sendmail(self.config.getDefault('mailSenderMail'), self.config.getDefault('mailReceiverMail'),
"Subject: " + subject + "\n\n" + message)