File size: 1,730 Bytes
86ffe15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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}")