-
Notifications
You must be signed in to change notification settings - Fork 314
fix(telegram): escape MarkdownV2 to stop 'can't parse entities' (#1982) #2105
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
Open
arun2dot0
wants to merge
12
commits into
robusta-dev:master
Choose a base branch
from
arun2dot0:fix/telegram-markdownv2-escaping
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1fa2811
feat(telegram): add MarkdownV2 escape helpers (#1982)
arun2dot0 7caaf30
feat(telegram): render typed blocks as MarkdownV2 (#1982)
arun2dot0 c6d083f
feat(telegram): convert MarkdownBlock inline markup to MarkdownV2 (#1…
arun2dot0 1c74cff
feat(telegram): add configurable parse_mode sink param (#1982)
arun2dot0 c1d1617
feat(telegram): thread parse_mode through client, omit when plain (#1…
arun2dot0 216774f
fix(telegram): render messages via MarkdownV2 transformer (#1982)
arun2dot0 e41e0b8
docs(telegram): document parse_mode option (#1982)
arun2dot0 b8fc49f
style(telegram): apply black/isort formatting (#1982)
arun2dot0 b05b6f4
fix(telegram): escape URLs in MarkdownV2 links, drop unused batch met…
arun2dot0 021b18a
docs(telegram): mark the MarkdownV2 converter extension point (#1982)
arun2dot0 8ac36d0
fix(telegram): add request timeouts and accumulate action links (#1982)
arun2dot0 84ba434
fix(telegram): preserve github-style **bold** in inline conversion (#…
arun2dot0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/robusta/core/sinks/telegram/telegram_transformer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import logging | ||
| import re | ||
| from typing import Optional | ||
|
|
||
| from robusta.core.reporting.base import BaseBlock | ||
| from robusta.core.reporting.blocks import DividerBlock, HeaderBlock, JsonBlock, KubernetesDiffBlock, MarkdownBlock | ||
|
|
||
| MARKDOWNV2 = "MarkdownV2" | ||
| _DIVIDER = "-------------------" | ||
|
|
||
| _MARKDOWNV2_SPECIAL_CHARS = r"_*[]()~`>#+-=|{}.!" | ||
| _MARKDOWNV2_ESCAPE_RE = re.compile("([" + re.escape(_MARKDOWNV2_SPECIAL_CHARS) + "])") | ||
|
|
||
| # inline token: slack link <url|text> | github link [text](url) | code `...` | bold *...* | ||
| # Extension point: to preserve more MarkdownV2 constructs in body text (italic _..._, | ||
| # strikethrough ~...~, spoiler ||...||), add an alternative group here and a matching | ||
| # branch in _inline_to_markdownv2. The escape char set itself is fixed by Telegram's | ||
| # MarkdownV2 spec and lives in _MARKDOWNV2_SPECIAL_CHARS. | ||
| _INLINE_RE = re.compile( | ||
| r"<(?P<slack_url>[^|>]+)\|(?P<slack_text>[^>]+)>" | ||
| r"|\[(?P<gh_text>[^\]]+)\]\((?P<gh_url>[^)]+)\)" | ||
| r"|`(?P<code>[^`]+)`" | ||
| r"|\*\*(?P<bold2>[^*]+)\*\*" # github-style **bold** — must precede the single-* alternative | ||
| r"|\*(?P<bold>[^*]+)\*" | ||
| ) | ||
|
|
||
|
|
||
| def escape_markdownv2(text: str) -> str: | ||
| """Escape all Telegram MarkdownV2 special characters in arbitrary text.""" | ||
| return _MARKDOWNV2_ESCAPE_RE.sub(r"\\\1", text) | ||
|
|
||
|
|
||
| def escape_markdownv2_code(text: str) -> str: | ||
| """Escape content placed inside a MarkdownV2 code/pre span (only ` and \\).""" | ||
| return text.replace("\\", "\\\\").replace("`", "\\`") | ||
|
|
||
|
|
||
| def escape_markdownv2_url(url: str) -> str: | ||
| """Escape content placed inside the (...) of a MarkdownV2 inline link (only ) and \\).""" | ||
| return url.replace("\\", "\\\\").replace(")", "\\)") | ||
|
|
||
|
|
||
| class TelegramTransformer: | ||
| """Render Robusta blocks/values as Telegram MarkdownV2 (or plain text when parse_mode is None).""" | ||
|
|
||
| def __init__(self, parse_mode: Optional[str] = MARKDOWNV2): | ||
| self.parse_mode = parse_mode | ||
| self.markdown = parse_mode == MARKDOWNV2 | ||
|
|
||
| def escape(self, text: str) -> str: | ||
| return escape_markdownv2(text) if self.markdown else text | ||
|
|
||
| def bold(self, text: str) -> str: | ||
| return f"*{escape_markdownv2(text)}*" if self.markdown else text | ||
|
|
||
| def code(self, text: str) -> str: | ||
| return f"`{escape_markdownv2_code(text)}`" if self.markdown else text | ||
|
|
||
| def link(self, text: str, url: str) -> str: | ||
| if self.markdown: | ||
| return f"[{escape_markdownv2(text)}]({escape_markdownv2_url(url)})" | ||
| return f"{text} ({url})" | ||
|
|
||
| def block_to_markdownv2(self, block: BaseBlock) -> str: | ||
| if isinstance(block, MarkdownBlock): | ||
| return self._inline_to_markdownv2(block.text) if block.text else "" | ||
| elif isinstance(block, HeaderBlock): | ||
| return self.bold(block.text) | ||
| elif isinstance(block, DividerBlock): | ||
| return self.escape(_DIVIDER) | ||
| elif isinstance(block, JsonBlock): | ||
| if self.markdown: | ||
| return f"```\n{escape_markdownv2_code(block.json_str)}\n```" | ||
| return block.json_str | ||
| elif isinstance(block, KubernetesDiffBlock): | ||
| lines = [] | ||
| for diff in block.diffs: | ||
| path = ".".join(diff.path) | ||
| lines.append( | ||
| f"{self.bold(path)}: {self.escape(str(diff.other_value))} " | ||
| f"{self.escape('==>')} {self.escape(str(diff.value))}" | ||
| ) | ||
| return "\n".join(lines) | ||
| else: | ||
| logging.debug(f"Unsupported block type ({type(block)}) for telegram MarkdownV2 rendering") | ||
| return "" | ||
|
|
||
| def _inline_to_markdownv2(self, text: str) -> str: | ||
| if not self.markdown: | ||
| # plain text: strip the markers, keep readable link text | ||
| text = re.sub(r"<([^|>]+)\|([^>]+)>", r"\2 (\1)", text) | ||
| text = re.sub(r"\[([^\]]+)\]\(([^)]+)\)", r"\1 (\2)", text) | ||
| text = re.sub(r"`([^`]+)`", r"\1", text) | ||
| text = re.sub(r"\*\*([^*]+)\*\*", r"\1", text) | ||
| text = re.sub(r"\*([^*]+)\*", r"\1", text) | ||
| return text | ||
|
|
||
| out = [] | ||
| last = 0 | ||
| for m in _INLINE_RE.finditer(text): | ||
| out.append(escape_markdownv2(text[last : m.start()])) # plain run before token | ||
| if m.group("slack_url") is not None: | ||
| out.append(self.link(m.group("slack_text"), m.group("slack_url"))) | ||
| elif m.group("gh_text") is not None: | ||
| out.append(self.link(m.group("gh_text"), m.group("gh_url"))) | ||
| elif m.group("code") is not None: | ||
| out.append(self.code(m.group("code"))) | ||
| elif m.group("bold2") is not None: | ||
| out.append(self.bold(m.group("bold2"))) | ||
| elif m.group("bold") is not None: | ||
| out.append(self.bold(m.group("bold"))) | ||
| last = m.end() | ||
| out.append(escape_markdownv2(text[last:])) # trailing plain run | ||
| return "".join(out) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.