frank0957 commited on
Commit
4f4b4b2
·
verified ·
1 Parent(s): 6e86ffb

Update arbitrage_launcher.py

Browse files
Files changed (1) hide show
  1. arbitrage_launcher.py +15 -7
arbitrage_launcher.py CHANGED
@@ -1,7 +1,7 @@
1
  # arbitrage_launcher.py
2
  # Wrapper that automatically disables the arbitrage bot when the CLOB API
3
  # is unreachable and re‑enables it once the API recovers.
4
- # Sends a Telegram notification on each state change.
5
  # No manual intervention needed – just replace the line in startup.sh.
6
  # All comments in English.
7
 
@@ -19,13 +19,15 @@ TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID")
19
  def send_telegram(text: str):
20
  """Send a message via Telegram Bot API. Fails silently if not configured."""
21
  if not TELEGRAM_TOKEN or not TELEGRAM_CHAT_ID:
22
- return
 
23
  url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
24
  data = urllib.parse.urlencode({"chat_id": TELEGRAM_CHAT_ID, "text": text}).encode()
25
  try:
26
  urllib.request.urlopen(urllib.request.Request(url, data=data, method="POST"), timeout=10)
 
27
  except:
28
- pass
29
 
30
  # ── State file to avoid duplicate alerts ─────────────
31
  STATE_FILE = "/app/arbitrage_clob_state.txt"
@@ -55,13 +57,20 @@ while True:
55
  # API is reachable
56
  if get_last_state() != "up":
57
  print("[Arbitrage] CLOB API is reachable. Enabling arbitrage bot...", flush=True)
58
- send_telegram("✅ CLOB API is now reachable. Arbitrage bot re‑enabled.")
 
 
 
59
  set_state("up")
60
  break
61
  except Exception as e:
62
  if get_last_state() != "down":
63
- print(f"[Arbitrage] CLOB API unreachable. Bot disabled. ({e})", flush=True)
64
- send_telegram(f"⚠️ Arbitrage bot disabled – CLOB API unreachable.\nError: {e}")
 
 
 
 
65
  set_state("down")
66
  time.sleep(300)
67
 
@@ -72,6 +81,5 @@ try:
72
  except Exception as e:
73
  print(f"[Arbitrage] Bot crashed: {e}", flush=True)
74
  traceback.print_exc()
75
- # Reset state to "down" so the next restart will re‑notify if needed
76
  set_state("down")
77
  sys.exit(1)
 
1
  # arbitrage_launcher.py
2
  # Wrapper that automatically disables the arbitrage bot when the CLOB API
3
  # is unreachable and re‑enables it once the API recovers.
4
+ # Sends a Telegram notification on each state change (with log confirmation).
5
  # No manual intervention needed – just replace the line in startup.sh.
6
  # All comments in English.
7
 
 
19
  def send_telegram(text: str):
20
  """Send a message via Telegram Bot API. Fails silently if not configured."""
21
  if not TELEGRAM_TOKEN or not TELEGRAM_CHAT_ID:
22
+ print("[Arbitrage] Telegram not configured – cannot send alert.", flush=True)
23
+ return False
24
  url = f"https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendMessage"
25
  data = urllib.parse.urlencode({"chat_id": TELEGRAM_CHAT_ID, "text": text}).encode()
26
  try:
27
  urllib.request.urlopen(urllib.request.Request(url, data=data, method="POST"), timeout=10)
28
+ return True
29
  except:
30
+ return False
31
 
32
  # ── State file to avoid duplicate alerts ─────────────
33
  STATE_FILE = "/app/arbitrage_clob_state.txt"
 
57
  # API is reachable
58
  if get_last_state() != "up":
59
  print("[Arbitrage] CLOB API is reachable. Enabling arbitrage bot...", flush=True)
60
+ if send_telegram("✅ CLOB API is now reachable. Arbitrage bot re‑enabled."):
61
+ print("[Arbitrage] Telegram alert sent (re‑enabled).", flush=True)
62
+ else:
63
+ print("[Arbitrage] Telegram alert failed to send.", flush=True)
64
  set_state("up")
65
  break
66
  except Exception as e:
67
  if get_last_state() != "down":
68
+ msg = f"⚠️ Arbitrage bot disabled – CLOB API unreachable.\nError: {e}"
69
+ print(f"[Arbitrage] {msg}", flush=True)
70
+ if send_telegram(msg):
71
+ print("[Arbitrage] Telegram alert sent (disabled).", flush=True)
72
+ else:
73
+ print("[Arbitrage] Telegram alert failed to send.", flush=True)
74
  set_state("down")
75
  time.sleep(300)
76
 
 
81
  except Exception as e:
82
  print(f"[Arbitrage] Bot crashed: {e}", flush=True)
83
  traceback.print_exc()
 
84
  set_state("down")
85
  sys.exit(1)