钉钉机器人增加了 加签的功能 python版本
# coding:utf-8
import json
import urllib.request
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = '这是你的秘钥'
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))
access_token = "这是你的机器人token地址"
url = "https://oapi.dingtalk.com/robot/send?access_token=%s&sign=%s×tamp=%s"%(access_token,sign,timestamp)
# url为机器人的webhook
header = {
"Content-Type": "application/json",
"Charset": "UTF-8"
}
data = {
"msgtype": "text",
"text": {
"content": "发送的消息的内容"
},
"at": {
"isAtAll": True #@全体成员(在此可设置@特定某人)
}
}
sendData = json.dumps(data)
sendData = sendData.encode("utf-8")
request = urllib.request.Request(url=url, data=sendData, headers=header)
opener = urllib.request.urlopen(request)
print(opener.read())
网友评论