#!/usr/bin/env bash set -euo pipefail CONFIG_FILE="/opt/appfactory/config/appfactory.env" if [ -f "$CONFIG_FILE" ]; then set -a source "$CONFIG_FILE" set +a fi EVENT_TYPE="${APPFACTORY_ALERT_EVENT_TYPE:-unknown}" SERVICE_ID="${APPFACTORY_ALERT_SERVICE_ID:-unknown}" INCIDENT_ID="${APPFACTORY_ALERT_INCIDENT_ID:-unknown}" PAYLOAD="${APPFACTORY_ALERT_PAYLOAD_JSON:-{}}" echo "ALERT" echo "EVENT_TYPE=$EVENT_TYPE" echo "SERVICE_ID=$SERVICE_ID" echo "INCIDENT_ID=$INCIDENT_ID" echo "PAYLOAD=$PAYLOAD" if [ "${ALERT_EMAIL_ENABLED:-0}" != "1" ]; then echo "Email alert disabled." exit 0 fi python3 - <<'PY' import os import smtplib from email.message import EmailMessage event_type = os.getenv("APPFACTORY_ALERT_EVENT_TYPE", "unknown") service_id = os.getenv("APPFACTORY_ALERT_SERVICE_ID", "unknown") incident_id = os.getenv("APPFACTORY_ALERT_INCIDENT_ID", "unknown") payload = os.getenv("APPFACTORY_ALERT_PAYLOAD_JSON", "{}") smtp_host = os.getenv("SMTP_HOST") smtp_port = int(os.getenv("SMTP_PORT", "587")) smtp_username = os.getenv("SMTP_USERNAME") smtp_password = os.getenv("SMTP_PASSWORD") smtp_from = os.getenv("SMTP_FROM") or smtp_username alert_to = os.getenv("ALERT_EMAIL_TO") missing = [ name for name, value in { "SMTP_HOST": smtp_host, "SMTP_USERNAME": smtp_username, "SMTP_PASSWORD": smtp_password, "SMTP_FROM": smtp_from, "ALERT_EMAIL_TO": alert_to, }.items() if not value ] if missing: raise SystemExit("Missing email config: " + ", ".join(missing)) subject = f"[CSBot Alert] {event_type} - {service_id}" body = f"""CSBot Services Portal alert Událost: {event_type} Služba: {service_id} Incident ID: {incident_id} Payload: {payload} """ msg = EmailMessage() msg["From"] = smtp_from msg["To"] = alert_to msg["Subject"] = subject msg.set_content(body) with smtplib.SMTP(smtp_host, smtp_port, timeout=20) as smtp: smtp.starttls() smtp.login(smtp_username, smtp_password) smtp.send_message(msg) print(f"Email sent to {alert_to}") PY