From 9235b1714816ec3d095e8f2ce67c875c41043f5b Mon Sep 17 00:00:00 2001 From: TingHsi Date: Sun, 8 May 2022 23:50:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E9=92=89=E9=92=89=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E4=BA=BA=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE=E5=8A=A0=E7=AD=BE?= =?UTF-8?q?=E5=AF=86=E9=92=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: TingHsi --- config/README.md | 26 +++++++++++++++++++++ config/push.ini.example | 2 +- push.py | 52 +++++++++++++++++++++++++++++------------ 3 files changed, 64 insertions(+), 16 deletions(-) diff --git a/config/README.md b/config/README.md index 7afe76b..70c1e14 100644 --- a/config/README.md +++ b/config/README.md @@ -98,3 +98,29 @@ `5`对应大别墅 `6`对应崩坏:星穹铁道 + + +# push.ini配置教程 + +* push_server 可选范围 cqhttp ftqq(sever酱) pushplus telegram dingrobot + + +## dingrobot +钉钉群机器人 + +[dingrobot]中的api_url填写**Webhook**地址 + +[setting]中的**push_token**用于填写**安全设置**中**加签**的密钥,此选项为可选项 + +填写示例 + +```ini +[setting] +enable=true +push_server=dingrobot +push_token= + +[dingrobot] +api_url=https://oapi.dingtalk.com/robot/send?access_token=XXX +``` + diff --git a/config/push.ini.example b/config/push.ini.example index 7872516..4d99f60 100644 --- a/config/push.ini.example +++ b/config/push.ini.example @@ -27,4 +27,4 @@ api_url=https://api2.pushdeer.com token= [dingrobot] -api_url=https://oapi.dingtalk.com/robot/send \ No newline at end of file +api_url=https://oapi.dingtalk.com/robot/send?access_token=XXX \ No newline at end of file diff --git a/push.py b/push.py index ec06088..9b14b48 100644 --- a/push.py +++ b/push.py @@ -1,4 +1,9 @@ import os +import time +import base64 +import hashlib +import urllib +import hmac from request import http from loghelper import log from configparser import ConfigParser @@ -98,36 +103,53 @@ def pushdeer(status, push_message): } ) - -# dingding robot +# 钉钉群机器人 def dingrobot(status, push_message): - # print('dingrobot', title(status) + "\r\n" + push_message) - access_token=cfg.get('setting', 'push_token') - # print(f"https://oapi.dingtalk.com/robot/send?access_token={access_token}") + api_url = cfg.get('dingrobot', 'api_url') # https://oapi.dingtalk.com/robot/send?access_token=XXX + secret = cfg.get('setting', 'push_token') # 安全设置 -> 加签 -> 密钥 + + if secret: + timestamp = str(round(time.time() * 1000)) + sign_string = f"{timestamp}\n{secret}" + hmac_code = hmac.new( + key=secret.encode("utf-8"), + msg=sign_string.encode("utf-8"), + digestmod=hashlib.sha256 + ).digest() + sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) + api_url = f"{api_url}×tamp={timestamp}&sign={sign}" + rep = http.post( - url=f"https://oapi.dingtalk.com/robot/send?access_token={access_token}", + url=api_url, headers={"Content-Type": "application/json; charset=utf-8"}, json={ "msgtype": "text", "text": { "content": title(status) + "\r\n" + push_message } } ).json() - print(rep) + log.info(f"推送结果:{rep.get('errmsg')}") def push(status, push_message): if not load_config(): return 0 if cfg.getboolean('setting', 'enable'): - push_server = cfg.get('setting', 'push_server').lower() log.info("正在执行推送......") + func_name = cfg.get('setting', 'push_server').lower() + func = globals().get(func_name) + # print(func) + if not func: + log.warning("推送服务名称错误:请检查config/push.ini -> [setting] -> push_server") + return 0 + log.debug(f"推送所用的服务为:{func_name}") try: - log.debug(f"推送所用的服务为:{push_server}") - eval(push_server[:10] + "(status, push_message)") - except NameError: - log.warning("推送服务名称错误") + # eval(push_server[:10] + "(status, push_message)") + # 与面代码等效 20220508 + func(status, push_message) + except: + log.warning("推送执行错误") + return 0 else: log.info("推送完毕......") - return 0 + return 1 if __name__ == "__main__": - push(0, '推送正文') - + push(0, f'推送验证{int(time.time())}') From dec7a6d0650332cf849bca7a95af5a71e3fac689 Mon Sep 17 00:00:00 2001 From: TingHsi Date: Mon, 9 May 2022 00:52:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=92=89=E9=92=89=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E4=BA=BA=E9=85=8D=E7=BD=AE=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: TingHsi --- config/README.md | 8 ++++---- config/push.ini.example | 3 ++- push.py | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/config/README.md b/config/README.md index 70c1e14..924a195 100644 --- a/config/README.md +++ b/config/README.md @@ -108,9 +108,9 @@ ## dingrobot 钉钉群机器人 -[dingrobot]中的api_url填写**Webhook**地址 +**webhook**填写**Webhook**地址 -[setting]中的**push_token**用于填写**安全设置**中**加签**的密钥,此选项为可选项 +**secret**填写**安全设置**中**加签**的密钥,此选项为可选项 填写示例 @@ -118,9 +118,9 @@ [setting] enable=true push_server=dingrobot -push_token= [dingrobot] -api_url=https://oapi.dingtalk.com/robot/send?access_token=XXX +webhook=https://oapi.dingtalk.com/robot/send?access_token=XXX +secret= ``` diff --git a/config/push.ini.example b/config/push.ini.example index 4d99f60..c139ea4 100644 --- a/config/push.ini.example +++ b/config/push.ini.example @@ -27,4 +27,5 @@ api_url=https://api2.pushdeer.com token= [dingrobot] -api_url=https://oapi.dingtalk.com/robot/send?access_token=XXX \ No newline at end of file +webhook=https://oapi.dingtalk.com/robot/send?access_token=XXX +secret= \ No newline at end of file diff --git a/push.py b/push.py index 9b14b48..ec4aca2 100644 --- a/push.py +++ b/push.py @@ -105,8 +105,8 @@ def pushdeer(status, push_message): # 钉钉群机器人 def dingrobot(status, push_message): - api_url = cfg.get('dingrobot', 'api_url') # https://oapi.dingtalk.com/robot/send?access_token=XXX - secret = cfg.get('setting', 'push_token') # 安全设置 -> 加签 -> 密钥 + api_url = cfg.get('dingrobot', 'webhook') # https://oapi.dingtalk.com/robot/send?access_token=XXX + secret = cfg.get('dingrobot', 'secret') # 安全设置 -> 加签 -> 密钥 -> SEC* if secret: timestamp = str(round(time.time() * 1000))