-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
354 lines (301 loc) · 14.9 KB
/
utils.py
File metadata and controls
354 lines (301 loc) · 14.9 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Standard library imports
import asyncio
import atexit
import functools
import hashlib
import html
import os
import random
import re
import time
import uuid
from contextvars import ContextVar
from typing import Optional
# Third-party imports
import requests
# Frappe framework imports
import frappe
# ─────────────────────────────────────────────────────────────────────────────
# Telegram API Session with Connection Pooling
# ─────────────────────────────────────────────────────────────────────────────
# Reusable session with connection pooling for Telegram API calls
_telegram_session: Optional[requests.Session] = None
def get_telegram_session() -> requests.Session:
"""
Get or create a requests.Session with connection pooling.
Uses a persistent session to reuse TCP connections across API calls.
"""
global _telegram_session
if _telegram_session is None:
_telegram_session = requests.Session()
adapter = requests.adapters.HTTPAdapter(
pool_connections=10,
pool_maxsize=20,
max_retries=3,
pool_block=False
)
_telegram_session.mount("https://", adapter)
_telegram_session.mount("http://", adapter)
return _telegram_session
def close_telegram_session():
"""Close the Telegram session (called on app shutdown via atexit)."""
global _telegram_session
if _telegram_session:
_telegram_session.close()
_telegram_session = None
# PERF-13 FIX: Register atexit handler so the session is closed when the process exits
atexit.register(close_telegram_session)
# ─────────────────────────────────────────────────────────────────────────────
# Structured Logging with Correlation IDs
# ─────────────────────────────────────────────────────────────────────────────
# Context variable to store correlation ID across the request/job lifecycle
_correlation_id_var: ContextVar[str] = ContextVar('correlation_id', default='')
def get_correlation_id() -> str:
"""Get the current correlation ID or generate a new one. Thread-safe and async-safe."""
corr_id = _correlation_id_var.get()
if not corr_id:
corr_id = uuid.uuid4().hex[:12].upper()
_correlation_id_var.set(corr_id)
return corr_id
def set_correlation_id(corr_id: str) -> None:
"""Set a specific correlation ID for the current context."""
_correlation_id_var.set(corr_id)
def clear_correlation_id() -> None:
"""Clear the correlation ID for the current context."""
_correlation_id_var.set('')
def structured_log(level: str, event: str, **context) -> None:
"""Structured logging with correlation ID and arbitrary context."""
corr_id = get_correlation_id()
log_data = {
'correlation_id': corr_id,
'event': event,
**context
}
logger = frappe.logger("bespo_notifications")
log_func = getattr(logger, level.lower(), logger.info)
log_func(f"[{corr_id}] {event}: {log_data}")
def get_log_context(**defaults):
"""Get a context dict with correlation_id pre-populated."""
return {
'correlation_id': get_correlation_id(),
**defaults
}
# ─────────────────────────────────────────────────────────────────────────────
# Centralized URL Normalization
# ─────────────────────────────────────────────────────────────────────────────
def normalize_telegram_api_url(api_url=None):
"""
Normalize Telegram API URL by stripping trailing slashes.
Centralized utility to replace duplicate patterns across codebase.
"""
return (api_url or "https://api.telegram.org/bot").rstrip('/')
# ─────────────────────────────────────────────────────────────────────────────
# Async PDF Generation
# ─────────────────────────────────────────────────────────────────────────────
def generate_pdf_safely_async(doctype, docname, site_name=None):
"""
Async wrapper for PDF generation to prevent blocking in async contexts.
Offloads PDF generation to a thread pool executor.
Returns:
coroutine that resolves to PDF bytes or None on failure
"""
import concurrent.futures
def _generate():
return generate_pdf_safely(doctype, docname, site_name)
loop = asyncio.new_event_loop()
executor = concurrent.futures.ThreadPoolExecutor(max_workers=2)
try:
future = executor.submit(_generate)
return loop.run_in_executor(executor, _generate)
finally:
executor.shutdown(wait=False)
def generate_pdf_safely(doctype, docname, site_name=None):
"""
Generate PDF for a document safely. Synchronous version.
Returns:
PDF bytes or None on failure
"""
if not doctype or not docname:
return None
try:
if site_name:
frappe.init(site_name)
frappe.connect()
base_url = frappe.utils.get_url()
pdf_url = (
f"{base_url}/api/method/frappe.utils.print_format.download_pdf"
f"?doctype={doctype}&name={docname}&format=Standard&no_letterhead=0"
)
frappe.local.session_obj = frappe.session
sid = frappe.session.get("sid") or frappe.db.get_value(
"Session Default", {"default": "Website"}, "user"
)
headers = {}
if frappe.session:
headers["Cookie"] = f"sid={frappe.session.sid}" if hasattr(frappe.session, "sid") else ""
# Use pooled session for PDF generation
session = get_telegram_session()
response = session.get(pdf_url, headers=headers, timeout=30)
if response.ok and response.content.startswith(b"%PDF"):
return response.content
else:
frappe.logger("bespo_notifications").warning(
f"PDF generation failed for {doctype}/{docname}: status={response.status_code}"
)
return None
except Exception as e:
frappe.logger("bespo_notifications").error(
f"Error generating PDF for {doctype}/{docname}: {e}"
)
return None
def run_async_safely(coro):
"""
Safely executes an asynchronous coroutine within Frappe's synchronous
RQ worker threads, preventing 'Event loop is closed' errors.
"""
try:
loop = asyncio.get_event_loop()
if loop.is_closed():
raise RuntimeError
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(coro)
finally:
pending = asyncio.all_tasks(loop)
for task in pending:
task.cancel()
def sanitize_telegram_html(text):
"""
Strips unsupported HTML tags from a string before sending to Telegram
(Parse Mode: HTML). Converts block tags to newlines and removes unsupported tags.
"""
if not text:
return ""
# 1. Replace block-level tags with newlines
text = re.sub(r'</?(p|div|br\s*/?)>', '\n', text, flags=re.IGNORECASE)
# 2. Strip unsupported tags but keep valid ones
valid_tags = ['b', 'strong', 'i', 'em', 'u', 'ins', 's', 'strike', 'del', 'a', 'code', 'pre', 'blockquote']
def replace_tag(match):
full_tag = match.group(0)
tag_name = match.group(1).lower()
if tag_name in valid_tags:
return full_tag
return ""
text = re.sub(r'</?([a-zA-Z0-9]+)[^>]*>', replace_tag, text)
# 3. Clean up excessive newlines
text = re.sub(r'\n{3,}', '\n\n', text).strip()
return text
def clean_jinja_template(template_html):
"""Removes HTML tags accidentally inserted inside Jinja tags."""
if not template_html:
return ""
def replacer(match):
content = match.group(0)
cleaned = re.sub(r'<[^>]+>', '', content)
return html.unescape(cleaned)
template_html = re.sub(r'\{\{.*?(?:\}\}|$)', replacer, template_html, flags=re.DOTALL)
template_html = re.sub(r'\{%.*?(?:%\}|$)', replacer, template_html, flags=re.DOTALL)
return template_html
def _debug_log(msg):
frappe.logger("bespo_notifications").debug(msg)
def _get_proxies(bot_name):
if not bot_name: return None
try:
data = frappe.db.get_value(
"Telegram Bot", bot_name,
["use_proxy", "http_proxy", "https_proxy"],
as_dict=True, ignore_permissions=True
)
if not data or not data.use_proxy: return None
proxies = {}
if data.http_proxy: proxies["http"] = data.http_proxy
if data.https_proxy: proxies["https"] = data.https_proxy
return proxies if proxies else None
except Exception:
return None
def _tg_reply(bot_token, chat_id, text, parse_mode="HTML", bot_api_url=None, bot_name=None):
"""Send a reply message to Telegram. Best-effort - failures are logged as warnings."""
base_url = (bot_api_url or "https://api.telegram.org/bot").rstrip('/')
proxies = _get_proxies(bot_name)
try:
# Use pooled session
session = get_telegram_session()
session.post(
f"{base_url}{bot_token}/sendMessage",
json={"chat_id": chat_id, "text": text, "parse_mode": parse_mode},
timeout=5,
proxies=proxies
)
except Exception as e:
frappe.logger("bespo_notifications").warning(f"Failed to send Telegram reply to {chat_id}: {e}")
def _send_telegram_document(bot_token, chat_id, file_content, filename, caption=None, parse_mode="HTML", bot_api_url=None, bot_name=None, message_thread_id=None):
base_url = (bot_api_url or "https://api.telegram.org/bot").rstrip('/')
proxies = _get_proxies(bot_name)
try:
url = f"{base_url}{bot_token}/sendDocument"
files = {'document': (filename, file_content, 'application/pdf')}
data = {'chat_id': chat_id, 'caption': caption, 'parse_mode': parse_mode}
if message_thread_id:
try:
data['message_thread_id'] = int(message_thread_id)
except ValueError:
frappe.logger("bespo_notifications").warning(f"Invalid message_thread_id: {message_thread_id}")
# Use pooled session
session = get_telegram_session()
resp = session.post(url, data=data, files=files, timeout=20, proxies=proxies)
return resp.json()
except Exception as e:
frappe.logger("bespo_notifications").error(f"Error sending Telegram document: {e}")
return {"ok": False, "description": str(e)}
def _process_telegram_update(bot_name, bot_token, update):
try:
cq = update.get("callback_query")
if cq:
bot_api_url = (frappe.db.get_value("Telegram Bot", bot_name, "bot_api_url") or "https://api.telegram.org/bot").rstrip('/')
_handle_workflow_callback(bot_name, bot_token, cq, bot_api_url=bot_api_url)
return True
my_chat_member = update.get("my_chat_member")
if my_chat_member:
chat_info = my_chat_member.get("chat", {})
new_status = my_chat_member.get("new_chat_member", {}).get("status")
chat_id = str(chat_info.get("id"))
chat_title = chat_info.get("title") or chat_info.get("first_name", "Unknown Chat")
chat_type = chat_info.get("type")
erp_chat_type = {"private": "Private User", "group": "Group", "supergroup": "Supergroup", "channel": "Channel"}.get(chat_type, "Group")
if new_status in ["member", "administrator"]:
existing_group = frappe.db.get_value("Telegram Chat Profile", {"telegram_chat_id": chat_id, "linked_bot": bot_name}, "name")
if not existing_group:
doc = frappe.new_doc("Telegram Chat Profile")
doc.chat_name = f"{chat_title} ({chat_id})"
doc.telegram_chat_id = chat_id
doc.chat_type = erp_chat_type
doc.linked_bot = bot_name
if erp_chat_type == "Supergroup": doc.topics_enabled = 1
try:
doc.insert(ignore_permissions=True)
bot_api_url = (frappe.db.get_value("Telegram Bot", bot_name, "bot_api_url") or "https://api.telegram.org/bot").rstrip('/')
_tg_reply(bot_token, chat_id, f"✅ Bespo QMS Integration Active (Bot: {bot_name}).\nThis chat has been registered as: *{doc.name}*.", parse_mode="Markdown", bot_api_url=bot_api_url)
except Exception as e:
frappe.logger("bespo_notifications").error(f"Error registering new chat profile {chat_id}: {e}")
return True
message = update.get("message") or update.get("channel_post") or {}
text = (message.get("text") or "").strip()
chat_info = message.get("chat", {})
chat_id = str(chat_info.get("id", ""))
chat_type = chat_info.get("type", "private")
if not chat_id: return False
if not text:
if chat_type == "private" and any(k in message for k in ["photo", "document", "video", "voice", "audio"]):
bot_api_url = (frappe.db.get_value("Telegram Bot", bot_name, "bot_api_url") or "https://api.telegram.org/bot").rstrip('/')
_tg_reply(bot_token, chat_id, "I am a notification bot and cannot process media attachments. Please log into the ERP to upload files.", bot_api_url=bot_api_url, bot_name=bot_name)
return False
if text.lower() in ["/start", "/menu", "menu"]:
bot_api_url = (frappe.db.get_value("Telegram Bot", bot_name, "bot_api_url") or "https://api.telegram.org/bot").rstrip('/')
_handle_telegram_menu(bot_token, chat_id, message, bot_api_url=bot_api_url, bot_name=bot_name)
return True
if text.lower().startswith("/verify"):
parts = text.split()
if len(parts) < 2:
bot_api_url = (frappe.db.get_value("Telegram Bot", bot_name, "bot_api_url") or "https://api.telegram.org/bot").rstrip('/')