DingTalkUtils.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """
  2. @desc 钉钉报警
  3. @auth ck
  4. 手机号是str,用","分隔,如果不填,默认@所有人
  5. """
  6. import requests
  7. import random
  8. url = "https://oapi.dingtalk.com/robot/send?access_token=ba21cd5591c44593ca7cac05902835e33298c7bf566a5381dc3f01e41c8d5c30"
  9. headers = {'Content-Type': 'application/json;charset=utf-8'}
  10. members = ["13726204048", "15168342316"]
  11. class DingTalkUtils:
  12. """
  13. @phone Str
  14. 不填:默认@所有人
  15. 可多个,按英文逗号隔开
  16. 可填 【ramdom】 随机发送
  17. """
  18. def send(self,msg, phone=""):
  19. if phone == "":
  20. isAtall = True
  21. atMobiles = []
  22. elif phone == "random":
  23. isAtall = False
  24. atMobiles = []
  25. atMobiles.append(random.choice(members))
  26. else:
  27. isAtall = False
  28. atMobiles =phone.split(",")
  29. data = {'msgtype': 'text',
  30. "at": {"isAtAll": isAtall,
  31. "atMobiles": atMobiles},
  32. "text": {"content": msg+"\n[趣程]"}}
  33. requests.post(url=url, headers=headers, json=data)
  34. from functools import wraps
  35. # 钉钉报警装饰器
  36. def DingTalkDecorators(msg):
  37. def wrapper(func):
  38. def deco(*args, **kwargs):
  39. try:
  40. func(*args, **kwargs)
  41. except Exception as e:
  42. DingTalkUtils().send(msg+"->运行出错: "+str(e))
  43. return deco
  44. return wrapper
  45. @DingTalkDecorators("装饰器测试")
  46. def test():
  47. print(3/0)
  48. if __name__ == '__main__':
  49. # DingTalkUtils.send("该下班了","13726204048,11")
  50. # 发送的内容得加上趣程
  51. test()