Stockker / src /orchestration /notifications.py
umer6016's picture
Upload folder using huggingface_hub
e56617b verified
import os
import requests
import urllib3
# Suppress InsecureRequestWarning for our DNS bypass
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from dotenv import load_dotenv
load_dotenv()
# Sanitize URL: Remove whitespace and replace PTB/Canary domains with standard domain
WEBHOOK_URL = (os.getenv("WEBHOOK_URL") or "").strip()
WEBHOOK_URL = WEBHOOK_URL.replace("ptb.discord.com", "discord.com").replace("canary.discord.com", "discord.com")
# Hardcoded Discord IPs (Cloudflare) to bypass DNS blocking
DISCORD_IPS = ["162.159.135.232", "162.159.136.232", "162.159.137.232"]
def notify_discord(message: str) -> tuple[bool, str]:
"""Sends a notification to Discord. Returns (Success, status_message)."""
if not WEBHOOK_URL:
msg = "Warning: WEBHOOK_URL not set. Skipping notification."
print(msg)
return False, msg
data = {"content": message}
# 1. Try Standard Request
try:
response = requests.post(WEBHOOK_URL, json=data, timeout=5)
response.raise_for_status()
return True, "Notification sent successfully (Standard DNS)."
except requests.exceptions.RequestException as e:
print(f"Standard DNS failed: {e}")
# 2. Try IP Bypass (DNS Blackhole Workaround)
# Extract path from URL (e.g., /api/webhooks/...)
from urllib.parse import urlparse
parsed = urlparse(WEBHOOK_URL)
path = parsed.path
headers = {"Host": "discord.com"}
for ip in DISCORD_IPS:
try:
# Construct direct IP URL
bypass_url = f"https://{ip}{path}"
print(f"Trying DNS Bypass with IP: {ip}")
# verify=False is needed because cert matches discord.com, not the IP.
# This is acceptable for a notification bot in a restricted env.
response = requests.post(bypass_url, json=data, headers=headers, verify=False, timeout=5)
response.raise_for_status()
return True, f"Notification sent successfully (DNS Bypass: {ip})"
except Exception as bypass_e:
print(f"Bypass {ip} failed: {bypass_e}")
# If all fail
return False, f"All attempts failed. Standard Error: {e}"