Skip to content

Commit

Permalink
Merge branch 'master' into jertel/percentiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jertel authored Nov 29, 2023
2 parents 2d626cc + a433737 commit df5ec81
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- TBD

## New features
- TBD
- [Telegram] Added new telegram_thread_id setting for sending alerts to different threads of supergroup/forum. - [#1319](https://github.com/jertel/elastalert2/pull/1319) - @polshe-v

## Other changes
- Update setup.py & requirements.txt & requirements-dev.txt - [#1316](https://github.com/jertel/elastalert2/pull/1316) - @nsano-rururu
Expand Down
2 changes: 2 additions & 0 deletions docs/source/ruletypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3688,6 +3688,8 @@ Optional:

``telegram_parse_mode``: The Telegram parsing mode, which determines the format of the alert text body. Possible values are ``markdown``, ``markdownV2``, ``html``. Defaults to ``markdown``.

``telegram_thread_id``: Unique identifier for the target thread of supergroup/forum using telegram message_thread_id (Optional, positive integer value, no default).

Example usage::

alert:
Expand Down
4 changes: 3 additions & 1 deletion elastalert/alerters/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, rule):
self.telegram_proxy_login = self.rule.get('telegram_proxy_login', None)
self.telegram_proxy_password = self.rule.get('telegram_proxy_pass', None)
self.telegram_parse_mode = self.rule.get('telegram_parse_mode', 'markdown')
self.telegram_thread_id = self.rule.get('telegram_thread_id', None)

def alert(self, matches):
if self.telegram_parse_mode != 'html':
Expand All @@ -49,7 +50,8 @@ def alert(self, matches):
'chat_id': self.telegram_room_id,
'text': body,
'parse_mode': self.telegram_parse_mode,
'disable_web_page_preview': True
'disable_web_page_preview': True,
'message_thread_id': self.telegram_thread_id
}

try:
Expand Down
1 change: 1 addition & 0 deletions elastalert/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,7 @@ properties:
telegram_proxy_login: {type: string}
telegram_proxy_pass: {type: string}
telegram_parse_mode: {type: string, enum: ['markdown', 'markdownV2', 'html']}
telegram_thread_id: {type: number}

### Tencent SMS
tencent_sms_secret_id: {type: string}
Expand Down
55 changes: 50 additions & 5 deletions tests/alerters/telegram_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,46 @@
from elastalert.util import EAException


def test_telegram_thread_id(caplog):
caplog.set_level(logging.INFO)
rule = {
'name': 'Test Telegram Rule',
'type': 'any',
'telegram_bot_token': 'xxxxx1',
'telegram_room_id': 'xxxxx2',
'telegram_thread_id': 2,
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = TelegramAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'somefield': 'foobarbaz'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])
expected_data = {
'chat_id': rule['telegram_room_id'],
'text': '⚠ *Test Telegram Rule* ⚠ ```\nTest Telegram Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n ```',
'parse_mode': 'markdown',
'disable_web_page_preview': True,
'message_thread_id': 2
}

mock_post_request.assert_called_once_with(
'https://api.telegram.org/botxxxxx1/sendMessage',
data=mock.ANY,
headers={'content-type': 'application/json'},
proxies=None,
auth=None
)

actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
assert expected_data == actual_data
assert ('elastalert', logging.INFO, 'Alert sent to Telegram room xxxxx2') == caplog.record_tuples[0]


def test_telegram_markdown(caplog):
caplog.set_level(logging.INFO)
rule = {
Expand All @@ -34,7 +74,8 @@ def test_telegram_markdown(caplog):
'chat_id': rule['telegram_room_id'],
'text': '⚠ *Test Telegram Rule* ⚠ ```\nTest Telegram Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n ```',
'parse_mode': 'markdown',
'disable_web_page_preview': True
'disable_web_page_preview': True,
'message_thread_id': None
}

mock_post_request.assert_called_once_with(
Expand Down Expand Up @@ -73,7 +114,8 @@ def test_telegram_html(caplog):
'chat_id': rule['telegram_room_id'],
'text': '⚠ Test Telegram Rule ⚠ \nTest Telegram Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n',
'parse_mode': 'html',
'disable_web_page_preview': True
'disable_web_page_preview': True,
'message_thread_id': None
}

mock_post_request.assert_called_once_with(
Expand Down Expand Up @@ -113,7 +155,8 @@ def test_telegram_proxy():
'chat_id': rule['telegram_room_id'],
'text': '⚠ *Test Telegram Rule* ⚠ ```\nTest Telegram Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n ```',
'parse_mode': 'markdown',
'disable_web_page_preview': True
'disable_web_page_preview': True,
'message_thread_id': None
}

mock_post_request.assert_called_once_with(
Expand Down Expand Up @@ -150,7 +193,8 @@ def test_telegram_text_maxlength():
'text': '⚠ *Test Telegram Rule' + ('a' * 3979) +
'\n⚠ *message was cropped according to telegram limits!* ⚠ ```',
'parse_mode': 'markdown',
'disable_web_page_preview': True
'disable_web_page_preview': True,
'message_thread_id': None
}

mock_post_request.assert_called_once_with(
Expand Down Expand Up @@ -275,7 +319,8 @@ def test_telegram_matchs():
'----------------------------------------\n' +
' ```',
'parse_mode': 'markdown',
'disable_web_page_preview': True
'disable_web_page_preview': True,
'message_thread_id': None
}

mock_post_request.assert_called_once_with(
Expand Down

0 comments on commit df5ec81

Please sign in to comment.