import urllib, requests, json, hmac, hashlib, base64, time
def notice(text):
"""
钉钉发送通知
:param text: 要发送的内容
:return:
"""
timestamp = str(round(time.time() * 1000))
secret = '' # 申请的钉钉机器人的密钥
http = '' # webhook的地址
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))
# 导入依赖库
headers = {'Content-Type': 'application/json'} # 定义数据类型
# 截至到×tamp之前
webhook = f'{http}×tamp={timestamp}&sign={sign}'
# 定义要发送的数据
data = {
# 定义内容
"msgtype": "markdown",
"markdown": {
"title": "这是定义的标题",
"text": "> 发送的内容\n%s" % text
}
}
requests.post(webhook, data=json.dumps(data), headers=headers) # 发送post请求
notice('')