Repository navigation
Expand file tree
/
Copy pathfix_telegram_parse_error.py
More file actions
103 lines (85 loc) · 3.7 KB
/
Copy pathfix_telegram_parse_error.py
File metadata and controls
103 lines (85 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""
כלי עזר לתיקון בעיות Parse בטלגרם בקבצי ההנדלר.
מודול זה מספק פונקציות עזר לביצוע תיקונים אוטומטיים בקובץ
`github_menu_handler.py`. הייבוא של המודול אינו מבצע שינויים
בפועל; לשם כך יש להריץ את הסקריפט כ־main או לקרוא לפונקציות ישירות.
"""
import re
from typing import List
def clean_for_telegram(text: str) -> str:
"""ניקוי טקסט לשליחה בטלגרם (הסרת תווי Markdown בעייתיים)."""
if not text:
return ""
text = str(text)
replacements = {
"**": "",
"__": "",
"```": "",
"`": "",
"[": "(",
"]": ")",
"_": "-",
"*": "•",
}
for old, new in replacements.items():
text = text.replace(old, new)
return text
def apply_fix(file_path: str = "github_menu_handler.py") -> None:
"""מיישם תיקוני parse_mode ותבניות הודעה בתוך פונקציית ההצגה."""
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
lines: List[str] = content.split("\n")
fixed_lines: List[str] = []
in_function = False
function_indent = 0
for i, line in enumerate(lines):
if "async def show_suggestion_details" in line:
in_function = True
function_indent = len(line) - len(line.lstrip())
fixed_lines.append(line)
continue
if in_function:
if line.strip() and not line.startswith(" " * function_indent) and not line.startswith("\t"):
in_function = False
else:
if "parse_mode=" in line:
if "Markdown" in line or "MARKDOWN" in line:
line = line.replace("'Markdown'", "'HTML'")
line = line.replace('"Markdown"', '"HTML"')
line = line.replace("ParseMode.MARKDOWN", "ParseMode.HTML")
line = line.replace("ParseMode.MARKDOWN_V2", "ParseMode.HTML")
if "message = " in line or "text = " in line:
if "**" in line:
line = line.replace("**", "")
if "```" in line:
line = line.replace("```python", "\n")
line = line.replace("```", "\n")
fixed_lines.append(line)
with open(file_path, "w", encoding="utf-8") as f:
f.write("\n".join(fixed_lines))
def wrap_edit_message_calls(file_path: str = "github_menu_handler.py") -> None:
"""עוטף קריאות edit_message_text ב־try/except ומנקה טקסט בעת שגיאת פרסינג."""
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
pattern = r"(await query\.edit_message_text\([^)]+\))"
def _wrap(match: re.Match) -> str:
call = match.group(1)
return (
"try:\n"
f" {call}\n"
" except telegram.error.BadRequest as e:\n"
" if \"Can't parse entities\" in str(e):\n"
" simple_text = clean_for_telegram(locals().get('message', 'הצעה לשיפור'))\n"
" await query.edit_message_text(simple_text)\n"
" else:\n"
" raise"
)
new_content = re.sub(pattern, _wrap, content)
with open(file_path, "w", encoding="utf-8") as f:
f.write(new_content)
if __name__ == "__main__":
# הרצה ידנית בלבד
apply_fix()
wrap_edit_message_calls()
print("✅ תיקונים הוחלו בהצלחה על github_menu_handler.py")