feat: geo lookup via ip-api.com in contact form, city/region in Telegram

This commit is contained in:
2026-05-14 10:17:07 +03:00
parent c603e5d30f
commit c80492315a
+16 -1
View File
@@ -821,12 +821,27 @@ def contact():
if not _re.match(r"^[\+\d][\d\s\-\(\)]{6,18}$", phone):
return jsonify({"detail": "Некорректный номер телефона"}), 422
# Geo lookup
ip = request.remote_addr or ""
geo_line = ""
try:
geo_url = f"http://ip-api.com/json/{ip}?lang=ru&fields=status,country,regionName,city,query"
geo_req = Request(geo_url, headers={"User-Agent": "Mozilla/5.0"})
with urlopen(geo_req, timeout=5) as geo_resp:
geo = json.loads(geo_resp.read())
if geo.get("status") == "success":
parts = [p for p in [geo.get("country"), geo.get("regionName"), geo.get("city")] if p]
geo_line = f"🌍 *Местоположение:* {', '.join(parts)}\n🖥 *IP:* {geo.get('query', ip)}\n"
except Exception:
geo_line = f"🖥 *IP:* {ip}\n" if ip else ""
divider = "" * 22
msg = (
f"🔔 *Сообщение через форму*\n{divider}\n\n"
f"👤 *Имя:* {name}\n"
f"📧 *Email:* {email}\n"
f"📱 *Телефон:* {phone}\n\n"
f"📱 *Телефон:* {phone}\n"
f"{geo_line}\n"
f"💬 *Сообщение:*\n{text}"
)
payload = json.dumps({