Spaces:
Running
on
Zero
Running
on
Zero
Update notifier.py
Browse files- notifier.py +54 -32
notifier.py
CHANGED
|
@@ -8,43 +8,66 @@ from email.mime.multipart import MIMEMultipart
|
|
| 8 |
EMAIL_USER = os.getenv("EMAIL_USER")
|
| 9 |
EMAIL_PASS = os.getenv("EMAIL_PASS")
|
| 10 |
|
| 11 |
-
def get_user_ip():
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 36 |
return
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 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,4 +82,3 @@ def send_email_notification(user_message):
|
|
| 59 |
server.sendmail(EMAIL_USER, EMAIL_USER, msg.as_string())
|
| 60 |
except Exception as e:
|
| 61 |
print(f"[Notifier] Email sending error: {e}")
|
| 62 |
-
|
|
|
|
| 8 |
EMAIL_USER = os.getenv("EMAIL_USER")
|
| 9 |
EMAIL_PASS = os.getenv("EMAIL_PASS")
|
| 10 |
|
| 11 |
+
# def get_user_ip():
|
| 12 |
+
# try:
|
| 13 |
+
# return requests.get("https://api.ipify.org", timeout=2).text
|
| 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
|
| 51 |
+
# msg["To"] = EMAIL_USER
|
| 52 |
+
# msg["Subject"] = subject
|
| 53 |
+
# msg.attach(MIMEText(body, "plain"))
|
| 54 |
+
|
| 55 |
+
# try:
|
| 56 |
+
# with smtplib.SMTP("smtp.gmail.com", 587) as server:
|
| 57 |
+
# server.starttls()
|
| 58 |
+
# server.login(EMAIL_USER, EMAIL_PASS)
|
| 59 |
+
# server.sendmail(EMAIL_USER, EMAIL_USER, msg.as_string())
|
| 60 |
+
# except Exception as e:
|
| 61 |
+
# print(f"[Notifier] Email sending error: {e}")
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def send_email_notification(user_message):
|
| 65 |
if not EMAIL_USER or not EMAIL_PASS:
|
| 66 |
+
print("[Notifier] Missing email credentials.")
|
| 67 |
return
|
| 68 |
|
| 69 |
+
subject = "RezAi Interaction"
|
| 70 |
+
body = f"Someone just sent a message to RezAi:\n\n{user_message}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
msg = MIMEMultipart()
|
| 73 |
msg["From"] = EMAIL_USER
|
|
|
|
| 82 |
server.sendmail(EMAIL_USER, EMAIL_USER, msg.as_string())
|
| 83 |
except Exception as e:
|
| 84 |
print(f"[Notifier] Email sending error: {e}")
|
|
|