Spaces:
Paused
Paused
| set -e | |
| echo ">>> [1/4] Patching nanobot and ptb internals for transparent relay..." | |
| NANOBOT_PATH=$(python3 -c "import nanobot, os; print(os.path.dirname(nanobot.__file__))") | |
| echo " nanobot found at: $NANOBOT_PATH" | |
| PATCHED=0 | |
| while IFS= read -r -d '' f; do | |
| if grep -q "api\.telegram\.org" "$f"; then | |
| sed -i 's|https://api\.telegram\.org|https://relay.cryptoisfuture.site|g' "$f" | |
| echo " Patched: $f" | |
| PATCHED=$((PATCHED + 1)) | |
| fi | |
| done < <(find "$NANOBOT_PATH" -name "*.py" -print0) | |
| PTB_PATH=$(python3 -c "import telegram, os; print(os.path.dirname(telegram.__file__))" 2>/dev/null || echo "") | |
| if [ -n "$PTB_PATH" ]; then | |
| while IFS= read -r -d '' f; do | |
| if grep -q "api\.telegram\.org" "$f"; then | |
| sed -i 's|https://api\.telegram\.org|https://relay.cryptoisfuture.site|g' "$f" | |
| echo " Patched (ptb): $f" | |
| PATCHED=$((PATCHED + 1)) | |
| fi | |
| done < <(find "$PTB_PATH" -name "*.py" -print0) | |
| fi | |
| echo " Total files patched: $PATCHED" | |
| echo ">>> [2/4] Injecting secrets from environment into config.json..." | |
| python3 -c " | |
| import json, os | |
| cfg_path = os.path.expanduser('~/.nanobot/config.json') | |
| with open(cfg_path) as f: | |
| cfg = json.load(f) | |
| bot_token = os.environ.get('TELEGRAM_BOT_TOKEN') | |
| if bot_token: | |
| cfg['channels']['telegram']['token'] = bot_token.strip().strip('\"').strip('\'') | |
| allow_from = os.environ.get('TELEGRAM_ALLOW_FROM') | |
| if allow_from: | |
| cfg['channels']['telegram']['allowFrom'] = [x.strip() for x in allow_from.split(',')] | |
| api_key = os.environ.get('LLM_API_KEY') | |
| api_base = os.environ.get('LLM_BASE_URL') | |
| model = os.environ.get('LLM_MODEL') | |
| if api_key: | |
| cfg['providers']['custom']['apiKey'] = api_key.strip().strip('\"').strip('\'') | |
| if api_base: | |
| cfg['providers']['custom']['apiBase'] = api_base.strip().strip('\"').strip('\'') | |
| if model: | |
| cfg['agents']['defaults']['model'] = model.strip().strip('\"').strip('\'') | |
| with open(cfg_path, 'w') as f: | |
| json.dump(cfg, f, indent=2) | |
| print('Config written successfully.') | |
| " | |
| echo ">>> [3/4] Diagnosing Telegram Connection via Transparent Relay..." | |
| python3 -c " | |
| import urllib.request, json, os, urllib.error | |
| cfg_path = os.path.expanduser('~/.nanobot/config.json') | |
| try: | |
| cfg = json.load(open(cfg_path)) | |
| token = cfg['channels']['telegram']['token'] | |
| except Exception as e: | |
| print('Could not read token from config:', e) | |
| exit(0) | |
| # Notice the clean URL structure here | |
| base_url = f'https://relay.cryptoisfuture.site/bot{token}' | |
| print('1. Verifying token via getMe...') | |
| try: | |
| req = urllib.request.Request(f'{base_url}/getMe', headers={'User-Agent': 'Mozilla/5.0'}) | |
| res = urllib.request.urlopen(req, timeout=10) | |
| print(' Success:', res.read().decode('utf-8')) | |
| except urllib.error.HTTPError as e: | |
| print(f' HTTP Error {e.code}:', e.read().decode('utf-8')) | |
| except Exception as e: | |
| print(' Error:', e) | |
| print('2. Forcing Webhook deletion and dropping pending updates...') | |
| try: | |
| req = urllib.request.Request(f'{base_url}/deleteWebhook?drop_pending_updates=True', headers={'User-Agent': 'Mozilla/5.0'}) | |
| res = urllib.request.urlopen(req, timeout=10) | |
| print(' Success:', res.read().decode('utf-8')) | |
| except urllib.error.HTTPError as e: | |
| print(f' HTTP Error {e.code}:', e.read().decode('utf-8')) | |
| except Exception as e: | |
| print(' Error:', e) | |
| " | |
| echo ">>> [4/4] Starting HF health-check server and nanobot gateway..." | |
| python3 -c " | |
| from http.server import HTTPServer, BaseHTTPRequestHandler | |
| class HealthHandler(BaseHTTPRequestHandler): | |
| def do_GET(self): | |
| self.send_response(200) | |
| self.end_headers() | |
| self.wfile.write(b'nanobot is running') | |
| def log_message(self, *args): | |
| pass | |
| print('Health-check server started on port 7860') | |
| HTTPServer(('0.0.0.0', 7860), HealthHandler).serve_forever() | |
| " & | |
| sleep 2 | |
| exec nanobot gateway |