Files
2026-05-07 10:49:58 +08:00

41 lines
863 B
Python

import time
import hmac
import hashlib
import base64
import urllib.parse
import requests
import json
from config import ACCESS_TOKEN, SECRET
def get_sign():
timestamp = str(round(time.time() * 1000))
string_to_sign = f"{timestamp}\n{SECRET}"
hmac_code = hmac.new(
SECRET.encode("utf-8"),
string_to_sign.encode("utf-8"),
hashlib.sha256
).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
return timestamp, sign
def send(msg):
timestamp, sign = get_sign()
url = f"https://oapi.dingtalk.com/robot/send?access_token={ACCESS_TOKEN}&timestamp={timestamp}&sign={sign}"
headers = {"Content-Type": "application/json"}
data = {
"msgtype": "text",
"text": {
"content": msg
}
}
requests.post(url, headers=headers, data=json.dumps(data))