美文网首页
钉钉自定义机器人+加签

钉钉自定义机器人+加签

作者: 百试成神 | 来源:发表于2020-04-15 19:18 被阅读0次

钉钉机器人增加了 加签的功能 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&timestamp=%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())

相关文章

网友评论

      本文标题:钉钉自定义机器人+加签

      本文链接:https://www.haomeiwen.com/subject/ecehvhtx.html