import time import hmac import hashlib import base64 import urllib.parse import requests,json #导入依赖库 def test(): timestamp = str(round(time.time() * 1000)) secret = 'SEC2f90ac798a4a94780ce52281047c10362b20bc6c6f27181cf7ae5ef548525f41' 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) headers={'Content-Type': 'application/json'} #定义数据类型 webhook = 'https://oapi.dingtalk.com/robot/send?access_token=8020df878c6f6508f076c5dc291960f798b982aca1b0aa4e4bee9facec8321ba×tamp='+timestamp+"&sign="+sign #定义要发送的数据 #"at": {"atMobiles": "['"+ mobile + "']" data = { "msgtype": "text", "text": {"content": '都谁没加到群里来?小心升不了班'}, "at": { "atMobiles": [ "13032201605" ], "isAtAll":False } } data2 = { "feedCard": { "links": [ { "title": "argparse库", "messageURL": "https://blog.csdn.net/codename_cys/article/details/107505718", "picURL": "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRBba_xRVN7Di9Io6_1Oc54lFzcyA7FzDXEtQ&usqp=CAU" }, { "title": "pydantic库简介", "messageURL": "https://blog.csdn.net/codename_cys/article/details/107675748", "picURL": "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRBba_xRVN7Di9Io6_1Oc54lFzcyA7FzDXEtQ&usqp=CAU" } ] }, "msgtype": "feedCard" } data3 = { "msgtype": "markdown", "markdown": { "title":"杭州天气", "text": "#### 杭州天气 @150XXXXXXXX \n> 9度,西北风1级,空气良89,相对温度73%\n> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n> ###### 10点20分发布 [天气](https://www.dingalk.com) \n" }, "at": { "atMobiles": [ "13032201605" ], "isAtAll": False } } res = requests.post(webhook, data=json.dumps(data3), headers=headers) #发送post请求 print(res.text) def sendMsg(msg, atuser): timestamp = str(round(time.time() * 1000)) secret = 'SEC2f90ac798a4a94780ce52281047c10362b20bc6c6f27181cf7ae5ef548525f41' 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) headers = {'Content-Type': 'application/json'} # 定义数据类型 webhook = 'https://oapi.dingtalk.com/robot/send?access_token=8020df878c6f6508f076c5dc291960f798b982aca1b0aa4e4bee9facec8321ba×tamp=' + timestamp + "&sign=" + sign atTittle = "" for n in atuser: atTittle += "@{}".format(n) data = { "msgtype": "markdown", "markdown": { "title": "通知", "text": "{}{}".format(atTittle, msg) }, "at": { "atMobiles": atuser, "isAtAll": False } } res = requests.post(webhook, data=json.dumps(data), headers=headers) # 发送post请求 print(res.text) if __name__ == '__main__': MSG = """ #### 机号:{} ##### 状态:{} ##### 时间:{} ##### 内容:{} ###### ------------- """.format('B-6419','未完成', '22点31分', 'xxxxxxxxxxx') nmsg = "" for i in range(10): nmsg+=MSG sendMsg(nmsg, ['13032201605'])