-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_notifier.py
More file actions
65 lines (56 loc) · 2.43 KB
/
telegram_notifier.py
File metadata and controls
65 lines (56 loc) · 2.43 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
63
64
65
import requests
import os
import sys
from datetime import datetime
# Get the directory of the calling script
caller_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
# Attempt to import Telegram credentials from the caller's directory
try:
# Temporarily add caller's directory to sys.path
sys.path.append(caller_dir)
from telegram_credentials import TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID
except ImportError as e:
print(f"[{datetime.now()}] Failed to import from {caller_dir}/telegram_credentials: {e}")
TELEGRAM_BOT_TOKEN = None
TELEGRAM_CHAT_ID = None
finally:
# Clean up sys.path to avoid side effects
if caller_dir in sys.path:
sys.path.remove(caller_dir)
def send_telegram_alert(message, bot_token=None, chat_id=None):
"""
Send a message to a Telegram chat using a bot.
Args:
message (str): The message to send.
bot_token (str, optional): Telegram bot token. Defaults to TELEGRAM_BOT_TOKEN from telegram_credentials.
chat_id (str, optional): Telegram chat ID. Defaults to TELEGRAM_CHAT_ID from telegram_credentials.
Returns:
bool: True if the message was sent successfully, False otherwise.
"""
# Use provided credentials or fall back to imported ones
bot_token = bot_token or TELEGRAM_BOT_TOKEN
chat_id = chat_id or TELEGRAM_CHAT_ID
# Check if credentials are available
if not bot_token or not chat_id:
print(f"[{datetime.now()}] Telegram alert failed: Missing TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID in {caller_dir}/telegram_credentials.py.")
return False
# Telegram API endpoint
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
# Payload for the POST request
payload = {
"chat_id": chat_id,
"text": message,
"parse_mode": "Markdown" # Optional: for formatting
}
try:
response = requests.post(url, json=payload, timeout=10)
response.raise_for_status() # Raise an exception for 4xx/5xx errors
if response.json().get("ok"):
print(f"[{datetime.now()}] Telegram alert sent successfully: {message}")
return True
else:
print(f"[{datetime.now()}] Telegram alert failed: {response.json().get('description', 'Unknown error')}")
return False
except requests.RequestException as e:
print(f"[{datetime.now()}] Telegram alert failed due to network error: {e}")
return False