EMail notifications work in progress

This commit is contained in:
Daniel Schick 2024-12-16 16:25:52 +01:00
parent 3e2b9f649c
commit 14244e2f48
5 changed files with 52 additions and 25 deletions

View File

@ -29,7 +29,7 @@
},
{
"type" : 5,
"color" : "#f8f8f8",
"color" : "#a8a8a8",
"name" : "unassigned",
"link" : "https://www.bremen-calling.de/",
"msg_text" : "Nominierung abgewählt"

View File

@ -6,7 +6,7 @@
<tbody>
<tr>
<td style="font-family: Helvetica, sans-serif; font-size: 16px; vertical-align: top; border-radius: 4px; text-align: center; background-color: [[color]];" valign="top" align="center" bgcolor="[[color]]">
<a href="[[link]]" target="_blank" style="border: solid 2px [[color]]; border-radius: 4px; box-sizing: border-box; cursor: pointer; display: inline-block; font-size: 16px; font-weight: bold; margin: 0; padding: 12px 24px; text-decoration: none; text-transform: capitalize; background-color: #0867ec; border-color: [[color]]; color: #ffffff;">[[text]]</a>
<a href="[[link]]" target="_blank" style="border: solid 2px [[color]]; border-radius: 4px; box-sizing: border-box; cursor: pointer; display: inline-block; font-size: 16px; font-weight: bold; margin: 0; padding: 12px 24px; text-decoration: none; text-transform: capitalize; background-color: [[color]]; border-color: [[color]]; color: #ffffff;">[[text]]</a>
</td>
</tr>
</tbody>

View File

@ -1,5 +1,5 @@
<!doctype html>
<html lang="en">
<html lang="de">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

View File

@ -5,4 +5,16 @@ NOTIFICATION_COOLDOWN = 600 # 10 minutes until a notification gets real
NOTIFICATION_MAX_AGE_DAYS = 3 # 3 days until a notification gets deleted
# Placeholder for the email credentials filled by startup logic
email_credentials = dict()
email_credentials = dict()
# Holding var for global message notification type info
message_types = dict()
# Constants for the email display
shipcall_types = {
1: "Arrival",
2: "Departure",
3: "Shifting"
}

View File

@ -1,6 +1,8 @@
import logging
import pydapper
import smtplib
import json
import os
from email.message import EmailMessage
from BreCal.schemas import model, defs
@ -82,14 +84,41 @@ def SendEmails(email_dict):
conn.ehlo()
conn.login(defs.email_credentials["sender"], defs.email_credentials["password_send"])
for user, message in email_dict.items():
current_path = os.path.dirname(os.path.abspath(__file__))
if not defs.message_types:
f = open(os.path.join(current_path,"../msg/msg_types.json"), encoding='utf-8');
defs.message_types = json.load(f)
f.close()
for user, notifications in email_dict.items():
msg = EmailMessage()
msg["Subject"] = '[Bremen calling] Notification'
msg["From"] = defs.email_credentials["sender"]
msg["To"] = user.user_email
# TODO: pretty-print and format message according to template
msg.set_content(message)
with open(os.path.join(current_path,'../msg/notification_template.html'), mode="r", encoding="utf-8") as file:
body = file.read()
replacement = ""
for notification in notifications:
message_type = defs.message_types[notification.type]
with open(os.path.join(current_path,'../msg/notification_element.html'), mode="r", encoding="utf-8") as file:
element = file.read()
element = element.replace("[[color]]", message_type["color"])
element = element.replace("[[link]]", message_type["link"])
# We want to show the following information for each notification:
# Ship-name, Arr/Dep/Shift, ETA/ETD, berth
element = element.replace("[[text]]", message_type["msg_text"])
replacement += element
body = body.replace("[[NOTIFICATION_ELEMENTS]]", replacement)
msg.set_content(body, subtype='html', charset='utf-8', cte='8bit')
conn.sendmail(defs.email_credentials["sender"], user.user_email, msg.as_string())
@ -136,23 +165,8 @@ def SendNotifications():
# send notification to user
if user.notify_email:
if user not in email_dict:
email_dict[user] = ""
match notification.type:
case 1: # assignment
email_dict[user] += "\n"
email_dict[user] += "You have been assigned to a new shipcall: " + str(notification.message or "")
case 2: # next 24 hours
email_dict[user] += "\n"
email_dict[user] += "A shipcall is scheduled for the next 24 hours: " + str(notification.message or "")
case 3: # Time conflict
email_dict[user] += "\n"
email_dict[user] += "There is a time conflict in a shipcall: " + str(notification.message or "")
case 4: # Time conflict resolved
email_dict[user] += "\n"
email_dict[user] += "A time conflict in a shipcall has been resolved: " + str(notification.message or "")
case 5: # unassigned
email_dict[user] += "\n"
email_dict[user] += "You have been unassigned from a shipcall: " + str(notification.message or "")
email_dict[user] = []
email_dict[user].append(notification)
if user.notify_whatsapp:
# TBD
pass
@ -164,7 +178,7 @@ def SendNotifications():
# send emails (if any)
if len(email_dict) > 0:
SendEmails(email_dict)
SendEmails(email_dict)
except Exception as ex:
logging.error(ex)
@ -232,6 +246,7 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60):
schedule.every().day.at("09:00").do(eval_next_24_hrs)
SendNotifications()
add_function_to_schedule_send_notifications(1)
# TODO: Add schedule function to evaluate all notifications in level 1 and create actions