钉钉机器人自定义参数

This commit is contained in:
Akimio521 2024-02-25 14:42:30 +08:00
parent 925ed9f6fc
commit 01a40e21b3

View File

@ -172,24 +172,30 @@ def console(title: str, content: str, **kwargs) -> None:
print(f"{title}\n\n{content}") print(f"{title}\n\n{content}")
def dingding_bot(title: str, content: str) -> None: def dingding_bot(title: str, content: str, **kwargs) -> None:
""" """
使用 钉钉机器人 推送消息 使用 钉钉机器人 推送消息
""" """
if not push_config.get("DD_BOT_SECRET") or not push_config.get("DD_BOT_TOKEN"): if not ((kwargs.get("DD_BOT_SECRET") and kwargs.get("DD_BOT_TOKEN")) or (push_config.get("DD_BOT_SECRET") and push_config.get("DD_BOT_TOKEN"))):
print("钉钉机器人 服务的 DD_BOT_SECRET 或者 DD_BOT_TOKEN 未设置!!\n取消推送") print("钉钉机器人 服务的 DD_BOT_SECRET 或者 DD_BOT_TOKEN 未设置!!\n取消推送")
return return
print("钉钉机器人 服务启动") print("钉钉机器人 服务启动")
if kwargs.get("DD_BOT_SECRET") and kwargs.get("DD_BOT_TOKEN"):
DD_BOT_SECRET = kwargs.get("DD_BOT_SECRET")
DD_BOT_TOKEN = kwargs.get("DD_BOT_TOKEN")
else:
DD_BOT_SECRET = push_config.get("DD_BOT_SECRET")
DD_BOT_TOKEN = push_config.get("DD_BOT_TOKEN")
timestamp = str(round(time.time() * 1000)) timestamp = str(round(time.time() * 1000))
secret_enc = push_config.get("DD_BOT_SECRET").encode("utf-8") secret_enc = DD_BOT_SECRET.encode("utf-8")
string_to_sign = "{}\n{}".format(timestamp, push_config.get("DD_BOT_SECRET")) string_to_sign = "{}\n{}".format(timestamp, DD_BOT_SECRET)
string_to_sign_enc = string_to_sign.encode("utf-8") string_to_sign_enc = string_to_sign.encode("utf-8")
hmac_code = hmac.new( hmac_code = hmac.new(
secret_enc, string_to_sign_enc, digestmod=hashlib.sha256 secret_enc, string_to_sign_enc, digestmod=hashlib.sha256
).digest() ).digest()
sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
url = f'https://oapi.dingtalk.com/robot/send?access_token={push_config.get("DD_BOT_TOKEN")}&timestamp={timestamp}&sign={sign}' url = f'https://oapi.dingtalk.com/robot/send?access_token={DD_BOT_TOKEN}&timestamp={timestamp}&sign={sign}'
headers = {"Content-Type": "application/json;charset=utf-8"} headers = {"Content-Type": "application/json;charset=utf-8"}
data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}} data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}}
response = requests.post( response = requests.post(