Elpida Deploy Bot commited on
Commit
53c947e
Β·
1 Parent(s): e97ba7b

deploy: 73d5382 telegram_listener: log specific HTTP/network failure cause

Browse files
Files changed (1) hide show
  1. elpidaapp/telegram_listener.py +29 -3
elpidaapp/telegram_listener.py CHANGED
@@ -72,7 +72,16 @@ def _save_offset(offset: int) -> None:
72
  # ════════════════════════════════════════════════════════════════════
73
 
74
  def _api(method: str, params: Optional[dict] = None, timeout: int = 30) -> dict:
75
- """Call Telegram Bot API; return parsed JSON or {} on failure."""
 
 
 
 
 
 
 
 
 
76
  if not TELEGRAM_BOT_TOKEN:
77
  return {}
78
  url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
@@ -85,7 +94,19 @@ def _api(method: str, params: Optional[dict] = None, timeout: int = 30) -> dict:
85
  with urlopen(req, timeout=timeout) as resp:
86
  return json.loads(resp.read())
87
  except Exception as e:
88
- logger.debug("Telegram API %s failed: %s", method, e)
 
 
 
 
 
 
 
 
 
 
 
 
89
  return {}
90
 
91
 
@@ -224,8 +245,13 @@ def _run_listener() -> None:
224
  GUEST_CHAMBER_ID,
225
  )
226
  else:
 
 
 
 
227
  logger.warning(
228
- "Telegram getMe failed at startup β€” token may be invalid. Will keep retrying."
 
229
  )
230
 
231
  offset = _load_offset()
 
72
  # ════════════════════════════════════════════════════════════════════
73
 
74
  def _api(method: str, params: Optional[dict] = None, timeout: int = 30) -> dict:
75
+ """Call Telegram Bot API; return parsed JSON or {} on failure.
76
+
77
+ On failure, logs the specific cause at WARNING (was DEBUG previously,
78
+ which made bot-token vs network-reachability vs auth bugs hard to tell
79
+ apart). The error string identifies the failure mode:
80
+ HTTP 401 β†’ token invalid
81
+ HTTP 409 β†’ competing getUpdates consumer (other instance / leftover)
82
+ HTTP 4xx/5xx β†’ other API error
83
+ timeout/DNS/TLS β†’ network reachability from HF Space
84
+ """
85
  if not TELEGRAM_BOT_TOKEN:
86
  return {}
87
  url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
 
94
  with urlopen(req, timeout=timeout) as resp:
95
  return json.loads(resp.read())
96
  except Exception as e:
97
+ # Try to extract HTTP status if present
98
+ from urllib.error import HTTPError, URLError
99
+ if isinstance(e, HTTPError):
100
+ try:
101
+ body = e.read().decode("utf-8", errors="replace")[:300]
102
+ except Exception:
103
+ body = "<unreadable>"
104
+ cause = f"HTTP {e.code} body={body!r}"
105
+ elif isinstance(e, URLError):
106
+ cause = f"URLError reason={e.reason!r}"
107
+ else:
108
+ cause = f"{type(e).__name__}: {e}"
109
+ logger.warning("Telegram API %s failed: %s", method, cause)
110
  return {}
111
 
112
 
 
245
  GUEST_CHAMBER_ID,
246
  )
247
  else:
248
+ # Don't pre-judge the cause β€” the WARNING from _api() above
249
+ # already names the specific failure mode (HTTP 401 = bad token,
250
+ # URLError = network unreachable, etc.). Just record that startup
251
+ # could not verify identity.
252
  logger.warning(
253
+ "Telegram getMe failed at startup β€” see preceding 'Telegram API getMe failed' "
254
+ "warning for specific cause. Listener will keep retrying via getUpdates."
255
  )
256
 
257
  offset = _load_offset()