Skip to content

Commit

Permalink
Merge pull request jertel#1369 from bitqiu/master
Browse files Browse the repository at this point in the history
add workwechat msgtype
  • Loading branch information
jertel authored Mar 28, 2024
2 parents 0a3fcb8 + 161a1bc commit f8397a6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- None

## Other changes
- None
- [workwechat] add workwechat msgtype - [#1369](https://github.com/jertel/elastalert2/pull/1369) - @bitqiu

# 2.17.0

Expand Down
2 changes: 2 additions & 0 deletions docs/source/alerts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2281,12 +2281,14 @@ WorkWechat alerter will send notification to a predefined bot in WorkWechat appl
Required:

``work_wechat_bot_id``: WorkWechat bot id.
``work_wechat_msgtype``: WorkWechat msgtype. default to ``text``. ``markdown``

Example usage::

alert:
- "workwechat"
work_wechat_bot_id: "your workwechat bot id"
work_wechat_msgtype: "text"

Zabbix
~~~~~~
Expand Down
25 changes: 17 additions & 8 deletions elastalert/alerters/workwechat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def __init__(self, rule):
super(WorkWechatAlerter, self).__init__(rule)
self.work_wechat_bot_id = self.rule.get('work_wechat_bot_id', None)
self.work_wechat_webhook_url = f'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={self.work_wechat_bot_id}'
self.work_wechat_msg_type = 'text'

self.work_wechat_msgtype = self.rule.get('work_wechat_msgtype', 'text')
def alert(self, matches):
title = self.create_title(matches)
body = self.create_alert_body(matches)
Expand All @@ -26,12 +25,22 @@ def alert(self, matches):
'Accept': 'application/json;charset=utf-8'
}

payload = {
'msgtype': self.work_wechat_msg_type,
"text": {
"content": body
},
}
if self.work_wechat_msgtype == 'text':
# text
payload = {
'msgtype': self.work_wechat_msgtype,
'text': {
'content': body
}
}
if self.work_wechat_msgtype == 'markdown':
# markdown
payload = {
'msgtype': self.work_wechat_msgtype,
'markdown': {
'content': body
}
}

try:
response = requests.post(
Expand Down
1 change: 1 addition & 0 deletions elastalert/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ properties:

### WorkWechat
work_wechat_bot_id: { type: string }
work_wechat_msgtype: { type: string }

### Zabbix
zbx_sender_host: {type: string}
Expand Down
14 changes: 10 additions & 4 deletions tests/alerters/workwechat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_work_wechat_text(caplog):
'name': 'Test WorkWechat Rule',
'type': 'any',
'work_wechat_bot_id': 'xxxxxxx',
'work_wechat_msgtype': 'text',
'alert': [],
}
rules_loader = FileRulesLoader({})
Expand Down Expand Up @@ -55,6 +56,7 @@ def test_work_wechat_ea_exception():
'name': 'Test WorkWechat Rule',
'type': 'any',
'work_wechat_bot_id': 'xxxxxxx',
'work_wechat_msgtype': 'text',
'alert': [],
}
rules_loader = FileRulesLoader({})
Expand All @@ -75,6 +77,7 @@ def test_work_wechat_getinfo():
'name': 'Test WorkWechat Rule',
'type': 'any',
'work_wechat_bot_id': 'xxxxxxx',
'work_wechat_msgtype': 'text',
'alert': [],
}
rules_loader = FileRulesLoader({})
Expand All @@ -89,15 +92,15 @@ def test_work_wechat_getinfo():
assert expected_data == actual_data


@pytest.mark.parametrize('work_wechat_bot_id, expected_data', [
('', 'Missing required option(s): work_wechat_bot_id'),
('xxxxxxx',
@pytest.mark.parametrize('work_wechat_bot_id, work_wechat_msgtype, expected_data', [
('', '', 'Missing required option(s): work_wechat_bot_id, work_wechat_msgtype'),
('xxxxxxx', 'yyyyyy',
{
'type': 'workwechat',
'work_wechat_webhook_url': 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxx'
}),
])
def test_work_wechat_required_error(work_wechat_bot_id, expected_data):
def test_work_wechat_required_error(work_wechat_bot_id, work_wechat_msgtype, expected_data):
try:
rule = {
'name': 'Test WorkWechat Rule',
Expand All @@ -108,6 +111,9 @@ def test_work_wechat_required_error(work_wechat_bot_id, expected_data):
if work_wechat_bot_id:
rule['work_wechat_bot_id'] = work_wechat_bot_id

if work_wechat_msgtype:
rule['work_wechat_msgtype'] = work_wechat_msgtype

rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = WorkWechatAlerter(rule)
Expand Down

0 comments on commit f8397a6

Please sign in to comment.