From c55800cb0e874a1a3a204a0aae6dd6954c824193 Mon Sep 17 00:00:00 2001 From: guohelu <141622458+guohelu@users.noreply.github.com> Date: Wed, 13 Nov 2024 19:48:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=B8=A0=E9=81=93=E6=8B=93=E5=B1=95=20#7545=20(#7607)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 新增通知渠道拓展 #7545 * fix: 修复逻辑判断错误 #7545 * fix: 修复逻辑错误 #7545 * fix: 修复注释错误 #7545 * fix: 修复协议问题 #7545 * fix: 修复参数错误 #7545 --- config/default.py | 5 ++++ env.py | 5 ++++ gcloud/core/api.py | 8 ++++++ gcloud/shortcuts/message/__init__.py | 6 ++-- gcloud/shortcuts/message/send_msg.py | 43 +++++++++++++++++++++++++++- 5 files changed, 63 insertions(+), 4 deletions(-) diff --git a/config/default.py b/config/default.py index ba5c58e90a..6b50c66834 100644 --- a/config/default.py +++ b/config/default.py @@ -883,3 +883,8 @@ def check_engine_admin_permission(request, *args, **kwargs): if "BKAPP_SOPS_BROKER_URL" in os.environ: BROKER_URL = os.getenv("BKAPP_SOPS_BROKER_URL") print(f"BROKER_URL: {BROKER_URL}") + +# bk_chat通知渠道 +ENABLE_BK_CHAT_CHANNEL = env.ENABLE_BK_CHAT_CHANNEL +# bk_chat路由 +BK_CHAT_API_ENTRY = env.BK_CHAT_API_ENTRY diff --git a/env.py b/env.py index 59c9045d14..a6553f09ae 100644 --- a/env.py +++ b/env.py @@ -153,3 +153,8 @@ # bk_audit BK_AUDIT_ENDPOINT = os.getenv("BK_AUDIT_ENDPOINT", None) BK_AUDIT_DATA_TOKEN = os.getenv("BK_AUDIT_DATA_TOKEN", None) + +# bk_chat通知渠道 +ENABLE_BK_CHAT_CHANNEL = False if os.getenv("BKAPP_ENABLE_BK_CHAT_CHANNEL") is None else True +# bk_chat通知路由 +BK_CHAT_API_ENTRY = os.getenv("BK_CHAT_API_ENTRY", "") diff --git a/gcloud/core/api.py b/gcloud/core/api.py index 814ef1d2d0..d006440cf5 100644 --- a/gcloud/core/api.py +++ b/gcloud/core/api.py @@ -160,6 +160,14 @@ def get_footer_info(request): def get_msg_types(request): client = get_client_by_user(request.user.username) result = client.cmsi.get_msg_type() + if settings.ENABLE_BK_CHAT_CHANNEL: + bk_chat = { + "type": "bk_chat", + "icon": None, + "label": "bk_chat", + "is_active": True, + } + result["data"].append(bk_chat) return JsonResponse(result) diff --git a/gcloud/shortcuts/message/__init__.py b/gcloud/shortcuts/message/__init__.py index 5575d6972d..0aacfe2f25 100644 --- a/gcloud/shortcuts/message/__init__.py +++ b/gcloud/shortcuts/message/__init__.py @@ -19,7 +19,7 @@ title_and_content_for_periodic_task_start_fail, title_and_content_for_clocked_task_create_fail, ) -from gcloud.shortcuts.message.send_msg import send_message +from gcloud.shortcuts.message.send_msg import send_message, MessageHandler from gcloud.periodictask.models import PeriodicTask logger = logging.getLogger("root") @@ -47,13 +47,13 @@ def send_task_flow_message(taskflow, msg_type, node_name=""): notify_type = notify_types.get("success", []) else: return False - + notify_info = notify_types.get("extra_info", {}) logger.info( "taskflow[id={flow_id}] will send {msg_type} message({notify_type}) to: {receivers}".format( flow_id=taskflow.id, msg_type=msg_type, notify_type=notify_type, receivers=receivers ) ) - send_message(executor, notify_type, receivers, title, content, email_content=email_content) + MessageHandler().send(executor, notify_type, notify_info, receivers, title, content, email_content=email_content) return True diff --git a/gcloud/shortcuts/message/send_msg.py b/gcloud/shortcuts/message/send_msg.py index 99dca7aad4..95eb41cd91 100644 --- a/gcloud/shortcuts/message/send_msg.py +++ b/gcloud/shortcuts/message/send_msg.py @@ -12,7 +12,7 @@ """ import logging - +import requests import ujson as json from gcloud.conf import settings @@ -20,6 +20,8 @@ get_client_by_user = settings.ESB_GET_CLIENT_BY_USER logger = logging.getLogger("root") +BK_CHAT_API_ENTRY = settings.BK_CHAT_API_ENTRY + def send_message(executor, notify_type, receivers, title, content, email_content=None): # 兼容旧数据 @@ -44,3 +46,42 @@ def send_message(executor, notify_type, receivers, title, content, email_content "taskflow send message failed, kwargs={}, result={}".format(json.dumps(kwargs), json.dumps(send_result)) ) return True + + +class MessageHandler: + def _get_bkchat_api(self): + return "{}/{}".format(BK_CHAT_API_ENTRY, "prod/im/api/v1/send_msg") + + def send(self, executor, notify_type, notify_info, receivers, title, content, email_content=None): + notify_cmsi = [] + notify_bkchat = [] + for notify in notify_type: + if notify == "bk_chat": + notify_bkchat.append(notify_info.get("bkchat_groupid")) + else: + notify_cmsi.append(notify) + if settings.ENABLE_BK_CHAT_CHANNEL and notify_bkchat: + self.send_bkchat(notify_bkchat, content) + send_message(executor, notify_cmsi, receivers, title, content, email_content) + + return True + + def send_bkchat(self, notify, content): + params = {"bk_app_code": settings.APP_CODE, "bk_app_secret": settings.SECRET_KEY} + + data = { + "im": "WEWORK", + "msg_type": "text", + "msg_param": {"content": content}, + "receiver": {"receiver_type": "group", "receiver_ids": notify}, + } + + result = requests.post(url=self._get_bkchat_api(), params=params, json=data) + send_result = result.json() + if send_result.get("code") == 0: + return True + else: + logger.error( + "bkchat send message failed, kwargs={}, result={}".format(json.dumps(data), json.dumps(send_result)) + ) + return False