Python发送钉钉消息
Posted 黑。白。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python发送钉钉消息相关的知识,希望对你有一定的参考价值。
import requests
url = "https://oapi.dingtalk.com/robot/send"
import time
import hmac
import hashlib
import base64
import urllib.parse
def get_dd_sign():
timestamp = str(round(time.time() * 1000))
secret = \'填写钉钉群助手获取的seret\'
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))
return timestamp,sign
def send_dd_msg(msg,at=None,at_all=False):
data = {
"msgtype": "text",
"text": {
"content": msg
},
"at": {
"atMobiles": at,
"isAtAll": at_all
}
}
timestamp,sign = get_dd_sign()
params = {
"access_token":"填写获取的token",
"timestamp":timestamp,
"sign":sign
}
try:
r = requests.post(url,json=data,params=params)
except:
print("滴滴消息没有发送成功")
else:
if r.json().get("errcode")==0:
return True
以上是关于Python发送钉钉消息的主要内容,如果未能解决你的问题,请参考以下文章