-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.py
More file actions
62 lines (53 loc) · 2.02 KB
/
Copy pathnotifier.py
File metadata and controls
62 lines (53 loc) · 2.02 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
notifier.py — optional Telegram notifications after each pipeline run.
"""
import logging
import httpx
from config import Config
logger = logging.getLogger(__name__)
class Notifier:
def __init__(self, cfg: Config):
self.cfg = cfg
self.active = bool(cfg.TELEGRAM_BOT_TOKEN and cfg.TELEGRAM_CHAT_ID)
if not self.active:
logger.info("Telegram notifier disabled (no token/chat_id).")
def send(self, top_jobs: list[dict], total_added: int):
if not self.active:
return
try:
lines = [
f"🤖 *Tech Jobs Pipeline*",
f"✅ *{total_added} new listings added*\n",
"🔥 *Top picks:*",
]
for j in top_jobs[:5]:
link = j.get("apply_link", "")
title = _esc(j.get("title", "N/A"))
co = _esc(j.get("company", "N/A"))
cat = _esc(j.get("category", ""))
jtype = _esc(j.get("job_type", ""))
loc = _esc(j.get("location", ""))
line = (
f"• *{title}* @ {co}\n"
f" _{cat}_ · {jtype} · {loc}"
)
if link:
line += f"\n [Apply →]({link})"
lines.append(line)
text = "\n".join(lines)
url = (f"https://api.telegram.org/bot{self.cfg.TELEGRAM_BOT_TOKEN}"
f"/sendMessage")
httpx.post(url, json={
"chat_id": self.cfg.TELEGRAM_CHAT_ID,
"text": text,
"parse_mode": "Markdown",
"disable_web_page_preview": True,
}, timeout=10)
logger.info("Telegram notification sent.")
except Exception as e:
logger.warning(f"Telegram notification failed: {e}")
def _esc(text: str) -> str:
"""Escape Markdown special characters."""
for ch in r"_*[]()~`>#+-=|{}.!":
text = text.replace(ch, f"\\{ch}")
return text