""" @desc 钉钉报警 @auth ck 手机号是str,用","分隔,如果不填,默认@所有人 """ import requests import random url = "https://oapi.dingtalk.com/robot/send?access_token=ba21cd5591c44593ca7cac05902835e33298c7bf566a5381dc3f01e41c8d5c30" headers = {'Content-Type': 'application/json;charset=utf-8'} members = ["13726204048", "15168342316"] class DingTalkUtils: """ @phone Str 不填:默认@所有人 可多个,按英文逗号隔开 可填 【ramdom】 随机发送 """ def send(self,msg, phone=""): if phone == "": isAtall = True atMobiles = [] elif phone == "random": isAtall = False atMobiles = [] atMobiles.append(random.choice(members)) else: isAtall = False atMobiles =phone.split(",") data = {'msgtype': 'text', "at": {"isAtAll": isAtall, "atMobiles": atMobiles}, "text": {"content": msg+"\n[趣程]"}} requests.post(url=url, headers=headers, json=data) from functools import wraps # 钉钉报警装饰器 def DingTalkDecorators(msg): def wrapper(func): def deco(*args, **kwargs): try: func(*args, **kwargs) except Exception as e: DingTalkUtils().send(msg+"->运行出错: "+str(e)) return deco return wrapper @DingTalkDecorators("装饰器测试") def test(): print(3/0) if __name__ == '__main__': # DingTalkUtils.send("该下班了","13726204048,11") # 发送的内容得加上趣程 test()