From 35160bc0330481981a37f44f66db742fb636d1d5 Mon Sep 17 00:00:00 2001 From: kbe Date: Sun, 20 Jul 2025 03:47:12 +0200 Subject: [PATCH] feat: Enable (or not) email or Telegram notifications --- .env.example | 4 ++++ crossfit_booker.py | 13 +++++++++++-- session_notifier.py | 18 ++++++++++++++---- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index 4424a4a..020e235 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,10 @@ CROSSFIT_USERNAME=Kevin8407 CROSSFIT_PASSWORD=9vx03OSE +# Notification settings +ENABLE_EMAIL_NOTIFICATIONS=true +ENABLE_TELEGRAM_NOTIFICATIONS=true + EMAIL_FROM=kevin@mistergeek.fr EMAIL_TO=kbataille@vivaldi.net EMAIL_PASSWORD= diff --git a/crossfit_booker.py b/crossfit_booker.py index f9b69f6..357c180 100644 --- a/crossfit_booker.py +++ b/crossfit_booker.py @@ -91,13 +91,22 @@ class CrossFitBooker: "to": os.environ.get("EMAIL_TO"), "password": os.environ.get("EMAIL_PASSWORD") } - + telegram_credentials = { "token": os.environ.get("TELEGRAM_TOKEN"), "chat_id": os.environ.get("TELEGRAM_CHAT_ID") } - self.notifier = SessionNotifier(email_credentials, telegram_credentials) + # Get notification settings from environment variables + enable_email = os.environ.get("ENABLE_EMAIL_NOTIFICATIONS", "true").lower() in ("true", "1", "yes") + enable_telegram = os.environ.get("ENABLE_TELEGRAM_NOTIFICATIONS", "true").lower() in ("true", "1", "yes") + + self.notifier = SessionNotifier( + email_credentials, + telegram_credentials, + enable_email=enable_email, + enable_telegram=enable_telegram + ) def get_auth_headers(self) -> Dict[str, str]: """ diff --git a/session_notifier.py b/session_notifier.py index 40d590d..5aba62f 100644 --- a/session_notifier.py +++ b/session_notifier.py @@ -1,4 +1,5 @@ import smtplib +import os from email.message import EmailMessage from telegram import Bot @@ -12,18 +13,24 @@ class SessionNotifier: (e.g., 'from', 'to', 'password') telegram_credentials (dict): Dictionary containing Telegram credentials (e.g., 'token', 'chat_id') + enable_email (bool): Whether to enable email notifications + enable_telegram (bool): Whether to enable Telegram notifications """ - def __init__(self, email_credentials, telegram_credentials): + def __init__(self, email_credentials, telegram_credentials, enable_email=True, enable_telegram=True): """ Initialize the SessionNotifier with email and Telegram credentials. Args: email_credentials (dict): Email credentials for authentication telegram_credentials (dict): Telegram credentials for authentication + enable_email (bool): Whether to enable email notifications + enable_telegram (bool): Whether to enable Telegram notifications """ self.email_credentials = email_credentials self.telegram_credentials = telegram_credentials + self.enable_email = enable_email + self.enable_telegram = enable_telegram def send_email_notification(self, message): """ @@ -71,6 +78,9 @@ class SessionNotifier: email_message = f"Session booked: {session_details}" telegram_message = f"Session booked: {session_details}" - # Send notifications through both channels - self.send_email_notification(email_message) - self.send_telegram_notification(telegram_message) \ No newline at end of file + # Send notifications through enabled channels + if self.enable_email: + self.send_email_notification(email_message) + + if self.enable_telegram: + self.send_telegram_notification(telegram_message) \ No newline at end of file