From 4cb2be8d28b5a9a11d943c1cb83a620a4e78e17c Mon Sep 17 00:00:00 2001 From: bitqiu Date: Fri, 2 Feb 2024 11:04:07 +0800 Subject: [PATCH 1/5] add workwechat msgtype --- docs/source/alerts.rst | 2 ++ elastalert/alerters/workwechat.py | 25 +++++++++++++++++-------- elastalert/schema.yaml | 1 + tests/alerters/workwechat_test.py | 12 +++++++++--- 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/docs/source/alerts.rst b/docs/source/alerts.rst index d84e8a10..01991d55 100644 --- a/docs/source/alerts.rst +++ b/docs/source/alerts.rst @@ -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 ~~~~~~ diff --git a/elastalert/alerters/workwechat.py b/elastalert/alerters/workwechat.py index d3d59b22..97370612 100644 --- a/elastalert/alerters/workwechat.py +++ b/elastalert/alerters/workwechat.py @@ -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) @@ -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( diff --git a/elastalert/schema.yaml b/elastalert/schema.yaml index e4d001ed..dc616852 100644 --- a/elastalert/schema.yaml +++ b/elastalert/schema.yaml @@ -795,6 +795,7 @@ properties: ### WorkWechat work_wechat_bot_id: { type: string } + work_wechat_msgtype: { type: string } ### Zabbix zbx_sender_host: {type: string} diff --git a/tests/alerters/workwechat_test.py b/tests/alerters/workwechat_test.py index 12550a70..0a2fe48b 100644 --- a/tests/alerters/workwechat_test.py +++ b/tests/alerters/workwechat_test.py @@ -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({}) @@ -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({}) @@ -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({}) @@ -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'), +@pytest.mark.parametrize('work_wechat_bot_id, work_wechat_msgtype, expected_data', [ + ('', 'Missing required option(s): work_wechat_bot_id, work_wechat_msgtype'), ('xxxxxxx', { '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', @@ -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) From 62dd3285d28d7d6023227c28b7ca996e9acadf93 Mon Sep 17 00:00:00 2001 From: bitqiu Date: Fri, 2 Feb 2024 13:08:09 +0800 Subject: [PATCH 2/5] add workwechat msgtype --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4166afae..eb920c14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - TBD ## New features +- [workwechat] add workwechat msgtype - [#1369](https://github.com/jertel/elastalert2/pull/1369) - @bitqiu - Add workwechat alerter - [#1367](https://github.com/jertel/elastalert2/pull/1367) - @wufeiqun ## Other changes From 3a9a55bffdb588bfa109dead3e1c18254f50f7de Mon Sep 17 00:00:00 2001 From: bitqiu Date: Fri, 2 Feb 2024 13:52:59 +0800 Subject: [PATCH 3/5] update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb920c14..cd0a7504 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,8 @@ - TBD ## New features -- [workwechat] add workwechat msgtype - [#1369](https://github.com/jertel/elastalert2/pull/1369) - @bitqiu - Add workwechat alerter - [#1367](https://github.com/jertel/elastalert2/pull/1367) - @wufeiqun +- [workwechat] add workwechat msgtype - [#1369](https://github.com/jertel/elastalert2/pull/1369) - @bitqiu ## Other changes - TBD From 33f905d284862415e5087802e9c00254b5159886 Mon Sep 17 00:00:00 2001 From: Jason Ertel Date: Thu, 28 Mar 2024 13:06:43 -0400 Subject: [PATCH 4/5] Correct changelog placement --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 608b260b..d850ac13 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - None ## Other changes -- None +- [workwechat] add workwechat msgtype - [#1369](https://github.com/jertel/elastalert2/pull/1369) - @bitqiu # 2.17.0 @@ -16,7 +16,6 @@ ## New features - Add workwechat alerter - [#1367](https://github.com/jertel/elastalert2/pull/1367) - @wufeiqun -- [workwechat] add workwechat msgtype - [#1369](https://github.com/jertel/elastalert2/pull/1369) - @bitqiu ## Other changes - [IRIS] Remove empty IOC in alerts that are not accepted by the API - [#1374](https://github.com/jertel/elastalert2/pull/1374) - @yaksnip425 From 161a1bcb02267bcb115395e48477ae377b4e5a11 Mon Sep 17 00:00:00 2001 From: Jason Ertel Date: Thu, 28 Mar 2024 16:23:22 -0400 Subject: [PATCH 5/5] Update workwechat_test.py --- tests/alerters/workwechat_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/alerters/workwechat_test.py b/tests/alerters/workwechat_test.py index 0a2fe48b..bdb9bc56 100644 --- a/tests/alerters/workwechat_test.py +++ b/tests/alerters/workwechat_test.py @@ -93,8 +93,8 @@ def test_work_wechat_getinfo(): @pytest.mark.parametrize('work_wechat_bot_id, work_wechat_msgtype, expected_data', [ - ('', 'Missing required option(s): work_wechat_bot_id, work_wechat_msgtype'), - ('xxxxxxx', + ('', '', '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'