-
Notifications
You must be signed in to change notification settings - Fork 0
Add telegram formatter, bump version #13
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [project] | ||
| name = "shondesh" | ||
| version = "0.1.2" | ||
| version = "0.1.3" | ||
| description = "Send notification via channels" | ||
| authors = [ | ||
| {name = "Rakibul Haq",email = "[email protected]"} | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import Any | ||
|
|
||
|
|
||
| class Formatter(ABC): | ||
| @abstractmethod | ||
| def format(self, data) -> str: | ||
| def format(self, data) -> Any: | ||
| pass |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from shondesh.formatters.base_formatter import Formatter | ||
|
|
||
|
|
||
| class TelegramMessageFormatter(Formatter): | ||
| """ | ||
| A class to format messages for Telegram. | ||
| """ | ||
|
|
||
| def format(self, message: dict) -> str: | ||
| """ | ||
| Format the message for Telegram. | ||
|
|
||
| Args: | ||
| message (str): The message to format. | ||
|
|
||
| Returns: | ||
| str: The formatted message. | ||
| """ | ||
| if not message: | ||
| return "" | ||
|
|
||
| for key, value in message.items(): | ||
| if isinstance(value, str): | ||
| # Escape special characters for Telegram | ||
| value = ( | ||
| value.replace("_", "\\_") | ||
| .replace("*", "\\*") | ||
| .replace("[", "\\[") | ||
| .replace("]", "\\]") | ||
| .replace("`", "\\`") | ||
| .replace("~", "\\~") | ||
| .replace(">", ">") | ||
| .replace("<", "<") | ||
| ) | ||
| message[key] = str(value) | ||
| # Join the message parts with double newlines for better readability | ||
| message = "\n".join(f"<b>{key}</b>: {value}" for key, value in message.items()) | ||
| # Wrap the message in a code block for Telegram | ||
| message = f"<pre>{message}</pre>" | ||
|
|
||
| return message | ||
35 changes: 35 additions & 0 deletions
35
tests/shondesh/formatters/test_telegram_message_formatter.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,35 @@ | ||
| from shondesh.formatters.telegram_message_formatter import TelegramMessageFormatter | ||
|
|
||
|
|
||
| def test_returns_empty_string_when_message_is_none(): | ||
| formatter = TelegramMessageFormatter() | ||
| assert formatter.format(None) == "" | ||
|
|
||
|
|
||
| def test_returns_empty_string_when_message_is_empty(): | ||
| formatter = TelegramMessageFormatter() | ||
| assert formatter.format({}) == "" | ||
|
|
||
|
|
||
| def test_formats_message_with_special_characters_correctly(): | ||
| formatter = TelegramMessageFormatter() | ||
| message = {"text": "Hello *world* _test_ [link](url)"} | ||
| formatted_message = formatter.format(message) | ||
| assert ( | ||
| "<b>text</b>: Hello \\*world\\* \\_test\\_ \\[link\\](url)" in formatted_message | ||
| ) | ||
|
|
||
|
|
||
| def test_escapes_html_characters_in_message(): | ||
| formatter = TelegramMessageFormatter() | ||
| message = {"text": "<script>alert('XSS')</script>"} | ||
| formatted_message = formatter.format(message) | ||
| assert "<script>alert('XSS')</script>" in formatted_message | ||
|
|
||
|
|
||
| def test_formats_message_with_multiple_keys_correctly(): | ||
| formatter = TelegramMessageFormatter() | ||
| message = {"title": "Notification", "body": "This is a *test*"} | ||
| formatted_message = formatter.format(message) | ||
| assert "<b>title</b>: Notification" in formatted_message | ||
| assert "<b>body</b>: This is a \\*test\\*" in formatted_message |
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.
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.
[nitpick] Mutating the input
messagedict in place can cause side-effects. Consider building a new dictionary or list of formatted parts instead of overwritingmessage.