Spaces:
Build error
Build error
| # notifier.py | |
| import smtplib | |
| import requests | |
| from email.mime.text import MIMEText | |
| from email.mime.multipart import MIMEMultipart | |
| from config import Config | |
| def send_email_notification(subject, body, recipient, cc=None): | |
| """Send an email via Gmail SMTP.""" | |
| if not Config.YOUR_PASSWORD: | |
| print("Email password not set. Skipping email notification.") | |
| return | |
| msg = MIMEMultipart() | |
| msg['From'] = Config.YOUR_EMAIL | |
| msg['To'] = recipient | |
| if cc: | |
| msg['Cc'] = cc | |
| msg['Subject'] = subject | |
| msg.attach(MIMEText(body, 'plain')) | |
| try: | |
| server = smtplib.SMTP(Config.SMTP_SERVER, Config.SMTP_PORT) | |
| server.starttls() | |
| server.login(Config.YOUR_EMAIL, Config.YOUR_PASSWORD) | |
| recipients = [recipient] | |
| if cc: | |
| recipients.append(cc) | |
| server.sendmail(Config.YOUR_EMAIL, recipients, msg.as_string()) | |
| server.quit() | |
| print(f"Email sent to {recipient}") | |
| except Exception as e: | |
| print(f"Email send failed: {e}") | |
| def send_telegram_message(message): | |
| """Send a message via Telegram bot.""" | |
| if not Config.TELEGRAM_BOT_TOKEN or not Config.TELEGRAM_CHAT_ID: | |
| print("Telegram credentials missing. Skipping.") | |
| return | |
| url = f"https://api.telegram.org/bot{Config.TELEGRAM_BOT_TOKEN}/sendMessage" | |
| payload = { | |
| 'chat_id': Config.TELEGRAM_CHAT_ID, | |
| 'text': message, | |
| 'parse_mode': 'Markdown' | |
| } | |
| try: | |
| resp = requests.post(url, json=payload, timeout=5) | |
| if resp.status_code == 200: | |
| print("Telegram message sent.") | |
| else: | |
| print(f"Telegram error: {resp.text}") | |
| except Exception as e: | |
| print(f"Telegram failed: {e}") |