Spaces:
Paused
Paused
File size: 1,507 Bytes
96e0cc2 5116a2e |
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 |
import os
import requests
from dotenv import load_dotenv
import yaml
load_dotenv()
settings = yaml.safe_load(open("config/settings.yaml"))
BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
def send_telegram(msg):
if not settings["telegram"]["enabled"]:
return
if not BOT_TOKEN or not CHAT_ID:
print("[WARNING] Telegram not configured.")
return
# Check if running in HF Spaces (network restrictions)
import os
hf_env_vars = ['SPACE_ID', 'HF_SPACE_ID', 'HF_SPACE_HOST']
is_hf_space = any(os.getenv(var) for var in hf_env_vars) or 'hf.space' in os.getenv('SPACE_HOST', '')
if is_hf_space:
print("[INFO] Telegram notifications disabled in HF Spaces (network restrictions)")
print(f"[INFO] Would send: {msg[:100]}...")
return
try:
url = f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage"
response = requests.post(url, data={"chat_id": CHAT_ID, "text": msg}, timeout=10)
if response.status_code == 200:
print("[INFO] Telegram message sent successfully")
else:
print(f"[WARNING] Telegram API error: {response.status_code} - {response.text}")
except requests.exceptions.RequestException as e:
print(f"[WARNING] Telegram network error: {e}")
print(f"[INFO] Would send: {msg[:100]}...")
except Exception as e:
print(f"[WARNING] Telegram error: {e}")
print(f"[INFO] Would send: {msg[:100]}...")
|