钉钉推送如果你想通过钉钉接收信息通知(注意不是钉钉邮件,所有邮件操作在上一节已经介绍过了)设置由于钉钉的个人权限,钉钉上的群机器人。企业权限请参考https://developers.dingtalk.com/document添加自定义机器人并指定群组获取Webhook地址。为了安全起见,添加一个签名,即secret根据https://developers.dingtalk。com/document/app/custom-robot-access?spm=ding_open_doc.document.0.0.6d9d28e1ji2ImR#topic-2026027使用Python实现机器人推送功能textpush#-*-coding:utf-8-*-importrequestsimportjsonimporttimeimporthmacimporthashlibimportbase64importurllib.parsedefdingMessage():timestamp=str(round(time.time()*1000))secret='SEC21022...cf06a65a761c86eb4027b'secret_enc=secret.encode('utf-8')string_to_sign='{}\n{}'.format(timestamp,secret)string_to_sign_enc=string_to_sign.encode('utf-8')hmac_code=hmac.new(secret_enc,string_to_sign_enc,digestmod=hashlib.sha256).digest()sign=urllib.parse.quote_plus(base64.b64encode(hmac_code))print(timestamp)print(sign)#请求URL,WebHook地址webhook=f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4...7bc0ec08354cc181a×tamp={蒂姆estamp}&sign={sign}"#构建请求头header={"Content-Type":"application/json","Charset":"UTF-8"}#构建请求数据tex="call0call0No"message={"msgtype":"text","text":{"content":f"@137....97091{tex}"},#todowhether@everyone"at":{"atMobiles":["137...7091"],"isAtAll":False}}#将请求的数据封装在json中message_json=json.dumps(message)#发送请求信息=requests.post(url=webhook,data=message_json,headers=header)#打印返回结果print(info.text)if__name__=="__main__":dingMessage()做了什么秘密?sha256加密后编码)tex文本信息消息主题对象,选择@relatedperson(这里以手机号为准)markdownpush#-*-coding:utf-8-*-importjsonimportrequestsdefdingmessage():importtimeimporthmacimporthashlib导入base64导入urllib.parse时间戳=str(轮(时间.时间()*1000))secret='SEC21022b79....9cf06a65a761c86eb4027b'secret_enc=secret.encode('utf-8')string_to_sign='{}\n{}'.format(时间戳,秘密)string_to_sign_enc=string_to_sign.encode('utf-8')hmac_code=hmac.new(secret_enc,string_to_sign_enc,digestmod=hashlib.sha256).digest()sign=urllib.parse.quote_plus(base64.b64encode(hmac_code))print(timestamp)print(sign)#请求的URL,WebHook地址webhook=f"https://oapi.dingtalk.com/robot/send?access_token=33576dfc....4800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}"#构建请求头header={"Content-Type":"application/json","Charset":"UTF-8"}message={"msgtype":"markdown",#"text":{#"content":f"@137...97091{tex}"#},"markdown":{"title":"杭州天气","text":"####杭州天气@155...773\n>900度,西北风1级,空气好8009,相对温度673%\n>[](https://developers.dingtalk.com/document/app/send-normal-messages)\n>######10:20发布[天气](https://www.dingtalk.com)\n"},#todowhether@everyone"at":{"atMobiles":["15....773"],"isAtAll":False}}#根据请求数据是封装在json中message_json=json.dumps(message)#发送请求info=requests.post(url=webhook,data=message_json,headers=header)#打印返回结果print(info.text)if__name__=="__main__":dingmessage()网页链接#-*-编码:utf-8-*-importjsonimportrequestsdefdingmessage():importtimeimporthmacimporthashlibimportbase64importurllib.parsetimestamp=str(round(time.time()*1000))secret='SEC21022b792577....8639cf06a65a761c86eb4027b'secret_enc=secret.encode('utf-8')string_to_sign='{}\n{}'.format(timestamp,secret)string_to_signf_enc=stringen_code('s-8')hmac_code=hmac.new(secret_enc,string_to_sign_enc,digestmod=hashlib.sha256).digest()sign=urllib.parse.quote_plus(base64.b64encode(hmac_code))print(timestamp)print(sign)#请求URL,WebHook地址webhook=f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4....800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}"header={"Content-Type":"application/json","Charset":"UTF-8"}message={"msgtype":"link","link":{"text":"这个即将推出的新版本,创始人xx称它为mangrove在此之前,每当面临重大升级时,产品经理都会根据情况选择一个代号。这次为什么是红树林","title":"时代列车前行","picUrl":"","messageUrl":"https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"},#todowhether@everyone"at":{"atMobiles":["15....8773"],"isAtAll":False}}message_json=json.dumps(message)info=requests.post(url=webhook,data=message_json,headers=header)print(info.text)if__name__=="__main__":dingmessage()卡片推送整体跳转ActionCardtype#-*-coding:utf-8-*-importjsonimportrequestsdefdingmessage():importtimeimporthmacimporthashlibimportbase64importurllib.parsetimestamp=str(圆(time.time()*1000))secret='SEC21022b7925....064668639cf06a65a761c86eb4027b'secret_enc=secret.encode('utf-8')string_to_sign='{}\n{}'.format(timestamp,secret)string_to_sign_string_to_sign.encode('utf-8')hmac_code=hmac.new(secret_enc,string_to_sign_enc,digestmod=hashlib.sha256).digest()sign=urllib.parse.quote_plus(base64.b64encode(hmac_code))打印(时间戳)打印(sign)#请求的URL,WebHook地址webhook=f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb.....4800d577bc0ec08354cc181a×tamp={timestamp}&sign={sign}"header={"Content-Type":"application/json","Charset":"UTF-8"}message={"msgtype":"actionCard","actionCard":{"text":"\n\n####乔布斯20年前想建的AppleCafe\n\nAppleStore的设计从最初的完整科技感正在走向日常生活,它的??日常生活趋势其实可以追溯到20年前苹果要建咖啡馆的计划”,"title":"时代列车向前行驶","btnOrientation":"0","singleTitle":"阅读全文","singleURL":"https://www.dingtalk.com/"},#todo是否@all"at":{"atMobiles":["155....8773"],"isAtAll":False}}message_json=json.dumps(message)info=requests.post(url=webhook,data=message_json,headers=header)print(info.text)if__name__=="__main__":dingmessage()独立跳转ActionCardtype#-*-coding:utf-8-*-importjsonimportrequestsdefdingmessage():importtimeimporthmacimporthashlibimportbase64importurllib.parsetimestamp=str(round(time.time()*1000))secret='SEC21022b792.....39cf06a65a761c86eb4027b'secret_enc=secret.encode('utf-8')string_to_sign='{}\n{}'.format(timestamp,secret)string_to_sign_enc=string_to_sign.encode('utf-8')hmac_code=hmac.new(secret_enc,string_to_sign_enc,digestmod=hashlib.sha256).digest()sign=urllib.parse.quote_plus(base64.b64encode(hmac_code))print(timestamp)print(sign)#请求URL,WebHook地址webhook=f"https://oapi.dingtalk.com/robot/send?access_token=33576dfcb4.....0ec08354cc181a×tamp={timestamp}&sign={sign}"header={"Content-Type":"application/json","Charset":"UTF-8"}message={"msgtype":"actionCard","actionCard":{"text":"\n\n####乔布斯20年前想要打造的AppleCafé\n\nAppleStore的设计正在从最初的科技感走向生活,它的??生活潮流其实可以追溯到苹果20年ago建咖啡馆的计划","title":"时代列车向前行驶","hideAvatar":"0","btnOrientation":"1","btns":[{"title":"好内容","actionURL":"https://www.dingtalk.com/"},{"title":"不感兴趣","actionURL":"https://www.dingtalk.com/"}]},#todowhether@everyone"at":{"atMobiles":["155....8773"],"isAtAll":False}}message_json=json.dumps(message)info=requests.post(url=webhook,data=message_json,headers=header)print(info.text)if__name__=="__main__":dingmessage()以上是钉钉的几个群机器人通知类型总而言之,钉钉机器人推送是一个非常不错的选择,足以应对日常工作中的各种场景(定时提醒、阈值报警、事件通知等)