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

增加企业微信群机器人通知(webhook) #810

Merged
merged 2 commits into from
Jul 14, 2020
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
20 changes: 17 additions & 3 deletions common/templates/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ <h5 style="color: darkgrey"><b>SQL优化</b></h5>
<br>
<h4 style="color: darkgrey;display: inline"><b>通知配置</b></h4>&nbsp;&nbsp;&nbsp;
<h6 style="color:red">注1:钉钉Webhook需要配置资源组Webhook地址,Webhook无法通知到指定用户</h6>
<h6 style="color:red">注2:企业微信开启后,若未配置企业微信UserID,则默认取用户的username作为UserId</h6>
<h6 style="color:red">注2:企业微信自建应用开启后,若未配置企业微信UserID,则默认取用户的username作为UserId</h6>
<h6 style="color:red">注3:企业微信群机器人需要配置资源组webhook地址(企业微信群机器人是通知到群;企业微信自建应用是通过应用通知到指定人)</h6>
<hr/>
<div class="form-horizontal">
<div class="form-group">
Expand Down Expand Up @@ -572,13 +573,13 @@ <h6 style="color:red">注2:企业微信开启后,若未配置企业微信Use

<div class="form-group">
<label for="wx"
class="col-sm-4 control-label">企业微信</label>
class="col-sm-4 control-label">企业微信自建应用</label>
<div class="col-sm-8">
<div class="switch switch-small">
<label>
<input id="wx" key="wx"
value="{{ config.wx }}"
type="checkbox"> 是否开启企业微信通知
type="checkbox"> 是否开启企业微信自建应用通知
</label>
</div>
</div>
Expand Down Expand Up @@ -625,6 +626,19 @@ <h6 style="color:red">注2:企业微信开启后,若未配置企业微信Use
</div>
</div>
</div>
<div class="form-group">
<label for="qywx_webhook"
class="col-sm-4 control-label">企业微信群机器人</label>
<div class="col-sm-8">
<div class="switch switch-small">
<label>
<input id="qywx_webhook" key="qywx_webhook"
value="{{ config.qywx_webhook }}"
type="checkbox"> 是否开启企业微信群机器人通知
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="feishu_webhook"
class="col-sm-4 control-label">飞书Webhook</label>
Expand Down
16 changes: 16 additions & 0 deletions common/utils/sendmsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ def send_wx2user(self, msg, user_list):
else:
logger.error(f'企业微信推送失败\n请求连接:{send_url}\n请求参数:{data}\n请求响应:{r_json}')

def send_qywx_webhook(self,qywx_webhook, msg):

send_url = qywx_webhook
data = {
"msgtype": "markdown",
"markdown": {
"content": msg
},
}
res = requests.post(url=send_url, json=data, timeout=5)
r_json = res.json()
if r_json['errcode'] == 0:
logger.debug(f'企业微信机器人推送成功\n通知对象:机器人')
else:
logger.error(f'企业微信机器人推送失败\n请求连接:{send_url}\n请求参数:{data}\n请求响应:{r_json}')

