Skip to content
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

Support character limit for line notify #320

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions elastalert/alerters/line.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import requests
from requests import RequestException

from elastalert.alerts import Alerter
from elastalert.alerts import Alerter, BasicMatchString
from elastalert.util import EAException, elastalert_logger


Expand All @@ -14,7 +14,14 @@ def __init__(self, rule):
self.linenotify_access_token = self.rule.get("linenotify_access_token", None)

def alert(self, matches):
body = self.create_alert_body(matches)
body = ''
for match in matches:
body += str(BasicMatchString(self.rule, match))
if len(matches) > 1:
body += '\n----------------------------------------\n'
if len(body) > 999:
body = body[0:900] + '\n *message was cropped according to line notify embed description limits!* '
body += '```'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of the 3 backticks at the end of the body, on this PR and the one for Chatwork?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I erased. It's garbage. Discord also has similar trash. discord will be fixed tomorrow.

# post to Line Notify
headers = {
"Content-Type": "application/x-www-form-urlencoded",
Expand Down
37 changes: 36 additions & 1 deletion tests/alerters/line_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_line_notify(caplog):
alert.alert([match])

expected_data = {
'message': 'Test LineNotify Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n'
'message': 'Test LineNotify Rule\n\n@timestamp: 2021-01-01T00:00:00\nsomefield: foobarbaz\n```'
}

mock_post_request.assert_called_once_with(
Expand Down Expand Up @@ -114,3 +114,38 @@ def test_line_required_error(linenotify_access_token, expected_data):
assert expected_data == actual_data
except Exception as ea:
assert expected_data in str(ea)


def test_line_notify_maxlength():
rule = {
'name': 'Test LineNotify Rule' + ('a' * 1000),
'type': 'any',
'linenotify_access_token': 'xxxxx',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = LineNotifyAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'somefield': 'foobarbaz'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])

expected_data = {
'message': 'Test LineNotify Rule' + ('a' * 880) +
'\n *message was cropped according to line notify embed description limits!* ```'
}

mock_post_request.assert_called_once_with(
'https://notify-api.line.me/api/notify',
data=mock.ANY,
headers={
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer {}'.format('xxxxx')
}
)

actual_data = mock_post_request.call_args_list[0][1]['data']
assert expected_data == actual_data