Update app.py
Browse files
app.py
CHANGED
|
@@ -5,15 +5,20 @@ import base64
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
import datetime
|
| 8 |
-
import
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Configuration from environment variables
|
| 11 |
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 12 |
DEFAULT_REPORT_EMAIL = os.getenv("REPORT_EMAIL")
|
| 13 |
-
RESEND_API_KEY = os.getenv("RESEND_API_KEY")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Store conversation history and settings
|
| 19 |
conversation_history = []
|
|
@@ -47,6 +52,35 @@ def update_report_email(new_email):
|
|
| 47 |
else:
|
| 48 |
return "β Please enter a valid email address"
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def analyze_coordination_needs(user_message, department, user_name, image=None):
|
| 51 |
"""Use LLM to analyze coordination needs and suggest actions - with image support"""
|
| 52 |
|
|
@@ -169,7 +203,8 @@ def send_department_email(sender_dept, recipient_dept, subject, message, user_na
|
|
| 169 |
# Determine recipient email
|
| 170 |
recipient_email = departments[recipient_dept]["email"]
|
| 171 |
if not recipient_email:
|
| 172 |
-
|
|
|
|
| 173 |
|
| 174 |
# Create professional email content
|
| 175 |
image_note = "<p style='margin: 5px 0;'><strong>π Attachment:</strong> Image included with request</p>" if image else ""
|
|
@@ -217,24 +252,18 @@ def send_department_email(sender_dept, recipient_dept, subject, message, user_na
|
|
| 217 |
</body>
|
| 218 |
</html>"""
|
| 219 |
|
| 220 |
-
# Send email
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
"subject": f"[{urgency}] {subject}",
|
| 225 |
-
"html": html_content,
|
| 226 |
-
}
|
| 227 |
-
|
| 228 |
-
email = resend.Emails.send(params)
|
| 229 |
-
return f"β
Email sent to {departments[recipient_dept]['name']}"
|
| 230 |
|
| 231 |
except Exception as e:
|
| 232 |
return f"β Failed to send email: {str(e)}"
|
| 233 |
|
| 234 |
def send_email_report(conversation_data):
|
| 235 |
"""Send comprehensive coordination report to administration"""
|
| 236 |
-
if not
|
| 237 |
-
return "Email reporting not configured - missing
|
| 238 |
|
| 239 |
try:
|
| 240 |
# Calculate department activity
|
|
@@ -336,16 +365,9 @@ def send_email_report(conversation_data):
|
|
| 336 |
</body>
|
| 337 |
</html>"""
|
| 338 |
|
| 339 |
-
# Send email
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
"to": [current_report_email],
|
| 343 |
-
"subject": subject,
|
| 344 |
-
"html": html_content,
|
| 345 |
-
}
|
| 346 |
-
|
| 347 |
-
email = resend.Emails.send(params)
|
| 348 |
-
return f"π§ Coordination report sent to {current_report_email}"
|
| 349 |
|
| 350 |
except Exception as e:
|
| 351 |
return f"Failed to send report: {str(e)}"
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
import datetime
|
| 8 |
+
import smtplib
|
| 9 |
+
from email.mime.text import MIMEText
|
| 10 |
+
from email.mime.multipart import MIMEMultipart
|
| 11 |
+
import ssl
|
| 12 |
|
| 13 |
# Configuration from environment variables
|
| 14 |
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 15 |
DEFAULT_REPORT_EMAIL = os.getenv("REPORT_EMAIL")
|
|
|
|
| 16 |
|
| 17 |
+
# Brevo SMTP Configuration
|
| 18 |
+
BREVO_SMTP_SERVER = os.getenv("BREVO_SMTP_SERVER", "smtp-relay.brevo.com")
|
| 19 |
+
BREVO_SMTP_PORT = os.getenv("BREVO_SMTP_PORT", "587")
|
| 20 |
+
BREVO_SMTP_USERNAME = os.getenv("BREVO_SMTP_USERNAME") # Your Brevo login email
|
| 21 |
+
BREVO_SMTP_PASSWORD = os.getenv("BREVO_SMTP_PASSWORD") # Your Brevo SMTP key
|
| 22 |
|
| 23 |
# Store conversation history and settings
|
| 24 |
conversation_history = []
|
|
|
|
| 52 |
else:
|
| 53 |
return "β Please enter a valid email address"
|
| 54 |
|
| 55 |
+
def send_email_smtp(to_email, subject, html_content, from_name="AI Coordination System"):
|
| 56 |
+
"""Send email using Brevo SMTP"""
|
| 57 |
+
try:
|
| 58 |
+
if not BREVO_SMTP_USERNAME or not BREVO_SMTP_PASSWORD:
|
| 59 |
+
return "β Email not configured - missing Brevo SMTP credentials"
|
| 60 |
+
|
| 61 |
+
# Create message
|
| 62 |
+
msg = MIMEMultipart()
|
| 63 |
+
msg['From'] = f"{from_name} <{BREVO_SMTP_USERNAME}>"
|
| 64 |
+
msg['To'] = to_email
|
| 65 |
+
msg['Subject'] = subject
|
| 66 |
+
|
| 67 |
+
# Add HTML content
|
| 68 |
+
msg.attach(MIMEText(html_content, 'html'))
|
| 69 |
+
|
| 70 |
+
# Create connection to Brevo SMTP
|
| 71 |
+
context = ssl.create_default_context()
|
| 72 |
+
|
| 73 |
+
# Connect to Brevo SMTP server
|
| 74 |
+
with smtplib.SMTP(BREVO_SMTP_SERVER, BREVO_SMTP_PORT) as server:
|
| 75 |
+
server.starttls(context=context) # Secure the connection
|
| 76 |
+
server.login(BREVO_SMTP_USERNAME, BREVO_SMTP_PASSWORD)
|
| 77 |
+
server.send_message(msg)
|
| 78 |
+
|
| 79 |
+
return f"β
Email sent to {to_email}"
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"β Failed to send email: {str(e)}"
|
| 83 |
+
|
| 84 |
def analyze_coordination_needs(user_message, department, user_name, image=None):
|
| 85 |
"""Use LLM to analyze coordination needs and suggest actions - with image support"""
|
| 86 |
|
|
|
|
| 203 |
# Determine recipient email
|
| 204 |
recipient_email = departments[recipient_dept]["email"]
|
| 205 |
if not recipient_email:
|
| 206 |
+
# If no department email configured, use the current report email
|
| 207 |
+
recipient_email = current_report_email
|
| 208 |
|
| 209 |
# Create professional email content
|
| 210 |
image_note = "<p style='margin: 5px 0;'><strong>π Attachment:</strong> Image included with request</p>" if image else ""
|
|
|
|
| 252 |
</body>
|
| 253 |
</html>"""
|
| 254 |
|
| 255 |
+
# Send email using Brevo SMTP
|
| 256 |
+
from_name = f"{departments[sender_dept]['name']}"
|
| 257 |
+
email_status = send_email_smtp(recipient_email, f"[{urgency}] {subject}", html_content, from_name)
|
| 258 |
+
return email_status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
except Exception as e:
|
| 261 |
return f"β Failed to send email: {str(e)}"
|
| 262 |
|
| 263 |
def send_email_report(conversation_data):
|
| 264 |
"""Send comprehensive coordination report to administration"""
|
| 265 |
+
if not BREVO_SMTP_USERNAME or not BREVO_SMTP_PASSWORD or not current_report_email:
|
| 266 |
+
return "Email reporting not configured - missing Brevo credentials or report email"
|
| 267 |
|
| 268 |
try:
|
| 269 |
# Calculate department activity
|
|
|
|
| 365 |
</body>
|
| 366 |
</html>"""
|
| 367 |
|
| 368 |
+
# Send email using Brevo SMTP
|
| 369 |
+
email_status = send_email_smtp(current_report_email, subject, html_content, "AI Coordination System")
|
| 370 |
+
return email_status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
except Exception as e:
|
| 373 |
return f"Failed to send report: {str(e)}"
|