python 发送 钉钉自定义机器人消息

发布时间:2024-08-10 09:01

def dd(dd_token, dd_secret, dd_txt):
    timestamp = str(round(time.time() * 1000))
    secret = dd_secret  # 钉钉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 = \'https://oapi.dingtalk.com/robot/send?access_token=\' + dd_token + \'×tamp=\' + timestamp + \'&sign=\' + sign

    print(url)
    data = {
        \"msgtype\": \"text\",
        \"text\": {\"content\": dd_txt}
    }

    headers = {\'Content-Type\': \'application/json\'}

    res = requests.post(url=url, data=json.dumps(data), headers=headers)

    print(res.text)

dd_secret = \'SECxxxxxxxxxxxxxxx\' #需要开启机器人加签 此处为加签的secret
dd_token = \'06a6xxxxxxxxxxxxxxx\' #自定义机器人的 token
dd_txt = \'要发送的内容\'

dd(dd_token, dd_secret, dd_txt)

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号