robot.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import requests
  2. import platform
  3. # 企业微信联系人
  4. MOBILE_LIST = [
  5. "15639297172", # 张金坤
  6. ]
  7. if platform.system() not in ['Darwin', 'Windows']:
  8. PLATFORM_RUL = "http://pycaptcha.spdata.jianyu360.com"
  9. else:
  10. PLATFORM_RUL = "http://127.0.0.1:2119"
  11. def post_data(platform, limit, usage, msg, allow_reset=False):
  12. """
  13. :param platform:平台名称
  14. :param limit:限制次数
  15. :param usage:使用次数
  16. :param msg: 通知内容
  17. :param allow_reset: 解除限制
  18. :return:
  19. """
  20. label = "<font color=\"warning\">{}</font>".format(msg)
  21. platform = ">平台名称: <font color=\"comment\">{}</font>".format(platform)
  22. c0 = ">调用上限: <font color=\"comment\">{}次</font>".format(limit)
  23. c1 = ">调用次数: <font color=\"comment\">{}次</font>".format(usage)
  24. msg = "{}\n {}\n > {}\n > {}\n".format(label, platform, c0, c1)
  25. if allow_reset:
  26. c2 = f"[解除限制]({PLATFORM_RUL}/v1/images/reset)"
  27. msg = "{}[{}]\n > {}\n > {}\n {}".format(label, c2, platform, c0, c1)
  28. content = msg
  29. markdown = {"msgtype": "markdown", "markdown": {"content": content}}
  30. text = {
  31. "msgtype": "text",
  32. "text": {
  33. "content": " 请以下同事注意",
  34. "mentioned_mobile_list": MOBILE_LIST
  35. }
  36. }
  37. body = {'markdown': markdown, 'text': text}
  38. return body
  39. def robot_webhook(data):
  40. url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
  41. headers = {"Content-Type": "application/json"}
  42. params = {"key": "683a19fe-c72d-464f-acbe-489656f06b05"}
  43. response = requests.post(url, headers=headers, params=params, json=data)
  44. return response.json()
  45. def send_msg(platform, limit, usage, msg, **kwargs):
  46. contents = post_data(platform, limit, usage, msg, **kwargs)
  47. for _, content in contents.items():
  48. response = robot_webhook(content)
  49. print('企业微信机器人 >>> ', response)