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 chatwork #319

Merged
merged 3 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions elastalert/alerters/chatwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from requests import RequestException
from requests.auth import HTTPProxyAuth

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


Expand All @@ -22,8 +22,13 @@ def __init__(self, rule):
self.chatwork_proxy_pass = self.rule.get('chatwork_proxy_pass', 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) > 2047:
body = body[0:1950] + '\n *message was cropped according to chatwork embed description limits!*'
headers = {'X-ChatWorkToken': self.chatwork_apikey}
# set https proxy, if it was provided
proxies = {'https': self.chatwork_proxy} if self.chatwork_proxy else None
Expand All @@ -32,7 +37,6 @@ def alert(self, matches):

try:
response = requests.post(self.url, params=params, headers=headers, proxies=proxies, auth=auth)
warnings.resetwarnings()
response.raise_for_status()
except RequestException as e:
raise EAException("Error posting to Chattwork: %s. Details: %s" % (e, "" if e.response is None else e.response.text))
Expand Down
34 changes: 34 additions & 0 deletions tests/alerters/chatwork_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,37 @@ def test_chatwork_required_error(chatwork_apikey, chatwork_room_id, expected_dat
assert expected_data == actual_data
except Exception as ea:
assert expected_data in str(ea)


def test_chatwork_maxlength():
rule = {
'name': 'Test Chatwork Rule' + ('a' * 2069),
'type': 'any',
'chatwork_apikey': 'xxxx1',
'chatwork_room_id': 'xxxx2',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = ChatworkAlerter(rule)
match = {
'@timestamp': '2021-01-01T00:00:00',
'somefield': 'foobarbaz'
}
with mock.patch('requests.post') as mock_post_request:
alert.alert([match])
expected_data = {
'body': 'Test Chatwork Rule' + ('a' * 1932) +
'\n *message was cropped according to chatwork embed description limits!*'
}

mock_post_request.assert_called_once_with(
'https://api.chatwork.com/v2/rooms/xxxx2/messages',
params=mock.ANY,
headers={'X-ChatWorkToken': 'xxxx1'},
proxies=None,
auth=None
)

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