-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegram_notifier.py
More file actions
40 lines (34 loc) · 1.38 KB
/
Copy pathtelegram_notifier.py
File metadata and controls
40 lines (34 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import logging
import requests
logger = logging.getLogger("telegram_notifier")
def send_telegram_lead(config, listing) -> bool:
"""Send a private listing lead to a Telegram chat."""
if not config.telegram_bot_token or not config.telegram_chat_id:
return False
url = f"https://api.telegram.org/bot{config.telegram_bot_token}/sendMessage"
text = (
f"🚨 <b>Новий приватний лід!</b>\n\n"
f"🏠 <b>{listing.title}</b>\n"
f"💰 {listing.price}\n"
f"📍 {listing.location}\n"
f"📏 {listing.size}\n\n"
f"📞 <b>{listing.phone}</b> ({listing.seller_name})\n\n"
f"🔗 <a href='{listing.link}'>Переглянути оголошення</a>"
)
payload = {
"chat_id": config.telegram_chat_id,
"text": text,
"parse_mode": "HTML",
"disable_web_page_preview": True
}
try:
response = requests.post(url, json=payload, timeout=10)
# Avoid crashing if the bot is blocked/not configured right
if not response.ok:
logger.error("Telegram API error: %s", response.text)
return False
logger.info("Successfully sent lead %s to Telegram.", listing.ad_id)
return True
except Exception as e:
logger.error("Failed to send lead %s to Telegram: %s", listing.ad_id, e)
return False