feat: Prepare for notification trough mail and Telegram
This commit is contained in:
@@ -8,6 +8,7 @@ pytz==2025.2
|
||||
requests==2.32.4
|
||||
setuptools==80.9.0
|
||||
six==1.17.0
|
||||
telegram==0.0.1
|
||||
typing==3.7.4.3
|
||||
urllib3==2.5.0
|
||||
zope.interface==7.2
|
||||
|
||||
76
session_notifier.py
Normal file
76
session_notifier.py
Normal 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)
|
||||
Reference in New Issue
Block a user