网站首页 > python 正文

python推送钉钉群消息

admin 2024-01-08 09:38:48 python 73 ℃
正文

python推送钉钉群消息

import time
import hmac
import hashlib
import base64
import urllib.parse
import requests,json
#钉钉加签
webhook='https://oapi.dingtalk.com/robot/send?access_token=********' #钉钉机器人webhook
timestamp = str(round(time.time() * 1000))
secret = 'SEC********'  #钉钉机器人秘钥
secret_enc = secret.encode('utf-8')
string_to_sign = '{}\n{}'.format(timestamp, secret)
# print(string_to_sign)
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))
webhook=webhook+'&timestamp='+timestamp+'&sign='+sign
print(webhook)
#定义数据类型
headers={'Content-Type':'application/json'}
msgtitle="new msg"
data={"msgtype":"text","text":{"content":f'通知:{msgtitle}!!!'},"isAtAll":True}
#发送post请求
res=requests.post(webhook,data=json.dumps(data),headers=headers)
print(res.text)


本文TAG:

蓝色枫叶