-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotifier.py
60 lines (52 loc) · 1.82 KB
/
notifier.py
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
53
54
55
56
57
58
59
60
# notifier.py
# Handles notifications for the Plant Monitoring System.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from telegram import Bot
from config import TELEGRAM_BOT_TOKEN
# Telegram notifications
def send_telegram_notification(chat_id, message):
"""
Sends a notification via Telegram.
"""
try:
bot = Bot(token=TELEGRAM_BOT_TOKEN)
bot.send_message(chat_id=chat_id, text=message)
print(f"Telegram notification sent to {chat_id}: {message}")
except Exception as e:
print(f"Error sending Telegram notification: {e}")
# Email notifications
def send_email_notification(to_email, subject, message):
"""
Sends a notification via Email.
"""
try:
smtp_server = "smtp.gmail.com"
smtp_port = 587
sender_email = "[email protected]"
sender_password = "your-email-password"
msg = MIMEMultipart()
msg["From"] = sender_email
msg["To"] = to_email
msg["Subject"] = subject
msg.attach(MIMEText(message, "plain"))
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, to_email, msg.as_string())
print(f"Email notification sent to {to_email}: {subject}")
except Exception as e:
print(f"Error sending email notification: {e}")
# Example notifications
if __name__ == "__main__":
# Telegram Example
chat_id = "123456789"
send_telegram_notification(chat_id, "Test notification from Plant Monitoring System.")
# Email Example
recipient_email = "[email protected]"
send_email_notification(
recipient_email,
"Plant Monitoring System Alert",
"Your plants need watering!"
)