python版本(3.8-3.10)
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))
import requests,json
headers={'Content-Type': 'application/json'}
webhook = '机器人的webhook'+'×tamp='+timestamp+"&sign="+sign
data = data = {
"text": {
"content":"大家好"
},
"msgtype":"text"
}
res = requests.post(webhook, data=json.dumps(data), headers=headers)
print(res.text)
运行即可
可发送格式
data = {
"at": {
"atMobiles":[
"被@人的手机号"
],
"atUserIds":[
"被@人的用户userid"
],
"isAtAll": "是否@所有人" False True
},
"text": {
"content":"大家好"
},
"msgtype":"text"
}
data = {
"msgtype": "link",
"link": {
"text": "消息内容",
"title": "消息标题",
"picUrl": "图片链接",
"messageUrl": "点击消息跳转的链接"
}
}
data = {
"msgtype": "markdown",
"markdown": {
"title":"消息标题",
"text": "消息内容"
},
"at": {
"atMobiles": [
"被@人的手机号"
],
"atUserIds": [
"被@人的用户userid"
],
"isAtAll": "是否@所有人" False True
}
}
官网文档:https://open.dingtalk.com/document/robots/custom-robot-access