You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
python-project/api/dingding.py

39 lines
1.2 KiB

3 years ago
#!/usr/bin/python3
#-*-coding: utf-8-*-
# by QianFeng.newrain
#
'''
Welcome QianFeng cloud computing
'''
import requests
import json
import sys
import time,hmac,base64,hashlib,urllib.parse
headers = {'Content-Type': 'application/json;charset=utf-8'}
3 years ago
# 需要修改,钉钉群助手中获取
timestamp = str(round(time.time() * 1000))
secret = 'SECf3aa0ec68151d6a0599d8d9db4fa40afa5341c358d53af717eb924f0fa29ee5f'
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))
api_url = f"https://oapi.dingtalk.com/robot/send?access_token=abdec0db67d1ca355b547711f822e0bb60929295cd3c42c1e4c89c7f24b4872e&timestamp={timestamp}&sign={sign}"
3 years ago
def msg(text):
json_text= {
"msgtype": "text",
"at": {
"atMobiles": [
"all"
],
"isAtAll": False
},
"text": {
"content": text
}
}
print(requests.post(api_url,json.dumps(json_text),headers=headers).content.decode())
3 years ago
msg('\n'+sys.argv[1])