import time
import requests
import hmac
import json
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = 'SE*********************************************************61'
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))
token = "b349***********************************17b049"
def dingmessage():
webhook = f"https://oapi.dingtalk.com/robot/send?access_token={token}×tamp={timestamp}&sign={sign}"
header = {"Content-Type": "application/json", "Charset": "UTF-8"}
message = {
"msgtype": "text",
"text": {"content": "测试测试"},
"at": {
"atMobiles": [
"135****336"
],
}
}
message_json = json.dumps(message)
info = requests.post(url=webhook, data=message_json, headers=header, verify=False)
print(info.text)
if __name__ == "__main__":
dingmessage()