-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathalert.py
97 lines (81 loc) · 2.73 KB
/
alert.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import imghdr
import os
import smtplib
import sys
from datetime import datetime
from email.message import EmailMessage
from telegram import Bot
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
import FlaskApp.database as database
db = database.Database("setting")
class Alert:
def sendMessage(self, receiver, subject, message, imagePath=None):
for data in db.get_multiple_data():
if "smtp" not in data:
smtpArray = []
else:
smtpArray = data["smtp"]
if len(smtpArray) == 0:
return "1"
for smtp in smtpArray:
EMAIL_SERVER = smtp["smtp_server"]
EMAIL_ADDRESS = smtp["smtp_address"]
EMAIL_PASSWORD = smtp["smtp_password"]
msg = EmailMessage()
msg["Subject"] = subject
msg["From"] = EMAIL_ADDRESS
msg["To"] = receiver
msg.set_content(message)
try:
if imagePath is not None:
with open(imagePath, "rb") as f:
file_data = f.read()
file_type = imghdr.what(f.name)
msg.add_attachment(
file_data, maintype="image", subtype=file_type, filename="Website image"
)
with smtplib.SMTP_SSL(EMAIL_SERVER, 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
smtp.close()
except smtplib.SMTPException as e:
print(e)
def sendBot(self, url, img_path):
for data in db.get_multiple_data():
if "telegram" not in data:
telegramArray = []
else:
telegramArray = data["telegram"]
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
if len(telegramArray) == 0:
return "1"
for telegram in telegramArray:
CHAT_ID = telegram["chat_id"]
TOKEN = telegram["token"]
bot = Bot(TOKEN)
try:
bot.sendPhoto(
CHAT_ID,
photo=open(img_path, "rb"),
caption="⚠️"
+ "Website "
+ url
+ " was defaced!\n"
+ "At "
+ current_time,
)
except:
print("Looks like CHAT_ID or TOKEN of telegram-bot was wrong!")
def getBotInfo(self, CHAT_ID, TOKEN):
try:
bot1 = Bot(TOKEN)
first_name = bot1.getMe().first_name
title = bot1.getChat(CHAT_ID).title
return first_name, title
except:
return "ERROR"
# alert = Alert()
# print(alert.getBotInfo())