feat: Prepare for notification trough mail and Telegram

This commit is contained in:
kbe
2025-07-20 03:27:13 +02:00
parent 4b27a6f4a3
commit 6c8647b11c
2 changed files with 77 additions and 0 deletions

76
session_notifier.py Normal file
View File

@@ -0,0 +1,76 @@
import smtplib
from email.message import EmailMessage
from telegram import Bot
class SessionNotifier:
"""
A class to handle notifications for session bookings.
Supports sending notifications via email and Telegram.
Attributes:
email_credentials (dict): Dictionary containing email credentials
(e.g., 'from', 'to', 'password')
telegram_credentials (dict): Dictionary containing Telegram credentials
(e.g., 'token', 'chat_id')
"""
def __init__(self, email_credentials, telegram_credentials):
"""
Initialize the SessionNotifier with email and Telegram credentials.
Args:
email_credentials (dict): Email credentials for authentication
telegram_credentials (dict): Telegram credentials for authentication
"""
self.email_credentials = email_credentials
self.telegram_credentials = telegram_credentials
def send_email_notification(self, message):
"""
Send an email notification with the given message.
Args:
message (str): The message content to be sent in the email
"""
# Create an EmailMessage object
email = EmailMessage()
email.set_content(message)
# Set the email sender and recipient
email['From'] = self.email_credentials['from']
email['To'] = self.email_credentials['to']
# Set the email subject
email['Subject'] = 'Session Booking Notification'
# Send the email using smtplib
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(self.email_credentials['from'], self.email_credentials['password'])
smtp.send_message(email)
def send_telegram_notification(self, message):
"""
Send a Telegram notification with the given message.
Args:
message (str): The message content to be sent in the Telegram chat
"""
# Create a Bot instance with the provided token
bot = Bot(token=self.telegram_credentials['token'])
# Send the message to the specified chat ID
bot.send_message(chat_id=self.telegram_credentials['chat_id'], text=message)
def notify_session_booking(self, session_details):
"""
Notify about a session booking via email and Telegram.
Args:
session_details (str): Details about the booked session
"""
# Create messages for both email and Telegram
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)