AdityaAdaki
commited on
Commit
·
bbd376b
1
Parent(s):
fae4149
ap
Browse files- .env +2 -1
- app.py +23 -34
- requirements.txt +2 -2
.env
CHANGED
|
@@ -3,4 +3,5 @@ SMTP_SERVER=smtp.gmail.com
|
|
| 3 |
SMTP_PORT=587
|
| 4 |
SMTP_USERNAME=adityaadaki21@gmail.com
|
| 5 |
SMTP_PASSWORD=beqk eefx fmir izxz
|
| 6 |
-
NOTIFICATION_EMAIL=adityaadaki21@gmail.com
|
|
|
|
|
|
| 3 |
SMTP_PORT=587
|
| 4 |
SMTP_USERNAME=adityaadaki21@gmail.com
|
| 5 |
SMTP_PASSWORD=beqk eefx fmir izxz
|
| 6 |
+
NOTIFICATION_EMAIL=adityaadaki21@gmail.com
|
| 7 |
+
TELEGRAM_CHAT_ID=5397241102
|
app.py
CHANGED
|
@@ -4,10 +4,9 @@ import base64
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from werkzeug.utils import secure_filename
|
| 7 |
-
import smtplib
|
| 8 |
-
from email.mime.text import MIMEText
|
| 9 |
-
from email.mime.multipart import MIMEMultipart
|
| 10 |
import re
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Load environment variables
|
| 13 |
load_dotenv()
|
|
@@ -21,40 +20,26 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
| 21 |
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
|
| 22 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
SMTP_PASSWORD = os.getenv('SMTP_PASSWORD')
|
| 29 |
-
NOTIFICATION_EMAIL = os.getenv('NOTIFICATION_EMAIL')
|
| 30 |
|
| 31 |
-
def send_invalid_certificate_notification(cert_name, details):
|
| 32 |
-
"""Send
|
| 33 |
try:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
Details: {details}
|
| 44 |
-
|
| 45 |
-
Please review this certificate as soon as possible.
|
| 46 |
-
"""
|
| 47 |
-
|
| 48 |
-
msg.attach(MIMEText(body, 'plain'))
|
| 49 |
-
|
| 50 |
-
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
|
| 51 |
-
server.starttls()
|
| 52 |
-
server.login(SMTP_USERNAME, SMTP_PASSWORD)
|
| 53 |
-
server.send_message(msg)
|
| 54 |
-
|
| 55 |
return True
|
| 56 |
except Exception as e:
|
| 57 |
-
print(f"Failed to send
|
| 58 |
return False
|
| 59 |
|
| 60 |
def check_validity_from_response(response_text):
|
|
@@ -126,7 +111,11 @@ def process_certificate(image_path, cert_name):
|
|
| 126 |
|
| 127 |
# Check if certificate is invalid
|
| 128 |
if check_validity_from_response(response.text):
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
return response.text
|
| 132 |
|
|
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from werkzeug.utils import secure_filename
|
|
|
|
|
|
|
|
|
|
| 7 |
import re
|
| 8 |
+
import asyncio
|
| 9 |
+
from telegram import Bot
|
| 10 |
|
| 11 |
# Load environment variables
|
| 12 |
load_dotenv()
|
|
|
|
| 20 |
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
|
| 21 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 22 |
|
| 23 |
+
# Telegram Configuration
|
| 24 |
+
TELEGRAM_BOT_TOKEN = "7868898974:AAH2p7sBkj0TH4sD__giOmUt-2ZXn7sg0_c"
|
| 25 |
+
TELEGRAM_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID') # Add this to your .env file
|
| 26 |
+
bot = Bot(token=TELEGRAM_BOT_TOKEN)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
async def send_invalid_certificate_notification(cert_name, details):
|
| 29 |
+
"""Send Telegram notification for invalid certificate."""
|
| 30 |
try:
|
| 31 |
+
message = f"""
|
| 32 |
+
🚨 Invalid Certificate Detected 🚨
|
| 33 |
+
|
| 34 |
+
Certificate Name: {cert_name}
|
| 35 |
+
Details: {details}
|
| 36 |
+
|
| 37 |
+
Please review this certificate as soon as possible.
|
| 38 |
+
"""
|
| 39 |
+
await bot.send_message(chat_id=TELEGRAM_CHAT_ID, text=message, parse_mode='HTML')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return True
|
| 41 |
except Exception as e:
|
| 42 |
+
print(f"Failed to send Telegram notification: {str(e)}")
|
| 43 |
return False
|
| 44 |
|
| 45 |
def check_validity_from_response(response_text):
|
|
|
|
| 111 |
|
| 112 |
# Check if certificate is invalid
|
| 113 |
if check_validity_from_response(response.text):
|
| 114 |
+
# Create event loop and run the async notification
|
| 115 |
+
loop = asyncio.new_event_loop()
|
| 116 |
+
asyncio.set_event_loop(loop)
|
| 117 |
+
loop.run_until_complete(send_invalid_certificate_notification(cert_name, response.text))
|
| 118 |
+
loop.close()
|
| 119 |
|
| 120 |
return response.text
|
| 121 |
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
flask
|
| 2 |
python-dotenv
|
| 3 |
google-generativeai
|
| 4 |
-
|
| 5 |
-
|
|
|
|
| 1 |
flask
|
| 2 |
python-dotenv
|
| 3 |
google-generativeai
|
| 4 |
+
Werkzeug
|
| 5 |
+
python-telegram-bot
|