@@ -288,6 +288,9 @@ def _is_accepted_report(payload: dict[str, Any]) -> tuple[bool, str]:
288288 return True , "report exists"
289289 return False , f"unaccepted status={ status or '-' } stage={ stage or '-' } "
290290
291+
292+
293+
291294def _telegram_secret_project () -> str | None :
292295 return (
293296 os .environ .get ("RUNTIME_HEARTBEAT_GCP_PROJECT_ID" )
@@ -297,80 +300,43 @@ def _telegram_secret_project() -> str | None:
297300 )
298301
299302
300- def _load_telegram_token_from_secret (secret_env_name : str ) -> str :
301- secret_name = (os .environ .get (secret_env_name ) or "" ).strip ()
303+ def _load_telegram_token_from_secret () -> str :
304+ secret_name = (os .environ .get ("TELEGRAM_TOKEN_SECRET_NAME" ) or "" ).strip ()
302305 if not secret_name :
303306 return ""
304- command = [
305- "gcloud" ,
306- "secrets" ,
307- "versions" ,
308- "access" ,
309- "latest" ,
310- "--secret" ,
311- secret_name ,
312- ]
307+ command = ["gcloud" , "secrets" , "versions" , "access" , "latest" , "--secret" , secret_name ]
313308 project = _telegram_secret_project ()
314309 if project :
315310 command .extend (["--project" , project ])
316311 result = _run_gcloud (command )
317312 if result .returncode != 0 :
318313 detail = (result .stderr or result .stdout or "" ).strip ()
319314 print (
320- f"Unable to read Telegram token from { secret_env_name } : "
321- f"{ detail or 'gcloud failed' } " ,
315+ f"Unable to read Telegram token from Secret Manager: { detail or 'gcloud failed' } " ,
322316 file = sys .stderr ,
323317 )
324318 return ""
325319 return result .stdout .strip ()
326320
327321
328- def _first_env_value (* names : str ) -> str :
329- for name in names :
330- value = (os .environ .get (name ) or "" ).strip ()
331- if value :
332- return value
333- return ""
334-
335-
336- def _telegram_targets () -> list [tuple [str , str ]]:
337- strategy_chat_ids = _split_values (
338- os .environ .get ("STRATEGY_PLUGIN_ALERT_TELEGRAM_CHAT_IDS" )
339- )
340- if strategy_chat_ids :
341- strategy_token = _first_env_value ("STRATEGY_PLUGIN_ALERT_TELEGRAM_BOT_TOKEN" )
342- if not strategy_token :
343- strategy_token = _load_telegram_token_from_secret (
344- "STRATEGY_PLUGIN_ALERT_TELEGRAM_BOT_TOKEN_SECRET_NAME"
345- )
346- if strategy_token :
347- return list (
348- dict .fromkeys ((strategy_token , chat_id ) for chat_id in strategy_chat_ids )
349- )
350- return []
351-
352- token = _first_env_value ("TELEGRAM_TOKEN" , "TG_TOKEN" )
353- if not token :
354- token = _load_telegram_token_from_secret ("TELEGRAM_TOKEN_SECRET_NAME" )
355- targets = [
356- (token , chat_id )
357- for chat_id in _split_values (os .environ .get ("GLOBAL_TELEGRAM_CHAT_ID" ))
358- if token
359- ]
360- return list (dict .fromkeys (targets ))
361-
322+ def _telegram_token () -> str :
323+ direct_token = (os .environ .get ("TELEGRAM_TOKEN" ) or os .environ .get ("TG_TOKEN" ) or "" ).strip ()
324+ if direct_token :
325+ return direct_token
326+ return _load_telegram_token_from_secret ()
362327
363328def _send_telegram (message : str ) -> bool :
364- unique_targets = _telegram_targets ()
329+ targets : list [tuple [str , str ]] = []
330+ token = _telegram_token ()
331+ for chat_id in _split_values (os .environ .get ("GLOBAL_TELEGRAM_CHAT_ID" )):
332+ if token :
333+ targets .append ((token , chat_id ))
334+ unique_targets = list (dict .fromkeys (targets ))
365335 if not unique_targets :
366- print (
367- "No Telegram token/chat configured; unable to send heartbeat alert." ,
368- file = sys .stderr ,
369- )
336+ print ("No Telegram token/chat configured; unable to send heartbeat alert." , file = sys .stderr )
370337 return False
371-
372- ok = True
373338 base_url = "https://api.telegram.org"
339+ ok = True
374340 for token_value , chat_id in unique_targets :
375341 body = urllib .parse .urlencode ({"chat_id" : chat_id , "text" : message }).encode ()
376342 request = urllib .request .Request (
@@ -380,9 +346,7 @@ def _send_telegram(message: str) -> bool:
380346 )
381347 try :
382348 with urllib .request .urlopen (request , timeout = 15 ) as response :
383- if response .status >= 400 :
384- ok = False
385- print (f"Telegram returned HTTP { response .status } " , file = sys .stderr )
349+ ok = ok and response .status < 400
386350 except Exception as exc : # noqa: BLE001
387351 ok = False
388352 print (f"Telegram send failed: { type (exc ).__name__ } " , file = sys .stderr )
0 commit comments