Update blitz.notify (#1402)

* add from and use system python
This commit is contained in:
frennkie 2020-08-02 14:41:27 +02:00 committed by GitHub
parent f30e074c3c
commit fe69fae904
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 16 deletions

View File

@ -87,8 +87,8 @@ def ext(cmd=None, message=None, debug=False):
# e-Mail
def mail(recipient=None, message=None, subject=None, cert=None, encrypt=False,
from_name=None, from_address=None, debug=False):
def mail(recipient: str = None, message: str = None, subject: str = None, cert: str = None,
encrypt: bool = False, from_name: str = None, from_address: str = None, debug: bool = False):
if debug:
print("send mail")
print("msg: {}".format(message))
@ -107,7 +107,7 @@ def mail(recipient=None, message=None, subject=None, cert=None, encrypt=False,
'From: {} <{}>'.format(from_name, from_address),
"Subject: {}".format(subject),
"",
"{}".format(message.encode('utf8'))
"{}".format(message)
]
with open(cert, 'rb') as pem:
@ -122,7 +122,7 @@ def mail(recipient=None, message=None, subject=None, cert=None, encrypt=False,
msg['From'] = '{} <{}>'.format(from_name, from_address),
msg['To'] = recipient
msg.set_payload(message.encode('utf8'))
msg.set_payload(message)
msg_to_send = msg.as_bytes()
# send message via e-Mail
@ -139,7 +139,7 @@ def mail(recipient=None, message=None, subject=None, cert=None, encrypt=False,
raise Exception(err)
def slack(message=None, debug=False):
def slack(message: str = None, debug: bool = False):
if debug:
print("send slack")
print("msg: {}".format(message))
@ -148,4 +148,3 @@ def slack(message=None, debug=False):
if __name__ == "__main__":
main()

View File

@ -40,6 +40,14 @@ if ! grep -Eq "^notifyMailHostname=.*" /mnt/hdd/raspiblitz.conf; then
echo "notifyMailHostname=$(hostname)" | sudo tee -a /mnt/hdd/raspiblitz.conf >/dev/null
fi
if ! grep -Eq "^notifyMailFromAddress=.*" /mnt/hdd/raspiblitz.conf; then
echo "notifyMailFromAddress=rb@example.com" | sudo tee -a /mnt/hdd/raspiblitz.conf >/dev/null
fi
if ! grep -Eq "^notifyMailFromName=.*" /mnt/hdd/raspiblitz.conf; then
echo "notifyMailFromName=\"RB User\"" | sudo tee -a /mnt/hdd/raspiblitz.conf >/dev/null
fi
if ! grep -Eq "^notifyMailUser=.*" /mnt/hdd/raspiblitz.conf; then
echo "notifyMailUser=username" | sudo tee -a /mnt/hdd/raspiblitz.conf >/dev/null
fi
@ -72,11 +80,12 @@ if [ "$1" = "1" ] || [ "$1" = "on" ]; then
echo "switching the NOTIFY ON"
# install sstmp if not already present
/usr/bin/which ssmtp &>/dev/null
[ $? -eq 0 ] || sudo apt-get install -y ssmtp
if ! command -v ssmtp >/dev/null; then
sudo apt-get install -y ssmtp
fi
# install python lib for smime into virtual env
/home/admin/python3-env-lnd/bin/python -m pip install smime
sudo -H /usr/bin/python3 -m pip install smime
# write ssmtp config
cat << EOF | sudo tee /etc/ssmtp/ssmtp.conf >/dev/null
@ -127,24 +136,22 @@ if [ "$1" = "send" ]; then
exit 1
fi
/usr/bin/which ssmtp &>/dev/null
if ! [ $? -eq 0 ]; then
if ! command -v ssmtp >/dev/null; then
echo "please run \"on\" first"
exit 1
fi
# now parse settings from config and use to send the message
if [ "${notifyMethod}" = "ext" ]; then
/home/admin/python3-env-lnd/bin/python3 /home/admin/XXsendNotification.py ext ${notifyExtCmd} "$2"
/usr/bin/python3 /home/admin/XXsendNotification.py ext ${notifyExtCmd} "$2"
elif [ "${notifyMethod}" = "mail" ]; then
if [ "${notifyMailEncrypt}" = "on" ]; then
/home/admin/python3-env-lnd/bin/python3 /home/admin/XXsendNotification.py mail "${@:3}" --cert ${notifyMailToCert} --encrypt ${notifyMailTo} "$2"
/usr/bin/python3 /home/admin/XXsendNotification.py mail "${@:3}" --from-address "${notifyMailFromAddress}" --from-name "${notifyMailFromName}" --cert "${notifyMailToCert}" --encrypt ${notifyMailTo} "$2"
else
/home/admin/python3-env-lnd/bin/python3 /home/admin/XXsendNotification.py mail "${@:3}" ${notifyMailTo} "$2"
/usr/bin/python3 /home/admin/XXsendNotification.py mail "${@:3}" --from-address "${notifyMailFromAddress}" --from-name "${notifyMailFromName}" "${notifyMailTo}" "$2"
fi
elif [ "${notifyMethod}" = "slack" ]; then
/home/admin/python3-env-lnd/bin/python3 /home/admin/XXsendNotification.py slack -h "$2"
/usr/bin/python3 /home/admin/XXsendNotification.py slack -h "$2"
else
echo "unknown notification method - check /mnt/hdd/raspiblitz.conf"
fi