这种是机器人IP白名单形式
import json
import requests
headers = {'Content-Type': 'application/json;charset=utf-8'}
boturl = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxx"
def msg(text):
# 返回钉钉机器人所需的文本格式
json_text = {
"msgtype": "text",
"text": {
"content": text
},
}
# print(json.dumps(text))
print(requests.post(boturl, json.dumps(json_text), headers=headers).content)
msg('hello world')
机器人加签的方法
加签需要把这段密钥进行转换
如图,说明文档中有签转换方法?
#python 3.8
import time
import hmac
import hashlib
import base64
import urllib.parse
timestamp = str(round(time.time() * 1000))
secret = 'this is 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)
会输出两段,一个时间戳,一段签码
然后拼接进行拼接
https://oapi.dingtalk.com/robot/send?access_token=XXXXXX×tamp=XXX&sign=XXX
|