feat: geo lookup via ip-api.com in contact form, city/region in Telegram
This commit is contained in:
+16
-1
@@ -821,12 +821,27 @@ def contact():
|
|||||||
if not _re.match(r"^[\+\d][\d\s\-\(\)]{6,18}$", phone):
|
if not _re.match(r"^[\+\d][\d\s\-\(\)]{6,18}$", phone):
|
||||||
return jsonify({"detail": "Некорректный номер телефона"}), 422
|
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
|
divider = "━" * 22
|
||||||
msg = (
|
msg = (
|
||||||
f"🔔 *Сообщение через форму*\n{divider}\n\n"
|
f"🔔 *Сообщение через форму*\n{divider}\n\n"
|
||||||
f"👤 *Имя:* {name}\n"
|
f"👤 *Имя:* {name}\n"
|
||||||
f"📧 *Email:* {email}\n"
|
f"📧 *Email:* {email}\n"
|
||||||
f"📱 *Телефон:* {phone}\n\n"
|
f"📱 *Телефон:* {phone}\n"
|
||||||
|
f"{geo_line}\n"
|
||||||
f"💬 *Сообщение:*\n{text}"
|
f"💬 *Сообщение:*\n{text}"
|
||||||
)
|
)
|
||||||
payload = json.dumps({
|
payload = json.dumps({
|
||||||
|
|||||||
Reference in New Issue
Block a user