@@ -665,9 +665,6 @@ def _is_accepted_report(payload: dict[str, Any]) -> tuple[bool, str]:
665665 return True , "report exists"
666666 return False , f"unaccepted status={ status or '-' } stage={ stage or '-' } "
667667
668-
669-
670-
671668def _telegram_secret_project () -> str | None :
672669 return (
673670 os .environ .get ("RUNTIME_HEARTBEAT_GCP_PROJECT_ID" )
@@ -677,43 +674,80 @@ def _telegram_secret_project() -> str | None:
677674 )
678675
679676
680- def _load_telegram_token_from_secret () -> str :
681- secret_name = (os .environ .get ("TELEGRAM_TOKEN_SECRET_NAME" ) or "" ).strip ()
677+ def _load_telegram_token_from_secret (secret_env_name : str ) -> str :
678+ secret_name = (os .environ .get (secret_env_name ) or "" ).strip ()
682679 if not secret_name :
683680 return ""
684- command = ["gcloud" , "secrets" , "versions" , "access" , "latest" , "--secret" , secret_name ]
681+ command = [
682+ "gcloud" ,
683+ "secrets" ,
684+ "versions" ,
685+ "access" ,
686+ "latest" ,
687+ "--secret" ,
688+ secret_name ,
689+ ]
685690 project = _telegram_secret_project ()
686691 if project :
687692 command .extend (["--project" , project ])
688693 result = _run_gcloud (command )
689694 if result .returncode != 0 :
690695 detail = (result .stderr or result .stdout or "" ).strip ()
691696 print (
692- f"Unable to read Telegram token from Secret Manager: { detail or 'gcloud failed' } " ,
697+ f"Unable to read Telegram token from { secret_env_name } : "
698+ f"{ detail or 'gcloud failed' } " ,
693699 file = sys .stderr ,
694700 )
695701 return ""
696702 return result .stdout .strip ()
697703
698704
699- def _telegram_token () -> str :
700- direct_token = (os .environ .get ("TELEGRAM_TOKEN" ) or os .environ .get ("TG_TOKEN" ) or "" ).strip ()
701- if direct_token :
702- return direct_token
703- return _load_telegram_token_from_secret ()
705+ def _first_env_value (* names : str ) -> str :
706+ for name in names :
707+ value = (os .environ .get (name ) or "" ).strip ()
708+ if value :
709+ return value
710+ return ""
711+
712+
713+ def _telegram_targets () -> list [tuple [str , str ]]:
714+ strategy_chat_ids = _split_values (
715+ os .environ .get ("STRATEGY_PLUGIN_ALERT_TELEGRAM_CHAT_IDS" )
716+ )
717+ if strategy_chat_ids :
718+ strategy_token = _first_env_value ("STRATEGY_PLUGIN_ALERT_TELEGRAM_BOT_TOKEN" )
719+ if not strategy_token :
720+ strategy_token = _load_telegram_token_from_secret (
721+ "STRATEGY_PLUGIN_ALERT_TELEGRAM_BOT_TOKEN_SECRET_NAME"
722+ )
723+ if strategy_token :
724+ return list (
725+ dict .fromkeys ((strategy_token , chat_id ) for chat_id in strategy_chat_ids )
726+ )
727+ return []
728+
729+ token = _first_env_value ("TELEGRAM_TOKEN" , "TG_TOKEN" )
730+ if not token :
731+ token = _load_telegram_token_from_secret ("TELEGRAM_TOKEN_SECRET_NAME" )
732+ targets = [
733+ (token , chat_id )
734+ for chat_id in _split_values (os .environ .get ("GLOBAL_TELEGRAM_CHAT_ID" ))
735+ if token
736+ ]
737+ return list (dict .fromkeys (targets ))
738+
704739
705740def _send_telegram (message : str ) -> bool :
706- targets : list [tuple [str , str ]] = []
707- token = _telegram_token ()
708- for chat_id in _split_values (os .environ .get ("GLOBAL_TELEGRAM_CHAT_ID" )):
709- if token :
710- targets .append ((token , chat_id ))
711- unique_targets = list (dict .fromkeys (targets ))
741+ unique_targets = _telegram_targets ()
712742 if not unique_targets :
713- print ("No Telegram token/chat configured; unable to send heartbeat alert." , file = sys .stderr )
743+ print (
744+ "No Telegram token/chat configured; unable to send heartbeat alert." ,
745+ file = sys .stderr ,
746+ )
714747 return False
715- base_url = "https://api.telegram.org"
748+
716749 ok = True
750+ base_url = "https://api.telegram.org"
717751 for token_value , chat_id in unique_targets :
718752 body = urllib .parse .urlencode ({"chat_id" : chat_id , "text" : message }).encode ()
719753 request = urllib .request .Request (
@@ -723,7 +757,9 @@ def _send_telegram(message: str) -> bool:
723757 )
724758 try :
725759 with urllib .request .urlopen (request , timeout = 15 ) as response :
726- ok = ok and response .status < 400
760+ if response .status >= 400 :
761+ ok = False
762+ print (f"Telegram returned HTTP { response .status } " , file = sys .stderr )
727763 except Exception as exc : # noqa: BLE001
728764 ok = False
729765 print (f"Telegram send failed: { type (exc ).__name__ } " , file = sys .stderr )
0 commit comments