Spaces:
Runtime error
Runtime error
Update notifier.py
Browse files- notifier.py +28 -2
notifier.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
from email.mime.text import MIMEText
|
| 5 |
from email.mime.multipart import MIMEMultipart
|
| 6 |
|
|
|
|
| 7 |
EMAIL_USER = os.getenv("EMAIL_USER")
|
| 8 |
EMAIL_PASS = os.getenv("EMAIL_PASS")
|
| 9 |
|
|
@@ -13,13 +14,37 @@ def get_user_ip():
|
|
| 13 |
except requests.RequestException:
|
| 14 |
return "Unavailable"
|
| 15 |
|
| 16 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
if not EMAIL_USER or not EMAIL_PASS:
|
| 18 |
print("[Notifier] Missing EMAIL_USER or EMAIL_PASS.")
|
| 19 |
return
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
subject = "RezAI Used"
|
| 22 |
-
body = f"A new user interacted with RezAi.\n\
|
| 23 |
|
| 24 |
msg = MIMEMultipart()
|
| 25 |
msg["From"] = EMAIL_USER
|
|
@@ -34,3 +59,4 @@ def send_email_notification(ip_address, user_message):
|
|
| 34 |
server.sendmail(EMAIL_USER, EMAIL_USER, msg.as_string())
|
| 35 |
except Exception as e:
|
| 36 |
print(f"[Notifier] Email sending error: {e}")
|
|
|
|
|
|
| 4 |
from email.mime.text import MIMEText
|
| 5 |
from email.mime.multipart import MIMEMultipart
|
| 6 |
|
| 7 |
+
|
| 8 |
EMAIL_USER = os.getenv("EMAIL_USER")
|
| 9 |
EMAIL_PASS = os.getenv("EMAIL_PASS")
|
| 10 |
|
|
|
|
| 14 |
except requests.RequestException:
|
| 15 |
return "Unavailable"
|
| 16 |
|
| 17 |
+
def get_geo_from_ip(ip):
|
| 18 |
+
try:
|
| 19 |
+
response = requests.get(f"http://ip-api.com/json/{ip}", timeout=5)
|
| 20 |
+
data = response.json()
|
| 21 |
+
if data["status"] == "success":
|
| 22 |
+
return {
|
| 23 |
+
"city": data.get("city", "Unknown"),
|
| 24 |
+
"region": data.get("regionName", "Unknown"),
|
| 25 |
+
"country": data.get("country", "Unknown"),
|
| 26 |
+
"error": ""
|
| 27 |
+
}
|
| 28 |
+
else:
|
| 29 |
+
return {"error": "Lookup failed"}
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return {"error": str(e)}
|
| 32 |
+
|
| 33 |
+
def send_email_notification(user_message):
|
| 34 |
if not EMAIL_USER or not EMAIL_PASS:
|
| 35 |
print("[Notifier] Missing EMAIL_USER or EMAIL_PASS.")
|
| 36 |
return
|
| 37 |
|
| 38 |
+
ip_address = _get_user_ip()
|
| 39 |
+
location = _get_geo_from_ip(ip_address)
|
| 40 |
+
|
| 41 |
+
if location["error"]:
|
| 42 |
+
location_info = f"IP: {ip_address} (location lookup failed)"
|
| 43 |
+
else:
|
| 44 |
+
location_info = f"{location['city']}, {location['region']}, {location['country']} (IP: {ip_address})"
|
| 45 |
+
|
| 46 |
subject = "RezAI Used"
|
| 47 |
+
body = f"A new user interacted with RezAi.\n\nLocation: {location_info}\nMessage: {user_message}"
|
| 48 |
|
| 49 |
msg = MIMEMultipart()
|
| 50 |
msg["From"] = EMAIL_USER
|
|
|
|
| 59 |
server.sendmail(EMAIL_USER, EMAIL_USER, msg.as_string())
|
| 60 |
except Exception as e:
|
| 61 |
print(f"[Notifier] Email sending error: {e}")
|
| 62 |
+
|