perf: 优化通知,对支持 markdown 的发 markdown

pull/7404/head
ibuler 3 years ago committed by Jiangjie.Bai
parent b113eeb1d6
commit b92530f0b9

@ -11,6 +11,7 @@
<b>{% trans 'Login city' %}:</b> {{ city }}({{ ip }})
</p>
-
<p>
{% trans 'If you suspect that the login behavior is abnormal, please modify the account password in time.' %}
</p>

@ -11,9 +11,8 @@
</a>
</p>
-
<p>
{% trans 'This link is valid for 1 hour. After it expires' %}
<a href="{{ forget_password_url }}?email={{ user.email }}">
{% trans 'request new one' %}
</a>
<a href="{{ forget_password_url }}?email={{ user.email }}">{% trans 'request new one' %}</a>
</p>

@ -8,6 +8,7 @@
<b>{% trans 'IP' %}:</b> {{ ip_address }} <br />
<b>{% trans 'Browser' %}:</b> {{ browser }}
</p>
-
<p>
{% trans 'If the password update was not initiated by you, your account may have security issues' %} <br />
{% trans 'If you have any questions, you can contact the administrator' %}

@ -8,6 +8,7 @@
<b>{% trans 'IP' %}:</b> {{ ip_address }} <br />
<b>{% trans 'Browser' %}:</b> {{ browser }}
</p>
-
<p>
{% trans 'If the public key update was not initiated by you, your account may have security issues' %} <br />
{% trans 'If you have any questions, you can contact the administrator' %}

@ -151,12 +151,29 @@ class DingTalk:
'data': data
}
data = self._request.post(URL.SEND_MESSAGE_BY_TEMPLATE, json=body, with_token=True)
return data
def send_markdown(self, user_ids, title, msg):
body = {
'agent_id': self._agentid,
'userid_list': ','.join(user_ids),
'to_all_user': False,
'msg': {
'msgtype': 'markdown',
'markdown': {
'title': title,
'text': msg
}
}
}
logger.info(f'Dingtalk send markdown to user {user_ids}: {msg}')
data = self._request.post(URL.SEND_MESSAGE, json=body, with_token=True)
return data
def send_text(self, user_ids, msg):
body = {
'agent_id': self._agentid,
'userid_list': ','.join(user_ids),
# 'dept_id_list': '',
'to_all_user': False,
'msg': {
'msgtype': 'text',
@ -165,7 +182,7 @@ class DingTalk:
}
}
}
logger.info(f'Dingtalk send text: user_ids={user_ids} msg={msg}')
logger.info(f'Dingtalk send msg to user {user_ids}: {msg}')
data = self._request.post(URL.SEND_MESSAGE, json=body, with_token=True)
return data

@ -90,7 +90,10 @@ class WeCom(RequestMixin):
timeout=timeout
)
def send_text(self, users: Iterable, msg: AnyStr, **kwargs):
def send_markdown(self, users: Iterable, msg: AnyStr, **kwargs):
pass
def send_text(self, users: Iterable, msg: AnyStr, markdown=False, **kwargs):
"""
https://open.work.weixin.qq.com/api/doc/90000/90135/90236
@ -115,6 +118,13 @@ class WeCom(RequestMixin):
},
**extra_params
}
if markdown:
body['msgtype'] = 'markdown'
body["markdown"] = {
"content": msg
}
body.pop('text', '')
logger.info(f'Wecom send text: users={users} msg={msg}')
data = self._requests.post(URL.SEND_MESSAGE, json=body, check_errcode_is_0=False)

@ -16,7 +16,7 @@ class DingTalk(BackendBase):
def send_msg(self, users, message, subject=None):
accounts, __, __ = self.get_accounts(users)
return self.dingtalk.send_text(accounts, message)
return self.dingtalk.send_markdown(accounts, subject, message)
backend = DingTalk

@ -17,7 +17,7 @@ class WeCom(BackendBase):
def send_msg(self, users, message, subject=None):
accounts, __, __ = self.get_accounts(users)
return self.wecom.send_text(accounts, message)
return self.wecom.send_text(accounts, message, markdown=True)
backend = WeCom

@ -94,7 +94,7 @@ class Message(metaclass=MessageType):
traceback.print_exc()
@classmethod
def send_test_msg(cls, ding=True):
def send_test_msg(cls, ding=True, wecom=False):
msg = cls.gen_test_msg()
if not msg:
return
@ -104,6 +104,8 @@ class Message(metaclass=MessageType):
backends = []
if ding:
backends.append(BACKEND.DINGTALK)
if wecom:
backends.append(BACKEND.WECOM)
msg.send_msg(users, backends)
@staticmethod
@ -113,8 +115,17 @@ class Message(metaclass=MessageType):
def get_html_msg(self) -> dict:
return self.get_common_msg()
def get_markdown_msg(self) -> dict:
h = HTML2Text()
h.body_width = 300
msg = self.get_html_msg()
content = msg['message']
msg['message'] = h.handle(content)
return msg
def get_text_msg(self) -> dict:
h = HTML2Text()
h.body_width = 90
msg = self.get_html_msg()
content = msg['message']
h.ignore_links = self.text_msg_ignore_links
@ -130,6 +141,10 @@ class Message(metaclass=MessageType):
msg = self.get_text_msg()
return msg
@lazyproperty
def markdown_msg(self):
return self.get_markdown_msg()
@lazyproperty
def html_msg(self) -> dict:
msg = self.get_html_msg()
@ -167,17 +182,17 @@ class Message(metaclass=MessageType):
# 支持不同发送消息的方式定义自己的消息内容,比如有些支持 html 标签
def get_dingtalk_msg(self) -> dict:
# 钉钉相同的消息一天只能发一次,所以给所有消息添加基于时间的序号,使他们不相同
message = self.text_msg['message']
message = self.markdown_msg['message']
time = local_now().strftime('%Y-%m-%d %H:%M:%S')
suffix = '\n{}: {}'.format(_('Time'), time)
return {
'subject': self.text_msg['subject'],
'subject': self.markdown_msg['subject'],
'message': message + suffix
}
def get_wecom_msg(self) -> dict:
return self.text_msg
return self.markdown_msg
def get_feishu_msg(self) -> dict:
return self.text_msg
@ -207,12 +222,12 @@ class Message(metaclass=MessageType):
return messages_cls
@classmethod
def test_all_messages(cls):
def test_all_messages(cls, ding=True, wecom=False):
messages_cls = cls.get_all_sub_messages()
for _cls in messages_cls:
try:
msg = _cls.send_test_msg()
_cls.send_test_msg(ding=ding, wecom=wecom)
except NotImplementedError:
continue

@ -16,6 +16,7 @@
</ul>
<br />
-
<p>
{% trans 'If you have any question, please contact the administrator' %}
</p>

@ -11,7 +11,7 @@
<a href="{{ update_password_url }}">{% trans 'Click here update password' %}</a>
<br />
</p>
-
<p>
{% trans 'If your password has expired, please click the link below to' %}
<a href="{{ forget_password_url }}?email={{ email }}">{% trans 'Reset password' %}</a>

@ -15,6 +15,9 @@
{% trans 'click here to set your password' %}
</a>
</p>
-
<p>
{% trans 'This link is valid for 1 hour. After it expires' %}
<a href="{{ forget_password_url }}?email={{ user.email }}">{% trans 'request new one' %}</a>

Loading…
Cancel
Save