python发送钉钉消息





import json
import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import urllib3
urllib3.disable_warnings()
def send_ding_msg(token_info, secret_info, mobile_list, send_msg):
header_info = {'Content-Type': 'application/json', "Charset": "UTF-8"}
timestamp = str(round(time.time() * 1000))
# 这里替换为自己复制过来的加签秘钥
secret = secret_info
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))
final_url = f'https://oapi.dingtalk.com/robot/send?access_token={token_info}×tamp={timestamp}&sign={sign}'
# 钉钉消息格式,其中 msg 就是我们要发送的具体内容
data = {"at": {"isAtAll": False, "atMobiles": mobile_list}, "text": {"content": send_msg},"msgtype": "text"}
requests.post(url=final_url, data=json.dumps(data), headers=header_info, verify=False)
浙公网安备 33010602011771号