From 01a40e21b3a07a7b4e638b2e14184705a1ac1b80 Mon Sep 17 00:00:00 2001 From: Akimio521 Date: Sun, 25 Feb 2024 14:42:30 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=89=E9=92=89=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sample/notify.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/sample/notify.py b/sample/notify.py index 264bd9b4..245319e2 100644 --- a/sample/notify.py +++ b/sample/notify.py @@ -172,24 +172,30 @@ def console(title: str, content: str, **kwargs) -> None: 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取消推送") return 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)) - secret_enc = push_config.get("DD_BOT_SECRET").encode("utf-8") - string_to_sign = "{}\n{}".format(timestamp, push_config.get("DD_BOT_SECRET")) + secret_enc = DD_BOT_SECRET.encode("utf-8") + string_to_sign = "{}\n{}".format(timestamp, DD_BOT_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)) - url = f'https://oapi.dingtalk.com/robot/send?access_token={push_config.get("DD_BOT_TOKEN")}×tamp={timestamp}&sign={sign}' + url = f'https://oapi.dingtalk.com/robot/send?access_token={DD_BOT_TOKEN}×tamp={timestamp}&sign={sign}' headers = {"Content-Type": "application/json;charset=utf-8"} data = {"msgtype": "text", "text": {"content": f"{title}\n\n{content}"}} response = requests.post(