devalerts 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: devalerts
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Send unhandled Python exceptions straight to a Telegram chat. No backend, no account, no database — just your own bot token.
5
5
  Keywords: telegram,error-tracking,exception-monitoring,alerts,logging,monitoring,asgi
6
6
  Author: sslinNn
@@ -94,6 +94,10 @@ devalerts.init(bot_token="123456:ABC-DEF...", chat_id=123456789)
94
94
  That's it — any unhandled exception (including ones raised in threads) now
95
95
  also lands in your Telegram chat.
96
96
 
97
+ The traceback itself arrives folded into a collapsed quote — the exception
98
+ type, message, and host are visible right away, tap to expand the full
99
+ traceback. Keeps a big stack trace from taking over the chat.
100
+
97
101
  ## Grouping, rate limiting, and the dashboard
98
102
 
99
103
  Exceptions are grouped by fingerprint (exception type + file + line where it
@@ -67,6 +67,10 @@ devalerts.init(bot_token="123456:ABC-DEF...", chat_id=123456789)
67
67
  That's it — any unhandled exception (including ones raised in threads) now
68
68
  also lands in your Telegram chat.
69
69
 
70
+ The traceback itself arrives folded into a collapsed quote — the exception
71
+ type, message, and host are visible right away, tap to expand the full
72
+ traceback. Keeps a big stack trace from taking over the chat.
73
+
70
74
  ## Grouping, rate limiting, and the dashboard
71
75
 
72
76
  Exceptions are grouped by fingerprint (exception type + file + line where it
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "devalerts"
3
- version = "0.2.0"
3
+ version = "0.2.1"
4
4
  description = "Send unhandled Python exceptions straight to a Telegram chat. No backend, no account, no database — just your own bot token."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -9,6 +9,10 @@ import traceback
9
9
  _MAX_MESSAGE_LENGTH = 4096
10
10
 
11
11
 
12
+ def _escape_html(text: str) -> str:
13
+ return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
14
+
15
+
12
16
  def _format_context(tags: dict[str, str] | None) -> str:
13
17
  try:
14
18
  hostname = socket.gethostname()
@@ -27,16 +31,25 @@ def _format_alert(
27
31
  if skipped:
28
32
  header += f"\n⚠️ Повторилась ещё {skipped} раз(а) с последнего алерта"
29
33
  body = "".join(traceback.format_exception(exc_type, exc_value, tb))
30
- message = f"{header}\n\n{body}"
31
- if len(message) <= _MAX_MESSAGE_LENGTH:
32
- return message
33
- marker = "\n\n...(truncated)...\n"
34
- # ponytail: header itself can exceed the limit if exc_value's str() is huge
35
- # (e.g. a validation error echoing a large payload) keep can go negative,
36
- # so clamp it and hard-truncate the final result as a backstop guarantee.
37
- keep = max(_MAX_MESSAGE_LENGTH - len(header) - len(marker), 0)
38
- truncated = f"{header}{marker}{body[-keep:]}" if keep else header
39
- return truncated[:_MAX_MESSAGE_LENGTH]
34
+
35
+ if len(header) + 2 + len(body) > _MAX_MESSAGE_LENGTH:
36
+ marker = "\n\n...(truncated)...\n"
37
+ # ponytail: header itself can exceed the limit if exc_value's str() is huge
38
+ # (e.g. a validation error echoing a large payload) keep can go negative,
39
+ # so clamp it; the header itself gets hard-truncated as a backstop. The "2"
40
+ # accounts for the "\n\n" separator joined in below.
41
+ keep = max(_MAX_MESSAGE_LENGTH - len(header) - 2 - len(marker), 0)
42
+ body = f"{marker}{body[-keep:]}" if keep else ""
43
+ header = header[:_MAX_MESSAGE_LENGTH]
44
+
45
+ # Budgeted above against the plain text -- Telegram's 4096-char limit
46
+ # applies to the parsed (tag-stripped) text, not the raw HTML we send.
47
+ escaped_header = _escape_html(header)
48
+ if not body:
49
+ return escaped_header
50
+ return (
51
+ f"{escaped_header}\n\n<blockquote expandable>{_escape_html(body)}</blockquote>"
52
+ )
40
53
 
41
54
 
42
55
  # ponytail: fixed pattern list, not exhaustive — catches common
@@ -40,7 +40,9 @@ def _log_failed_delivery(text: str) -> None:
40
40
 
41
41
  def _send_telegram_message(bot_token: str, chat_id: int | str, text: str) -> bool:
42
42
  url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
43
- payload = json.dumps({"chat_id": chat_id, "text": text}).encode("utf-8")
43
+ payload = json.dumps(
44
+ {"chat_id": chat_id, "text": text, "parse_mode": "HTML"}
45
+ ).encode("utf-8")
44
46
  last_error: Exception | None = None
45
47
 
46
48
  for attempt in range(_MAX_ATTEMPTS):
File without changes