12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- # -*- coding: utf-8 -*-
- """
- Created on 2022-06-15
- ---------
- @summary: 企业微信机器人
- ---------
- @author: Dzr
- """
- import requests
- import setting
- def post_data(platform, limit, usage, msg, allow_reset=False):
- """
- :param platform:平台名称
- :param limit:限制次数
- :param usage:使用次数
- :param msg: 通知内容
- :param allow_reset: 解除限制
- :return:
- """
- label = "<font color=\"warning\">{}</font>".format(msg)
- platform = ">平台名称: <font color=\"comment\">{}</font>".format(platform)
- c0 = ">调用上限: <font color=\"comment\">{}次</font>".format(limit)
- c1 = ">调用次数: <font color=\"comment\">{}次</font>".format(usage)
- msg = "{}\n {}\n > {}\n > {}\n".format(label, platform, c0, c1)
- if allow_reset:
- c2 = f"[解除限制]({setting.PLATFORM_API}/v1/images/reset)"
- msg = "{}[{}]\n > {}\n > {}\n {}".format(label, c2, platform, c0, c1)
- content = msg
- markdown = {"msgtype": "markdown", "markdown": {"content": content}}
- text = {
- "msgtype": "text",
- "text": {
- "content": " 请以下同事注意",
- "mentioned_mobile_list": setting.WECHAT_WARNING_PHONES
- }
- }
- body = {'markdown': markdown, 'text': text}
- return body
- def robot_webhook(data):
- headers = {"Content-Type": "application/json"}
- params = {"key": setting.WECHAT_WARNING_ROBOT_KEY}
- url = setting.WECHAT_WARNING_URL
- response = requests.post(url, headers=headers, params=params, json=data)
- return response.json()
- def send_msg(platform, limit, usage, msg, **kwargs):
- contents = post_data(platform, limit, usage, msg, **kwargs)
- for _, content in contents.items():
- response = robot_webhook(content)
- print('企业微信机器人 >>> ', response)
|