1.添加自定义机器人
2.编写python代码请求钉钉机器人所给的webhook
钉钉自定义机器人官方文档
安全方式使用加签的方式:
第一步,把timestamp+"\n"+密钥当做签名字符串,使用HmacSHA256算法计算签名,然后进行Base64 encode,最后再把签名参数再进行urlEncode,得到最终的签名(需要使用UTF-8字符集)。
参数
说明
timestamp
当前时间戳,单位是毫秒,与请求调用时间误差不能超过1小时
secret
密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的字符串
import requests
#python 3.8
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))
print(timestamp)
print(sign)
第二步,把 timestamp和第一步得到的签名值拼接到URL中。
参数
说明
timestamp
第一步使用到的时间戳
sign
第一步得到的签名值
https://oapi.dingtalk.com/robot/send"htmlcode">
url='生成的Webhook×tamp={}&sign={}'.format(timestamp, sign)
print (url)
headers={
'Content-Type':'application/json'
}
json={"msgtype": "text",
"text": {
"content": "888"
} }
resp=requests.post(url=url,headers=headers,json=json)
print (resp.text)
结果:
整体代码:
import requests
#python 3.8
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))
print(timestamp)
print(sign)
url='生成的Webhook×tamp={}&sign={}'.format(timestamp, sign)
print (url)
headers={
'Content-Type':'application/json'
}
json={"msgtype": "text",
"text": {
"content": "测试"
} }
resp=requests.post(url=url,headers=headers,json=json)
print (resp.text)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“python使用自定义钉钉机器人的示例代码”评论...
更新动态
2025年10月29日
2025年10月29日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]


