Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,41 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import subprocess
|
| 3 |
import shutil
|
| 4 |
-
import sys
|
| 5 |
|
| 6 |
home = os.path.expanduser("~")
|
| 7 |
hermes_dir = os.path.join(home, ".hermes")
|
|
@@ -15,28 +49,20 @@ NV_KEY = os.getenv('NVIDIA_API_KEY')
|
|
| 15 |
TG_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
| 16 |
TG_USER = os.getenv('TELEGRAM_ALLOWED_USERS') or os.getenv('TELEGRAM_USER_ID')
|
| 17 |
|
| 18 |
-
# Strict safety check
|
| 19 |
if not TG_TOKEN or not TG_USER:
|
| 20 |
-
print("❌ ERROR: Main Telegram credentials missing
|
| 21 |
sys.exit(1)
|
| 22 |
|
| 23 |
TG_USER_STR = str(TG_USER).strip()
|
| 24 |
|
| 25 |
-
# --- Network Patch for Hugging Face Block ---
|
| 26 |
-
# Ye public proxy URL hai jo api.telegram.org ke block ko bypass karega
|
| 27 |
-
os.environ["LOCAL_BOT_API_URL"] = "https://api.telegram.org/"
|
| 28 |
-
# Agar framework standard env overrides leta hai to hum direct proxy endpoint path set karte hain
|
| 29 |
-
os.environ["TELEGRAM_API_URL"] = "https://telegg.xyz/bor/"
|
| 30 |
-
|
| 31 |
# --- Write .env ---
|
| 32 |
with open(os.path.join(hermes_dir, ".env"), "w") as f:
|
| 33 |
f.write(f"OPENROUTER_API_KEY={OR_KEY}\n")
|
| 34 |
f.write(f"NVIDIA_API_KEY={NV_KEY}\n")
|
| 35 |
f.write(f"TELEGRAM_BOT_TOKEN={TG_TOKEN}\n")
|
| 36 |
f.write(f"TELEGRAM_ALLOWED_USERS={TG_USER_STR}\n")
|
| 37 |
-
f.write(f"TELEGRAM_API_URL=https://telegg.xyz/bor/\n")
|
| 38 |
|
| 39 |
-
# --- Write config.yaml
|
| 40 |
with open(os.path.join(hermes_dir, "config.yaml"), "w") as f:
|
| 41 |
f.write("""model:
|
| 42 |
provider: nvidia
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
# ==========================================
|
| 5 |
+
# MONKEY PATCH: FORCE TELEGRAM PROXY ROUTE
|
| 6 |
+
# ==========================================
|
| 7 |
+
# Yeh script Hermes ke chalne se PEHLE unki underlying HTTP requests ko intercept karegi
|
| 8 |
+
try:
|
| 9 |
+
import urllib3
|
| 10 |
+
origin_url = urllib3.util.url.parse_url
|
| 11 |
+
def patched_parse_url(url):
|
| 12 |
+
if "api.telegram.org" in url:
|
| 13 |
+
url = url.replace("api.telegram.org", "telegg.xyz/bor")
|
| 14 |
+
return origin_url(url)
|
| 15 |
+
urllib3.util.url.parse_url = patched_parse_url
|
| 16 |
+
urllib3.util.parse_url = patched_parse_url
|
| 17 |
+
print("✅ Urllib3 Network Patch Applied.")
|
| 18 |
+
except Exception:
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
import aiohttp
|
| 23 |
+
# Agar aiohttp use ho raha hai toh uske client connection ko hack karenge
|
| 24 |
+
orig_init = aiohttp.ClientSession._request
|
| 25 |
+
async def patched_request(self, method, url, *args, **kwargs):
|
| 26 |
+
if isinstance(url, str) and "api.telegram.org" in url:
|
| 27 |
+
url = url.replace("api.telegram.org", "telegg.xyz/bor")
|
| 28 |
+
elif hasattr(url, 'human_repr') and "api.telegram.org" in url.human_repr():
|
| 29 |
+
import yarl
|
| 30 |
+
url = yarl.URL(url.human_repr().replace("api.telegram.org", "telegg.xyz/bor"))
|
| 31 |
+
return await orig_init(self, method, url, *args, **kwargs)
|
| 32 |
+
aiohttp.ClientSession._request = patched_request
|
| 33 |
+
print("✅ Aiohttp Network Patch Applied.")
|
| 34 |
+
except Exception:
|
| 35 |
+
pass
|
| 36 |
+
|
| 37 |
import subprocess
|
| 38 |
import shutil
|
|
|
|
| 39 |
|
| 40 |
home = os.path.expanduser("~")
|
| 41 |
hermes_dir = os.path.join(home, ".hermes")
|
|
|
|
| 49 |
TG_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
| 50 |
TG_USER = os.getenv('TELEGRAM_ALLOWED_USERS') or os.getenv('TELEGRAM_USER_ID')
|
| 51 |
|
|
|
|
| 52 |
if not TG_TOKEN or not TG_USER:
|
| 53 |
+
print("❌ ERROR: Main Telegram credentials missing!")
|
| 54 |
sys.exit(1)
|
| 55 |
|
| 56 |
TG_USER_STR = str(TG_USER).strip()
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
# --- Write .env ---
|
| 59 |
with open(os.path.join(hermes_dir, ".env"), "w") as f:
|
| 60 |
f.write(f"OPENROUTER_API_KEY={OR_KEY}\n")
|
| 61 |
f.write(f"NVIDIA_API_KEY={NV_KEY}\n")
|
| 62 |
f.write(f"TELEGRAM_BOT_TOKEN={TG_TOKEN}\n")
|
| 63 |
f.write(f"TELEGRAM_ALLOWED_USERS={TG_USER_STR}\n")
|
|
|
|
| 64 |
|
| 65 |
+
# --- Write config.yaml ---
|
| 66 |
with open(os.path.join(hermes_dir, "config.yaml"), "w") as f:
|
| 67 |
f.write("""model:
|
| 68 |
provider: nvidia
|