scalperBot / services /telegram.py
nexusbert's picture
Upload 47 files
5116a2e verified
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]}...")