@staticmethod
def send_feishu_webhook(url, title, content):
data = {
Expand Down
2 changes: 1 addition & 1 deletion sql/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UsersAdmin(UserAdmin):
# 资源组管理
@admin.register(ResourceGroup)
class ResourceGroupAdmin(admin.ModelAdmin):
list_display = ('group_id', 'group_name', 'ding_webhook', 'feishu_webhook', 'is_deleted')
list_display = ('group_id', 'group_name', 'ding_webhook', 'feishu_webhook', 'qywx_webhook', 'is_deleted')
exclude = ('group_parent_id', 'group_sort', 'group_level',)


Expand Down
1 change: 1 addition & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ResourceGroup(models.Model):
group_level = models.IntegerField('层级', default=1)
ding_webhook = models.CharField('钉钉webhook地址', max_length=255, blank=True)
feishu_webhook = models.CharField('飞书webhook地址', max_length=255, blank=True)
qywx_webhook = models.CharField('企业微信webhook地址', max_length=255, blank=True)
is_deleted = models.IntegerField('是否删除', choices=((0, '否'), (1, '是')), default=0)
create_time = models.DateTimeField(auto_now_add=True)
sys_time = models.DateTimeField(auto_now=True)
Expand Down
12 changes: 9 additions & 3 deletions sql/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ def __notify_cnf_status():
ding_status = sys_config.get('ding_to_person')
ding_webhook_status = sys_config.get('ding')
wx_status = sys_config.get('wx')
qywx_webhook_status = sys_config.get("qywx_webhook")
feishu_webhook_status = sys_config.get("feishu_webhook")
feishu_status = sys_config.get("feishu")
if not any([mail_status, ding_status, ding_webhook_status, wx_status, feishu_status, feishu_webhook_status]):
if not any([mail_status, ding_status, ding_webhook_status, wx_status, feishu_status, feishu_webhook_status ,qywx_webhook_status]):
logger.info('未开启任何消息通知,可在系统设置中开启')
return False
else:
Expand All @@ -45,6 +46,7 @@ def __send(msg_title, msg_content, msg_to, msg_cc=None, **kwargs):
msg_cc = msg_cc if msg_cc else []
dingding_webhook = kwargs.get('dingding_webhook')
feishu_webhook = kwargs.get('feishu_webhook')
qywx_webhook = kwargs.get('qywx_webhook')
msg_to_email = [user.email for user in msg_to if user.email]
msg_cc_email = [user.email for user in msg_cc if user.email]
msg_to_ding_user = [user.ding_user_id for user in chain(msg_to, msg_cc) if user.ding_user_id]
Expand All @@ -64,6 +66,8 @@ def __send(msg_title, msg_content, msg_to, msg_cc=None, **kwargs):
open_id = [user.feishu_open_id for user in chain(msg_to, msg_cc) if user.feishu_open_id]
user_mail = [user.email for user in chain(msg_to, msg_cc) if not user.feishu_open_id]
msg_sender.send_feishu_user(msg_title, msg_content, open_id, user_mail)
if sys_config.get('qywx_webhook') and qywx_webhook:
msg_sender.send_qywx_webhook(qywx_webhook, msg_title + '\n' + msg_content)


def notify_for_audit(audit_id, **kwargs):
Expand Down Expand Up @@ -92,6 +96,7 @@ def notify_for_audit(audit_id, **kwargs):
group_name = audit_detail.group_name
dingding_webhook = ResourceGroup.objects.get(group_id=audit_detail.group_id).ding_webhook
feishu_webhook = ResourceGroup.objects.get(group_id=audit_detail.group_id).feishu_webhook
qywx_webhook = ResourceGroup.objects.get(group_id=audit_detail.group_id).qywx_webhook
# 获取当前审批和审批流程
workflow_auditors, current_workflow_auditors = Audit.review_info(audit_detail.workflow_id,
audit_detail.workflow_type)
Expand Down Expand Up @@ -204,7 +209,7 @@ def notify_for_audit(audit_id, **kwargs):
raise Exception('工单状态不正确')
logger.info(f"通知Debug{msg_to}{msg_cc}")
# 发送通知
__send(msg_title, msg_content, msg_to, msg_cc, feishu_webhook=feishu_webhook, dingding_webhook=dingding_webhook)
__send(msg_title, msg_content, msg_to, msg_cc, feishu_webhook=feishu_webhook, dingding_webhook=dingding_webhook, qywx_webhook=qywx_webhook)


def notify_for_execute(workflow):
Expand Down Expand Up @@ -242,8 +247,9 @@ def notify_for_execute(workflow):
# 处理接收人
dingding_webhook = ResourceGroup.objects.get(group_id=workflow.group_id).ding_webhook
feishu_webhook = ResourceGroup.objects.get(group_id=workflow.group_id).feishu_webhook
qywx_webhook = ResourceGroup.objects.get(group_id=workflow.group_id).qywx_webhook
# 发送通知
__send(msg_title, msg_content, msg_to, msg_cc, dingding_webhook=dingding_webhook, feishu_webhook=feishu_webhook)
__send(msg_title, msg_content, msg_to, msg_cc, dingding_webhook=dingding_webhook, feishu_webhook=feishu_webhook, qywx_webhook=qywx_webhook)

# DDL通知
if sys_config.get('ddl_notify_auth_group') and workflow.status == 'workflow_finish':
Expand Down
5 changes: 4 additions & 1 deletion src/init_sql/v1.7.11_v1.7.12.sql
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ CREATE TABLE `sql_instance` (
UNIQUE KEY `instance_name` (`instance_name`),
KEY `sql_instance_tunnel_id_99377638_fk_ssh_tunnel_id` (`tunnel_id`),
CONSTRAINT `sql_instance_tunnel_id_99377638_fk_ssh_tunnel_id` FOREIGN KEY (`tunnel_id`) REFERENCES `ssh_tunnel` (`id`)
) ;
) ;

-- 增加企业微信信息
alter table resource_group add qywx_webhook varchar(255) not null default '' comment '企业微信webhook地址';