-
Notifications
You must be signed in to change notification settings - Fork 308
fix: retry Telegram message without parse_mode on Markdown parsing failure #2073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -27,9 +27,18 @@ def send_message(self, message: str, disable_links_preview: bool = True): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response = requests.post(url, json=message_json) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if response.status_code != 200: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logging.error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"Failed to send telegram message: chat_id - {self.chat_id} reason - {response.reason} {response.text}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logging.warning( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"Failed to send telegram message with Markdown parse_mode: chat_id - {self.chat_id} " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"reason - {response.reason} {response.text}. Retrying without parse_mode." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Retry without parse_mode to handle messages with unescaped Markdown characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message_json.pop("parse_mode", None) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| response = requests.post(url, json=message_json) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing timeout on the retry Ruff S113 flags this. A hanging network call on the retry path can block the calling thread indefinitely. The original call on line 27 is also missing a timeout (pre-existing), but since line 36 is new code this should be addressed here. Consider adding a consistent timeout to both calls. ⏱️ Proposed fix- response = requests.post(url, json=message_json)
+ response = requests.post(url, json=message_json, timeout=10)📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.15.12)[error] 36-36: Probable use of (S113) 🤖 Prompt for AI Agents
Comment on lines
29
to
+36
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if response.status_code != 200: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+38
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logging.error( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f"Failed to send telegram message: chat_id - {self.chat_id} reason - {response.reason} {response.text}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
29
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Retry fires on all non-200 responses, not just Markdown parse failures. The current condition retries without 🛡️ Proposed fix to scope the retry to Markdown parse failures- if response.status_code != 200:
- logging.warning(
- f"Failed to send telegram message with Markdown parse_mode: chat_id - {self.chat_id} "
- f"reason - {response.reason} {response.text}. Retrying without parse_mode."
- )
- # Retry without parse_mode to handle messages with unescaped Markdown characters
- message_json.pop("parse_mode", None)
- response = requests.post(url, json=message_json)
-
- if response.status_code != 200:
- logging.error(
- f"Failed to send telegram message: chat_id - {self.chat_id} reason - {response.reason} {response.text}"
- )
+ if response.status_code != 200:
+ if response.status_code == 400 and "can't parse entities" in response.text.lower():
+ logging.warning(
+ f"Failed to send telegram message with Markdown parse_mode: chat_id - {self.chat_id} "
+ f"reason - {response.reason} {response.text}. Retrying without parse_mode."
+ )
+ # Retry without parse_mode to handle messages with unescaped Markdown characters
+ message_json.pop("parse_mode", None)
+ response = requests.post(url, json=message_json)
+ if response.status_code != 200:
+ logging.error(
+ f"Failed to send telegram message: chat_id - {self.chat_id} reason - {response.reason} {response.text}"
+ )
+ else:
+ logging.error(
+ f"Failed to send telegram message: chat_id - {self.chat_id} reason - {response.reason} {response.text}"
+ )📝 Committable suggestion
Suggested change
🧰 Tools🪛 Ruff (0.15.12)[error] 36-36: Probable use of (S113) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def send_file(self, file_name: str, contents: bytes): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| file_type = "Photo" if is_image(file_name) else "Document" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new fallback retries on every non-200 response, not just Markdown parsing errors. That means rate-limit (
429) and transient/server failures will immediately trigger a secondsendMessagecall with identical payload exceptparse_mode, which can worsen throttling and increase dropped/duplicated alert risk under load; previously those cases made only one request. Gate this retry to parse-related failures (e.g.,400with parse-entity error text) instead of unconditional non-200 responses.Useful? React with 👍 / 👎.