From d27ee22a8b83b356756ef4af11c49f37173e335e Mon Sep 17 00:00:00 2001 From: Freeman <46896789+soranoo@users.noreply.github.com> Date: Wed, 25 Dec 2024 11:23:17 +0000 Subject: [PATCH] Update src/broadcast.py Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- src/broadcast.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/broadcast.py b/src/broadcast.py index 8f141b9..37f8080 100644 --- a/src/broadcast.py +++ b/src/broadcast.py @@ -56,19 +56,35 @@ def send_webhook(payload:str | dict): continue log.error(f"Sent webhook to {webhook_url} failed, response code: {res.status_code}, Content: {payload}.") -def send_msg_to_tg(payload:str | dict): +def send_msg_to_tg(payload:str | dict) -> None: """ ### Description ### Send a message to the specified Telegram chat. ### Parameters ### - `payload` (str | dict): The content of the message to send + + ### Raises ### + - ValueError: If message exceeds Telegram's length limit """ if isinstance(payload, dict): payload = json.dumps(payload) + if len(payload) > 4096: + raise ValueError("Message exceeds Telegram's 4096 character limit") + payload = json.dumps({"chat_id": f"{tg_chat_id}", "text": f"{payload}"}) - send_post_request(f"https://api.telegram.org/bot{tg_bot_token}/sendMessage", payload, POST_REQUEST_HEADERS, proxies=proxies) + try: + response = send_post_request( + f"https://api.telegram.org/bot{tg_bot_token}/sendMessage", + payload, + POST_REQUEST_HEADERS, + proxies=proxies + ) + if response.status_code >= 400: + log.error(f"Telegram API error: {response.text}") + except Exception as e: + log.error(f"Failed to send Telegram message: {str(e)}") def broadcast(payload:str | dict): """